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 <sstream>
20 #include <string>
21 #include <string_view>
22 #include <vector>
23
24 namespace android {
25 namespace 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.
32 std::vector<std::string> Split(const std::string& s,
33 const std::string& delimiters);
34
35 // Trims whitespace off both ends of the given string.
36 std::string Trim(const std::string& s);
37
38 // Joins a container of things into a single string, using the given separator.
39 template <typename ContainerT, typename SeparatorT>
Join(const ContainerT & things,SeparatorT separator)40 std::string Join(const ContainerT& things, SeparatorT separator) {
41 if (things.empty()) {
42 return "";
43 }
44
45 std::ostringstream result;
46 result << *things.begin();
47 for (auto it = std::next(things.begin()); it != things.end(); ++it) {
48 result << separator << *it;
49 }
50 return result.str();
51 }
52
53 // We instantiate the common cases in strings.cpp.
54 extern template std::string Join(const std::vector<std::string>&, char);
55 extern template std::string Join(const std::vector<const char*>&, char);
56 extern template std::string Join(const std::vector<std::string>&, const std::string&);
57 extern template std::string Join(const std::vector<const char*>&, const std::string&);
58
59 // Tests whether 's' starts with 'prefix'.
60 bool StartsWith(std::string_view s, std::string_view prefix);
61 bool StartsWith(std::string_view s, char prefix);
62 bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix);
63
64 // Tests whether 's' ends with 'suffix'.
65 bool EndsWith(std::string_view s, std::string_view suffix);
66 bool EndsWith(std::string_view s, char suffix);
67 bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix);
68
69 // Tests whether 'lhs' equals 'rhs', ignoring case.
70 bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs);
71
72 // Removes `prefix` from the start of the given string and returns true (if
73 // it was present), false otherwise.
ConsumePrefix(std::string_view * s,std::string_view prefix)74 inline bool ConsumePrefix(std::string_view* s, std::string_view prefix) {
75 if (!StartsWith(*s, prefix)) return false;
76 s->remove_prefix(prefix.size());
77 return true;
78 }
79
80 // Removes `suffix` from the end of the given string and returns true (if
81 // it was present), false otherwise.
ConsumeSuffix(std::string_view * s,std::string_view suffix)82 inline bool ConsumeSuffix(std::string_view* s, std::string_view suffix) {
83 if (!EndsWith(*s, suffix)) return false;
84 s->remove_suffix(suffix.size());
85 return true;
86 }
87
88 // Replaces `from` with `to` in `s`, once if `all == false`, or as many times as
89 // there are matches if `all == true`.
90 [[nodiscard]] std::string StringReplace(std::string_view s, std::string_view from,
91 std::string_view to, bool all);
92
93 } // namespace base
94 } // namespace android
95