• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Splits a string into a vector of string tokens.
36 //
37 // The string is split at each occurrence of a character in delimiters.
38 // Coalesce runs of delimiter bytes and ignore delimiter bytes at the start or
39 // end of string. In other words, return only nonempty string tokens.
40 // Use when you don't care about recovering the original string with Join().
41 //
42 // Example:
43 //   Tokenize(" foo  bar ", " ") => {"foo", "bar"}
44 //   Join(Tokenize("  foo  bar", " "), " ") => "foo bar"
45 //
46 // The empty string is not a valid delimiter list.
47 std::vector<std::string> Tokenize(const std::string& s, const std::string& delimiters);
48 
49 // Trims whitespace off both ends of the given string.
50 std::string Trim(const std::string& s);
51 
52 // Joins a container of things into a single string, using the given separator.
53 template <typename ContainerT, typename SeparatorT>
Join(const ContainerT & things,SeparatorT separator)54 std::string Join(const ContainerT& things, SeparatorT separator) {
55   if (things.empty()) {
56     return "";
57   }
58 
59   std::ostringstream result;
60   result << *things.begin();
61   for (auto it = std::next(things.begin()); it != things.end(); ++it) {
62     result << separator << *it;
63   }
64   return result.str();
65 }
66 
67 // We instantiate the common cases in strings.cpp.
68 extern template std::string Join(const std::vector<std::string>&, char);
69 extern template std::string Join(const std::vector<const char*>&, char);
70 extern template std::string Join(const std::vector<std::string>&, const std::string&);
71 extern template std::string Join(const std::vector<const char*>&, const std::string&);
72 
73 // Tests whether 's' starts with 'prefix'.
74 bool StartsWith(std::string_view s, std::string_view prefix);
75 bool StartsWith(std::string_view s, char prefix);
76 bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix);
77 
78 // Tests whether 's' ends with 'suffix'.
79 bool EndsWith(std::string_view s, std::string_view suffix);
80 bool EndsWith(std::string_view s, char suffix);
81 bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix);
82 
83 // Tests whether 'lhs' equals 'rhs', ignoring case.
84 bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs);
85 
86 // Removes `prefix` from the start of the given string and returns true (if
87 // it was present), false otherwise.
ConsumePrefix(std::string_view * s,std::string_view prefix)88 inline bool ConsumePrefix(std::string_view* s, std::string_view prefix) {
89   if (!StartsWith(*s, prefix)) return false;
90   s->remove_prefix(prefix.size());
91   return true;
92 }
93 
94 // Removes `suffix` from the end of the given string and returns true (if
95 // it was present), false otherwise.
ConsumeSuffix(std::string_view * s,std::string_view suffix)96 inline bool ConsumeSuffix(std::string_view* s, std::string_view suffix) {
97   if (!EndsWith(*s, suffix)) return false;
98   s->remove_suffix(suffix.size());
99   return true;
100 }
101 
102 // Replaces `from` with `to` in `s`, once if `all == false`, or as many times as
103 // there are matches if `all == true`.
104 [[nodiscard]] std::string StringReplace(std::string_view s, std::string_view from,
105                                         std::string_view to, bool all);
106 
107 // Converts an errno number to its error message string.
108 std::string ErrnoNumberAsString(int errnum);
109 
110 }  // namespace base
111 }  // namespace android
112