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