xref: /aosp_15_r20/system/libbase/include/android-base/strings.h (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1 /*
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 <ctype.h>
20 
21 #include <iterator>
22 #include <numeric>
23 #include <set>
24 #include <sstream>
25 #include <string>
26 #include <string_view>
27 #include <type_traits>
28 #include <unordered_set>
29 #include <utility>
30 #include <vector>
31 
32 namespace android {
33 namespace base {
34 
35 // Splits a string into a vector of strings.
36 //
37 // The string is split at each occurrence of a character in delimiters.
38 //
39 // The empty string is not a valid delimiter list.
40 std::vector<std::string> Split(const std::string& s,
41                                const std::string& delimiters);
42 
43 // Splits a string into a vector of string tokens.
44 //
45 // The string is split at each occurrence of a character in delimiters.
46 // Coalesce runs of delimiter bytes and ignore delimiter bytes at the start or
47 // end of string. In other words, return only nonempty string tokens.
48 // Use when you don't care about recovering the original string with Join().
49 //
50 // Example:
51 //   Tokenize(" foo  bar ", " ") => {"foo", "bar"}
52 //   Join(Tokenize("  foo  bar", " "), " ") => "foo bar"
53 //
54 // The empty string is not a valid delimiter list.
55 std::vector<std::string> Tokenize(const std::string& s, const std::string& delimiters);
56 
57 namespace internal {
58 template <typename>
59 constexpr bool always_false_v = false;
60 }
61 
62 template <typename T>
Trim(T && t)63 std::string Trim(T&& t) {
64   std::string_view sv;
65   std::string s;
66   if constexpr (std::is_convertible_v<T, std::string_view>) {
67     sv = std::forward<T>(t);
68   } else if constexpr (std::is_convertible_v<T, std::string>) {
69     // The previous version of this function allowed for types which are implicitly convertible
70     // to std::string but not to std::string_view. For these types we go through std::string first
71     // here in order to retain source compatibility.
72     s = t;
73     sv = s;
74   } else {
75     static_assert(internal::always_false_v<T>,
76                   "Implicit conversion to std::string or std::string_view not possible");
77   }
78 
79   // Skip initial whitespace.
80   while (!sv.empty() && isspace(sv.front())) {
81     sv.remove_prefix(1);
82   }
83 
84   // Skip terminating whitespace.
85   while (!sv.empty() && isspace(sv.back())) {
86     sv.remove_suffix(1);
87   }
88 
89   return std::string(sv);
90 }
91 
92 // We instantiate the common cases in strings.cpp.
93 extern template std::string Trim(const char*&);
94 extern template std::string Trim(const char*&&);
95 extern template std::string Trim(const std::string&);
96 extern template std::string Trim(const std::string&&);
97 extern template std::string Trim(std::string_view&);
98 extern template std::string Trim(std::string_view&&);
99 
100 // Joins a container of things into a single string, using the given separator.
101 template <typename ContainerT, typename SeparatorT>
Join(ContainerT && things,SeparatorT separator)102 std::string Join(ContainerT&& things, SeparatorT separator) {
103   using ElementType = typename std::remove_reference_t<ContainerT>::value_type;
104 
105   if (things.empty()) {
106     return {};
107   } else if (things.size() == 1) {
108     // Nothing to do! Return the first element if it's already a string-like type, otherwise
109     // fallthrough to the slower format-conversion case at the bottom of this function.
110 
111     if constexpr (std::is_convertible_v<ElementType, std::string>) {
112       return *things.begin();
113     } else if constexpr (std::is_constructible_v<std::string, ElementType>) {
114       // std::string_view is not implicitly convertible to std::string so do it explicitly, making
115       // a copy in this case.
116       return std::string(*things.begin());
117     }
118   }
119 
120   if constexpr (std::is_convertible_v<ElementType, std::string_view>) {
121     // String-like types are what the vast majority of callers use.
122     // Use a much faster implementation for these types.
123 
124     // char separator types need special handling because they cannot be converted to
125     // std::string_view to determine their size, and they require a special std::string::append
126     // invocation below.
127     constexpr bool sepIsChar = std::is_same_v<std::remove_cv_t<SeparatorT>, char>;
128     std::string_view::size_type sepSize;
129     if constexpr (sepIsChar) sepSize = 1;
130     else                     sepSize = std::string_view(separator).size();
131 
132     const std::string_view::size_type total = std::accumulate(
133         std::next(things.begin()), things.end(), std::string_view(*things.begin()).size(),
134         [&sepSize](std::string_view::size_type sum, std::string_view sv) {
135           return sum + sepSize + sv.size();
136         }
137     );
138 
139     std::string result;
140     result.reserve(total);  // allocate once
141     result.append(*things.begin());
142     for(auto it = std::next(things.begin()); it != things.end(); ++it) {
143       if constexpr (sepIsChar) result.append(1, separator).append(*it);
144       else                     result.append(separator).append(*it);
145     }
146     return result;
147 
148   } else {
149     // Some callers depend on the conversion performed by std::ostream:operator<< to get string
150     // representations from non-string types.
151 
152     std::ostringstream result;
153     result << *things.begin();
154     for (auto it = std::next(things.begin()); it != things.end(); ++it) {
155       result << separator << *it;
156     }
157     return result.str();
158   }
159 }
160 
161 // These cases were measured either to be used during build by more than one binary, or during
162 // runtime as a significant portion of total calls.
163 // Instantiate them in strings.cpp to aid compile time and binary size.
164 extern template std::string Join(std::vector<std::string>&, char);
165 extern template std::string Join(std::vector<std::string>&, const char*);
166 extern template std::string Join(std::vector<std::string>&&, const char*);
167 extern template std::string Join(const std::vector<std::string>&, char);
168 extern template std::string Join(const std::vector<std::string>&, const char*);
169 extern template std::string Join(const std::vector<std::string>&&, const char*);
170 extern template std::string Join(std::set<std::string>&, const char*);
171 extern template std::string Join(const std::set<std::string>&, char);
172 extern template std::string Join(const std::set<std::string>&, const char*);
173 extern template std::string Join(const std::unordered_set<std::string>&, const char*);
174 
175 // Tests whether 's' starts with 'prefix'.
176 bool StartsWith(std::string_view s, std::string_view prefix);
177 bool StartsWith(std::string_view s, char prefix);
178 bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix);
179 
180 // Tests whether 's' ends with 'suffix'.
181 bool EndsWith(std::string_view s, std::string_view suffix);
182 bool EndsWith(std::string_view s, char suffix);
183 bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix);
184 
185 // Tests whether 'lhs' equals 'rhs', ignoring case.
186 bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs);
187 
188 // Removes `prefix` from the start of the given string and returns true (if
189 // it was present), false otherwise.
ConsumePrefix(std::string_view * s,std::string_view prefix)190 inline bool ConsumePrefix(std::string_view* s, std::string_view prefix) {
191   if (!StartsWith(*s, prefix)) return false;
192   s->remove_prefix(prefix.size());
193   return true;
194 }
195 
196 // Removes `suffix` from the end of the given string and returns true (if
197 // it was present), false otherwise.
ConsumeSuffix(std::string_view * s,std::string_view suffix)198 inline bool ConsumeSuffix(std::string_view* s, std::string_view suffix) {
199   if (!EndsWith(*s, suffix)) return false;
200   s->remove_suffix(suffix.size());
201   return true;
202 }
203 
204 // Replaces `from` with `to` in `s`, once if `all == false`, or as many times as
205 // there are matches if `all == true`.
206 [[nodiscard]] std::string StringReplace(std::string_view s, std::string_view from,
207                                         std::string_view to, bool all);
208 
209 // Converts an errno number to its error message string.
210 std::string ErrnoNumberAsString(int errnum);
211 
212 }  // namespace base
213 }  // namespace android
214