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 [[deprecated("Retained only for binary compatibility (symbol name)")]]
Trim(const std::string & s)73 std::string Trim(const std::string& s) {
74 return Trim(std::string_view(s));
75 }
76
77 template std::string Trim(const char*&);
78 template std::string Trim(const char*&&);
79 template std::string Trim(const std::string&);
80 template std::string Trim(const std::string&&);
81 template std::string Trim(std::string_view&);
82 template std::string Trim(std::string_view&&);
83
84 // These cases are probably the norm, so we mark them extern in the header to
85 // aid compile time and binary size.
86 template std::string Join(const std::vector<std::string>&, char);
87 template std::string Join(const std::vector<const char*>&, char);
88 template std::string Join(const std::vector<std::string>&, const std::string&);
89 template std::string Join(const std::vector<const char*>&, const std::string&);
90
StartsWith(std::string_view s,std::string_view prefix)91 bool StartsWith(std::string_view s, std::string_view prefix) {
92 return s.substr(0, prefix.size()) == prefix;
93 }
94
StartsWith(std::string_view s,char prefix)95 bool StartsWith(std::string_view s, char prefix) {
96 return !s.empty() && s.front() == prefix;
97 }
98
StartsWithIgnoreCase(std::string_view s,std::string_view prefix)99 bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix) {
100 return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0;
101 }
102
EndsWith(std::string_view s,std::string_view suffix)103 bool EndsWith(std::string_view s, std::string_view suffix) {
104 return s.size() >= suffix.size() && s.substr(s.size() - suffix.size(), suffix.size()) == suffix;
105 }
106
EndsWith(std::string_view s,char suffix)107 bool EndsWith(std::string_view s, char suffix) {
108 return !s.empty() && s.back() == suffix;
109 }
110
EndsWithIgnoreCase(std::string_view s,std::string_view suffix)111 bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix) {
112 return s.size() >= suffix.size() &&
113 strncasecmp(s.data() + (s.size() - suffix.size()), suffix.data(), suffix.size()) == 0;
114 }
115
EqualsIgnoreCase(std::string_view lhs,std::string_view rhs)116 bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs) {
117 return lhs.size() == rhs.size() && strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0;
118 }
119
StringReplace(std::string_view s,std::string_view from,std::string_view to,bool all)120 std::string StringReplace(std::string_view s, std::string_view from, std::string_view to,
121 bool all) {
122 if (from.empty()) return std::string(s);
123
124 std::string result;
125 std::string_view::size_type start_pos = 0;
126 do {
127 std::string_view::size_type pos = s.find(from, start_pos);
128 if (pos == std::string_view::npos) break;
129
130 result.append(s.data() + start_pos, pos - start_pos);
131 result.append(to.data(), to.size());
132
133 start_pos = pos + from.size();
134 } while (all);
135 result.append(s.data() + start_pos, s.size() - start_pos);
136 return result;
137 }
138
ErrnoNumberAsString(int errnum)139 std::string ErrnoNumberAsString(int errnum) {
140 char buf[100];
141 buf[0] = '\0';
142 int strerror_err = posix_strerror_r(errnum, buf, sizeof(buf));
143 if (strerror_err < 0) {
144 return StringPrintf("Failed to convert errno %d to string: %d", errnum, strerror_err);
145 }
146 return buf;
147 }
148
149 } // namespace base
150 } // namespace android
151