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
Trim(const std::string & s)72 std::string Trim(const std::string& s) {
73 std::string result;
74
75 if (s.size() == 0) {
76 return result;
77 }
78
79 size_t start_index = 0;
80 size_t end_index = s.size() - 1;
81
82 // Skip initial whitespace.
83 while (start_index < s.size()) {
84 if (!isspace(s[start_index])) {
85 break;
86 }
87 start_index++;
88 }
89
90 // Skip terminating whitespace.
91 while (end_index >= start_index) {
92 if (!isspace(s[end_index])) {
93 break;
94 }
95 end_index--;
96 }
97
98 // All spaces, no beef.
99 if (end_index < start_index) {
100 return "";
101 }
102 // Start_index is the first non-space, end_index is the last one.
103 return s.substr(start_index, end_index - start_index + 1);
104 }
105
106 // These cases are probably the norm, so we mark them extern in the header to
107 // aid compile time and binary size.
108 template std::string Join(const std::vector<std::string>&, char);
109 template std::string Join(const std::vector<const char*>&, char);
110 template std::string Join(const std::vector<std::string>&, const std::string&);
111 template std::string Join(const std::vector<const char*>&, const std::string&);
112
StartsWith(std::string_view s,std::string_view prefix)113 bool StartsWith(std::string_view s, std::string_view prefix) {
114 return s.substr(0, prefix.size()) == prefix;
115 }
116
StartsWith(std::string_view s,char prefix)117 bool StartsWith(std::string_view s, char prefix) {
118 return !s.empty() && s.front() == prefix;
119 }
120
StartsWithIgnoreCase(std::string_view s,std::string_view prefix)121 bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix) {
122 return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0;
123 }
124
EndsWith(std::string_view s,std::string_view suffix)125 bool EndsWith(std::string_view s, std::string_view suffix) {
126 return s.size() >= suffix.size() && s.substr(s.size() - suffix.size(), suffix.size()) == suffix;
127 }
128
EndsWith(std::string_view s,char suffix)129 bool EndsWith(std::string_view s, char suffix) {
130 return !s.empty() && s.back() == suffix;
131 }
132
EndsWithIgnoreCase(std::string_view s,std::string_view suffix)133 bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix) {
134 return s.size() >= suffix.size() &&
135 strncasecmp(s.data() + (s.size() - suffix.size()), suffix.data(), suffix.size()) == 0;
136 }
137
EqualsIgnoreCase(std::string_view lhs,std::string_view rhs)138 bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs) {
139 return lhs.size() == rhs.size() && strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0;
140 }
141
StringReplace(std::string_view s,std::string_view from,std::string_view to,bool all)142 std::string StringReplace(std::string_view s, std::string_view from, std::string_view to,
143 bool all) {
144 if (from.empty()) return std::string(s);
145
146 std::string result;
147 std::string_view::size_type start_pos = 0;
148 do {
149 std::string_view::size_type pos = s.find(from, start_pos);
150 if (pos == std::string_view::npos) break;
151
152 result.append(s.data() + start_pos, pos - start_pos);
153 result.append(to.data(), to.size());
154
155 start_pos = pos + from.size();
156 } while (all);
157 result.append(s.data() + start_pos, s.size() - start_pos);
158 return result;
159 }
160
ErrnoNumberAsString(int errnum)161 std::string ErrnoNumberAsString(int errnum) {
162 char buf[100];
163 buf[0] = '\0';
164 int strerror_err = posix_strerror_r(errnum, buf, sizeof(buf));
165 if (strerror_err < 0) {
166 return StringPrintf("Failed to convert errno %d to string: %d", errnum, strerror_err);
167 }
168 return buf;
169 }
170
171 } // namespace base
172 } // namespace android
173