• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Iterates over a string's parts using |splitBy| as a delimiter.
39 // |splitBy| must be a nonempty string well, or it's a no-op.
40 // Otherwise, |func| is called on each of the splits, excluding the
41 // characters that are part of |splitBy|.  If two |splitBy|'s occur in a row,
42 // |func| will be called on a StringView("") in between. See
43 // StringUtils_unittest.cpp for the full story.
44 template <class String>
split(String str,String splitBy,std::function<void (const String &)> func)45 void split(String str, String splitBy, std::function<void(const String&)> func) {
46     if (splitBy.empty()) return;
47 
48     size_t splitSize = splitBy.size();
49     size_t begin = 0;
50     size_t end = str.find(splitBy);
51 
52     while (true) {
53         func(str.substr(begin, end - begin));
54         if (end == std::string::npos) return;
55         begin = end + splitSize;
56         end = str.find(splitBy, begin);
57     }
58 }
59 
60 // Splits a string into a vector of strings.
61 //
62 // The string is split at each occurrence of a character in delimiters.
63 //
64 // The empty string is not a valid delimiter list.
65 std::vector<std::string> Split(const std::string& s,
66                                const std::string& delimiters);
67 
68 // Trims whitespace off both ends of the given string.
69 std::string Trim(const std::string& s);
70 
71 // Joins a container of things into a single string, using the given separator.
72 template <typename ContainerT, typename SeparatorT>
Join(const ContainerT & things,SeparatorT separator)73 std::string Join(const ContainerT& things, SeparatorT separator) {
74     if (things.empty()) {
75         return "";
76     }
77 
78     std::ostringstream result;
79     result << *things.begin();
80     for (auto it = std::next(things.begin()); it != things.end(); ++it) {
81         result << separator << *it;
82     }
83     return result.str();
84 }
85 
86 // We instantiate the common cases in strings.cpp.
87 extern template std::string Join(const std::vector<std::string>&, char);
88 extern template std::string Join(const std::vector<const char*>&, char);
89 extern template std::string Join(const std::vector<std::string>&,
90                                  const std::string&);
91 extern template std::string Join(const std::vector<const char*>&,
92                                  const std::string&);
93 
94 // Tests whether 's' starts with 'prefix'.
95 bool StartsWith(std::string_view s, std::string_view prefix);
96 bool StartsWith(std::string_view s, char prefix);
97 bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix);
98 
99 // Tests whether 's' ends with 'suffix'.
100 bool EndsWith(std::string_view s, std::string_view suffix);
101 bool EndsWith(std::string_view s, char suffix);
102 bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix);
103 
104 // Tests whether 'lhs' equals 'rhs', ignoring case.
105 bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs);
106 
107 // Removes `prefix` from the start of the given string and returns true (if
108 // it was present), false otherwise.
ConsumePrefix(std::string_view * s,std::string_view prefix)109 inline bool ConsumePrefix(std::string_view* s, std::string_view prefix) {
110     if (!StartsWith(*s, prefix))
111         return false;
112     s->remove_prefix(prefix.size());
113     return true;
114 }
115 
116 // Removes `suffix` from the end of the given string and returns true (if
117 // it was present), false otherwise.
ConsumeSuffix(std::string_view * s,std::string_view suffix)118 inline bool ConsumeSuffix(std::string_view* s, std::string_view suffix) {
119     if (!EndsWith(*s, suffix))
120         return false;
121     s->remove_suffix(suffix.size());
122     return true;
123 }
124 
125 // Replaces `from` with `to` in `s`, once if `all == false`, or as many times as
126 // there are matches if `all == true`.
127 [[nodiscard]] std::string StringReplace(std::string_view s,
128                                         std::string_view from,
129                                         std::string_view to,
130                                         bool all);
131 
132 }  // namespace base
133 }  // namespace android
134