blob: e7945402a361c3e9a0807b25bd2389796ff27d81 [file] [log] [blame]
Martin Stjernholmc15e7e42020-12-02 22:50:53 +00001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <sstream>
20#include <string>
21#include <string_view>
22#include <vector>
23
24namespace android {
25namespace base {
26
27// Splits a string into a vector of strings.
28//
29// The string is split at each occurrence of a character in delimiters.
30//
31// The empty string is not a valid delimiter list.
32std::vector<std::string> Split(const std::string& s,
33 const std::string& delimiters);
34
android-t13d2c5b22022-10-12 13:43:18 +080035// Splits a string into a vector of string tokens.
36//
37// The string is split at each occurrence of a character in delimiters.
38// Coalesce runs of delimiter bytes and ignore delimiter bytes at the start or
39// end of string. In other words, return only nonempty string tokens.
40// Use when you don't care about recovering the original string with Join().
41//
42// Example:
43// Tokenize(" foo bar ", " ") => {"foo", "bar"}
44// Join(Tokenize(" foo bar", " "), " ") => "foo bar"
45//
46// The empty string is not a valid delimiter list.
47std::vector<std::string> Tokenize(const std::string& s, const std::string& delimiters);
48
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000049// Trims whitespace off both ends of the given string.
50std::string Trim(const std::string& s);
51
52// Joins a container of things into a single string, using the given separator.
53template <typename ContainerT, typename SeparatorT>
54std::string Join(const ContainerT& things, SeparatorT separator) {
55 if (things.empty()) {
56 return "";
57 }
58
59 std::ostringstream result;
60 result << *things.begin();
61 for (auto it = std::next(things.begin()); it != things.end(); ++it) {
62 result << separator << *it;
63 }
64 return result.str();
65}
66
67// We instantiate the common cases in strings.cpp.
68extern template std::string Join(const std::vector<std::string>&, char);
69extern template std::string Join(const std::vector<const char*>&, char);
70extern template std::string Join(const std::vector<std::string>&, const std::string&);
71extern template std::string Join(const std::vector<const char*>&, const std::string&);
72
73// Tests whether 's' starts with 'prefix'.
74bool StartsWith(std::string_view s, std::string_view prefix);
75bool StartsWith(std::string_view s, char prefix);
76bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix);
77
78// Tests whether 's' ends with 'suffix'.
79bool EndsWith(std::string_view s, std::string_view suffix);
80bool EndsWith(std::string_view s, char suffix);
81bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix);
82
83// Tests whether 'lhs' equals 'rhs', ignoring case.
84bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs);
85
86// Removes `prefix` from the start of the given string and returns true (if
87// it was present), false otherwise.
88inline bool ConsumePrefix(std::string_view* s, std::string_view prefix) {
89 if (!StartsWith(*s, prefix)) return false;
90 s->remove_prefix(prefix.size());
91 return true;
92}
93
94// Removes `suffix` from the end of the given string and returns true (if
95// it was present), false otherwise.
96inline bool ConsumeSuffix(std::string_view* s, std::string_view suffix) {
97 if (!EndsWith(*s, suffix)) return false;
98 s->remove_suffix(suffix.size());
99 return true;
100}
101
102// Replaces `from` with `to` in `s`, once if `all == false`, or as many times as
103// there are matches if `all == true`.
104[[nodiscard]] std::string StringReplace(std::string_view s, std::string_view from,
105 std::string_view to, bool all);
106
android-t13d2c5b22022-10-12 13:43:18 +0800107// Converts an errno number to its error message string.
108std::string ErrnoNumberAsString(int errnum);
109
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000110} // namespace base
111} // namespace android