1 // Copyright (C) 2015 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #pragma once
16
17 #include <functional>
18 #include <iterator>
19 #include <sstream>
20 #include <string>
21 #include <string_view>
22 #include <vector>
23
24 #include <stddef.h>
25
26 #ifdef _WIN32
27 // Returns a pointer to the first occurrence of |needle| in |haystack|, or a
28 // NULL pointer if |needle| is not part of |haystack|.
29 // Intentionally in global namespace. This is already provided by the system
30 // C library on Linux and OS X.
31 extern "C" const void* memmem(const void* haystack, size_t haystack_len,
32 const void* needle, size_t needlelen);
33 #endif // _WIN32
34
35 namespace android {
36 namespace base {
37
38 // Copy the content of |view| into a new heap-allocated zero-terminated
39 // C string. Caller must free() the result.
40 char* strDup(std::string_view str);
41
42 // Returns true iff |haystack| contains |needle|.
43 bool strContains(std::string_view haystack, const char* needle);
44
45 // Joins all elements from the |range| into a single string, using |delim|
46 // as a delimiter
47 template <class Range, class Delimiter>
join(const Range & range,const Delimiter & delim)48 std::string join(const Range& range, const Delimiter& delim) {
49 std::ostringstream out;
50
51 // make sure we use the ADL versions of begin/end for custom ranges
52 using std::begin;
53 using std::end;
54 auto it = begin(range);
55 const auto itEnd = end(range);
56 if (it != itEnd) {
57 out << *it;
58 for (++it; it != itEnd; ++it) {
59 out << delim << *it;
60 }
61 }
62
63 return out.str();
64 }
65
66 // Convenience version of join() with delimiter set to a single comma
67 template <class Range>
join(const Range & range)68 std::string join(const Range& range) {
69 return join(range, ',');
70 }
71
72 // Returns a version of |in| with whitespace trimmed from the front/end
73 std::string trim(const std::string& in);
74
75 bool startsWith(std::string_view string, std::string_view prefix);
76 bool endsWith(std::string_view string, std::string_view suffix);
77
78 // Iterates over a string's parts using |splitBy| as a delimiter.
79 // |splitBy| must be a nonempty string well, or it's a no-op.
80 // Otherwise, |func| is called on each of the splits, excluding the
81 // characters that are part of |splitBy|. If two |splitBy|'s occur in a row,
82 // |func| will be called on a String("") in between. See
83 // StringUtils_unittest.cpp for the full story.
84 template <class String>
split(String str,String splitBy,std::function<void (const String &)> func)85 void split(String str, String splitBy, std::function<void(const String&)> func) {
86 if (splitBy.empty()) return;
87
88 size_t splitSize = splitBy.size();
89 size_t begin = 0;
90 size_t end = str.find(splitBy);
91
92 while (true) {
93 func(str.substr(begin, end - begin));
94 if (end == std::string::npos) return;
95 begin = end + splitSize;
96 end = str.find(splitBy, begin);
97 }
98 }
99
100 // Tokenlize a string using |splitBy| as a delimiter.
101 // |splitBy| must be a nonempty string well, or it's a no-op.
102 // Whitespace is removed.
103 // Note: make sure out is an empty vector. It will be cleared
104 // before store the result tokens
105 void splitTokens(const std::string& input,
106 std::vector<std::string>* out,
107 std::string_view splitBy);
108
109 // Splits a string into a vector of strings.
110 //
111 // The string is split at each occurrence of a character in delimiters.
112 //
113 // The empty string is not a valid delimiter list.
114 std::vector<std::string> Split(const std::string& s,
115 const std::string& delimiters);
116
117 // Trims whitespace off both ends of the given string.
118 std::string Trim(const std::string& s);
119
120 // Joins a container of things into a single string, using the given separator.
121 template <typename ContainerT, typename SeparatorT>
Join(const ContainerT & things,SeparatorT separator)122 std::string Join(const ContainerT& things, SeparatorT separator) {
123 if (things.empty()) {
124 return "";
125 }
126
127 std::ostringstream result;
128 result << *things.begin();
129 for (auto it = std::next(things.begin()); it != things.end(); ++it) {
130 result << separator << *it;
131 }
132 return result.str();
133 }
134
135 // We instantiate the common cases in strings.cpp.
136 extern template std::string Join(const std::vector<std::string>&, char);
137 extern template std::string Join(const std::vector<const char*>&, char);
138 extern template std::string Join(const std::vector<std::string>&,
139 const std::string&);
140 extern template std::string Join(const std::vector<const char*>&,
141 const std::string&);
142
143 // Tests whether 's' starts with 'prefix'.
144 bool StartsWith(std::string_view s, std::string_view prefix);
145 bool StartsWith(std::string_view s, char prefix);
146 bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix);
147
148 // Tests whether 's' ends with 'suffix'.
149 bool EndsWith(std::string_view s, std::string_view suffix);
150 bool EndsWith(std::string_view s, char suffix);
151 bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix);
152
153 // Tests whether 'lhs' equals 'rhs', ignoring case.
154 bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs);
155
156 // Removes `prefix` from the start of the given string and returns true (if
157 // it was present), false otherwise.
ConsumePrefix(std::string_view * s,std::string_view prefix)158 inline bool ConsumePrefix(std::string_view* s, std::string_view prefix) {
159 if (!StartsWith(*s, prefix))
160 return false;
161 s->remove_prefix(prefix.size());
162 return true;
163 }
164
165 // Removes `suffix` from the end of the given string and returns true (if
166 // it was present), false otherwise.
ConsumeSuffix(std::string_view * s,std::string_view suffix)167 inline bool ConsumeSuffix(std::string_view* s, std::string_view suffix) {
168 if (!EndsWith(*s, suffix))
169 return false;
170 s->remove_suffix(suffix.size());
171 return true;
172 }
173
174 // Replaces `from` with `to` in `s`, once if `all == false`, or as many times as
175 // there are matches if `all == true`.
176 [[nodiscard]] std::string StringReplace(std::string_view s,
177 std::string_view from,
178 std::string_view to,
179 bool all);
180 } // namespace base
181 } // namespace android
182