xref: /aosp_15_r20/system/libbase/strings.cpp (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 #include "android-base/strings.h"
18 
19 #include "android-base/stringprintf.h"
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include <string>
25 #include <vector>
26 
27 // Wraps the posix version of strerror_r to make it available in translation units
28 // that define _GNU_SOURCE.
29 extern "C" int posix_strerror_r(int errnum, char* buf, size_t buflen);
30 
31 namespace android {
32 namespace base {
33 
34 #define CHECK_NE(a, b) \
35   if ((a) == (b)) abort();
36 
Split(const std::string & s,const std::string & delimiters)37 std::vector<std::string> Split(const std::string& s,
38                                const std::string& delimiters) {
39   CHECK_NE(delimiters.size(), 0U);
40 
41   std::vector<std::string> result;
42 
43   size_t base = 0;
44   size_t found;
45   while (true) {
46     found = s.find_first_of(delimiters, base);
47     result.push_back(s.substr(base, found - base));
48     if (found == s.npos) break;
49     base = found + 1;
50   }
51 
52   return result;
53 }
54 
Tokenize(const std::string & s,const std::string & delimiters)55 std::vector<std::string> Tokenize(const std::string& s, const std::string& delimiters) {
56   CHECK_NE(delimiters.size(), 0U);
57 
58   std::vector<std::string> result;
59   size_t end = 0;
60 
61   while (true) {
62     size_t base = s.find_first_not_of(delimiters, end);
63     if (base == s.npos) {
64       break;
65     }
66     end = s.find_first_of(delimiters, base);
67     result.push_back(s.substr(base, end - base));
68   }
69   return result;
70 }
71 
72 template std::string Trim(const char*&);
73 template std::string Trim(const char*&&);
74 template std::string Trim(const std::string&);
75 template std::string Trim(const std::string&&);
76 template std::string Trim(std::string_view&);
77 template std::string Trim(std::string_view&&);
78 
79 // These cases were measured either to be used during build by more than one binary, or during
80 // runtime as a significant portion of total calls.
81 // Instantiate them to aid compile time and binary size.
82 template std::string Join(std::vector<std::string>&, char);
83 template std::string Join(std::vector<std::string>&, const char*);
84 template std::string Join(std::vector<std::string>&&, const char*);
85 template std::string Join(const std::vector<std::string>&, char);
86 template std::string Join(const std::vector<std::string>&, const char*);
87 template std::string Join(const std::vector<std::string>&&, const char*);
88 template std::string Join(std::set<std::string>&, const char*);
89 template std::string Join(const std::set<std::string>&, char);
90 template std::string Join(const std::set<std::string>&, const char*);
91 template std::string Join(const std::unordered_set<std::string>&, const char*);
92 
93 
StartsWith(std::string_view s,std::string_view prefix)94 bool StartsWith(std::string_view s, std::string_view prefix) {
95   return s.starts_with(prefix);
96 }
97 
StartsWith(std::string_view s,char prefix)98 bool StartsWith(std::string_view s, char prefix) {
99   return s.starts_with(prefix);
100 }
101 
StartsWithIgnoreCase(std::string_view s,std::string_view prefix)102 bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix) {
103   return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0;
104 }
105 
EndsWith(std::string_view s,std::string_view suffix)106 bool EndsWith(std::string_view s, std::string_view suffix) {
107   return s.ends_with(suffix);
108 }
109 
EndsWith(std::string_view s,char suffix)110 bool EndsWith(std::string_view s, char suffix) {
111   return s.ends_with(suffix);
112 }
113 
EndsWithIgnoreCase(std::string_view s,std::string_view suffix)114 bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix) {
115   return s.size() >= suffix.size() &&
116          strncasecmp(s.data() + (s.size() - suffix.size()), suffix.data(), suffix.size()) == 0;
117 }
118 
EqualsIgnoreCase(std::string_view lhs,std::string_view rhs)119 bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs) {
120   return lhs.size() == rhs.size() && strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0;
121 }
122 
StringReplace(std::string_view s,std::string_view from,std::string_view to,bool all)123 std::string StringReplace(std::string_view s, std::string_view from, std::string_view to,
124                           bool all) {
125   if (from.empty()) return std::string(s);
126 
127   std::string result;
128   std::string_view::size_type start_pos = 0;
129   do {
130     std::string_view::size_type pos = s.find(from, start_pos);
131     if (pos == std::string_view::npos) break;
132 
133     result.append(s.data() + start_pos, pos - start_pos);
134     result.append(to.data(), to.size());
135 
136     start_pos = pos + from.size();
137   } while (all);
138   result.append(s.data() + start_pos, s.size() - start_pos);
139   return result;
140 }
141 
ErrnoNumberAsString(int errnum)142 std::string ErrnoNumberAsString(int errnum) {
143   char buf[100];
144   buf[0] = '\0';
145   int strerror_err = posix_strerror_r(errnum, buf, sizeof(buf));
146   if (strerror_err < 0) {
147     return StringPrintf("Failed to convert errno %d to string: %d", errnum, strerror_err);
148   }
149   return buf;
150 }
151 
152 }  // namespace base
153 }  // namespace android
154