blob: deb6e280ff9593b43b6d9e6738ffd63fbc6aefc2 [file] [log] [blame]
Dan Albert00716d72015-03-13 22:57:40 -07001/*
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
Elliott Hughesb6351622015-12-04 22:00:26 -080017#include "android-base/strings.h"
Dan Albert00716d72015-03-13 22:57:40 -070018
Colin Cross4bea6552021-12-21 13:54:40 -080019#include "android-base/stringprintf.h"
20
Dan Albert0d716d02015-03-19 13:24:26 -070021#include <stdlib.h>
Dan Albert852d2e72015-03-26 23:23:32 -070022#include <string.h>
Dan Albert0d716d02015-03-19 13:24:26 -070023
Dan Albert00716d72015-03-13 22:57:40 -070024#include <string>
25#include <vector>
26
Colin Cross4bea6552021-12-21 13:54:40 -080027// Wraps the posix version of strerror_r to make it available in translation units
28// that define _GNU_SOURCE.
29extern "C" int posix_strerror_r(int errnum, char* buf, size_t buflen);
30
Dan Albert00716d72015-03-13 22:57:40 -070031namespace android {
32namespace base {
33
Dan Albert0d716d02015-03-19 13:24:26 -070034#define CHECK_NE(a, b) \
35 if ((a) == (b)) abort();
36
37std::vector<std::string> Split(const std::string& s,
38 const std::string& delimiters) {
39 CHECK_NE(delimiters.size(), 0U);
40
Elliott Hughes9964efc2015-04-24 23:02:00 -070041 std::vector<std::string> result;
Dan Albert0d716d02015-03-19 13:24:26 -070042
43 size_t base = 0;
44 size_t found;
Elliott Hughesb2264792017-02-07 15:30:24 -080045 while (true) {
Dan Albert0d716d02015-03-19 13:24:26 -070046 found = s.find_first_of(delimiters, base);
Elliott Hughes9964efc2015-04-24 23:02:00 -070047 result.push_back(s.substr(base, found - base));
Elliott Hughesb2264792017-02-07 15:30:24 -080048 if (found == s.npos) break;
Dan Albert0d716d02015-03-19 13:24:26 -070049 base = found + 1;
Elliott Hughesb2264792017-02-07 15:30:24 -080050 }
Dan Albert0d716d02015-03-19 13:24:26 -070051
Elliott Hughes9964efc2015-04-24 23:02:00 -070052 return result;
Dan Albert00716d72015-03-13 22:57:40 -070053}
54
Yi-Yo Chiang5ad131f2021-11-09 19:48:09 +080055std::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
Dan Albert00716d72015-03-13 22:57:40 -070072std::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
Dan Albert0d20cc92015-05-21 18:37:36 -0700106// These cases are probably the norm, so we mark them extern in the header to
107// aid compile time and binary size.
108template std::string Join(const std::vector<std::string>&, char);
109template std::string Join(const std::vector<const char*>&, char);
Casey Dahlinf8557e72015-10-30 18:54:38 -0700110template std::string Join(const std::vector<std::string>&, const std::string&);
111template std::string Join(const std::vector<const char*>&, const std::string&);
Dan Albert00716d72015-03-13 22:57:40 -0700112
Yurii Zubrytskyief973ce2019-03-19 17:03:42 -0700113bool StartsWith(std::string_view s, std::string_view prefix) {
114 return s.substr(0, prefix.size()) == prefix;
Dan Albert00716d72015-03-13 22:57:40 -0700115}
116
Yurii Zubrytskyief973ce2019-03-19 17:03:42 -0700117bool StartsWith(std::string_view s, char prefix) {
118 return !s.empty() && s.front() == prefix;
Elliott Hughes9955af22017-12-20 09:41:00 -0800119}
120
Yurii Zubrytskyief973ce2019-03-19 17:03:42 -0700121bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix) {
122 return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0;
Yabin Cuia87b3882018-10-30 15:49:53 -0700123}
124
Yurii Zubrytskyief973ce2019-03-19 17:03:42 -0700125bool EndsWith(std::string_view s, std::string_view suffix) {
126 return s.size() >= suffix.size() && s.substr(s.size() - suffix.size(), suffix.size()) == suffix;
Elliott Hughes85484272016-10-25 14:56:04 -0700127}
128
Yurii Zubrytskyief973ce2019-03-19 17:03:42 -0700129bool EndsWith(std::string_view s, char suffix) {
130 return !s.empty() && s.back() == suffix;
Elliott Hughes9955af22017-12-20 09:41:00 -0800131}
132
Yurii Zubrytskyief973ce2019-03-19 17:03:42 -0700133bool 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;
Elliott Hughes85484272016-10-25 14:56:04 -0700136}
137
Yurii Zubrytskyief973ce2019-03-19 17:03:42 -0700138bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs) {
139 return lhs.size() == rhs.size() && strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0;
Elliott Hughes7872ff32017-01-13 18:51:32 -0800140}
141
Elliott Hughes80ac17b2019-11-18 16:01:21 -0800142std::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
Colin Cross4bea6552021-12-21 13:54:40 -0800161std::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
Dan Albert00716d72015-03-13 22:57:40 -0700171} // namespace base
172} // namespace android