Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/strings.h" |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 18 | |
Colin Cross | 4bea655 | 2021-12-21 13:54:40 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
| 20 | |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Dan Albert | 852d2e7 | 2015-03-26 23:23:32 -0700 | [diff] [blame] | 22 | #include <string.h> |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 23 | |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Colin Cross | 4bea655 | 2021-12-21 13:54:40 -0800 | [diff] [blame] | 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 | |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace base { |
| 33 | |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 34 | #define CHECK_NE(a, b) \ |
| 35 | if ((a) == (b)) abort(); |
| 36 | |
| 37 | std::vector<std::string> Split(const std::string& s, |
| 38 | const std::string& delimiters) { |
| 39 | CHECK_NE(delimiters.size(), 0U); |
| 40 | |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 41 | std::vector<std::string> result; |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 42 | |
| 43 | size_t base = 0; |
| 44 | size_t found; |
Elliott Hughes | b226479 | 2017-02-07 15:30:24 -0800 | [diff] [blame] | 45 | while (true) { |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 46 | found = s.find_first_of(delimiters, base); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 47 | result.push_back(s.substr(base, found - base)); |
Elliott Hughes | b226479 | 2017-02-07 15:30:24 -0800 | [diff] [blame] | 48 | if (found == s.npos) break; |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 49 | base = found + 1; |
Elliott Hughes | b226479 | 2017-02-07 15:30:24 -0800 | [diff] [blame] | 50 | } |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 52 | return result; |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Yi-Yo Chiang | 5ad131f | 2021-11-09 19:48:09 +0800 | [diff] [blame] | 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 | |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 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 | |
Dan Albert | 0d20cc9 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 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); |
Casey Dahlin | f8557e7 | 2015-10-30 18:54:38 -0700 | [diff] [blame] | 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&); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 112 | |
Yurii Zubrytskyi | ef973ce | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 113 | bool StartsWith(std::string_view s, std::string_view prefix) { |
| 114 | return s.substr(0, prefix.size()) == prefix; |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Yurii Zubrytskyi | ef973ce | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 117 | bool StartsWith(std::string_view s, char prefix) { |
| 118 | return !s.empty() && s.front() == prefix; |
Elliott Hughes | 9955af2 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Yurii Zubrytskyi | ef973ce | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 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; |
Yabin Cui | a87b388 | 2018-10-30 15:49:53 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Yurii Zubrytskyi | ef973ce | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 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; |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Yurii Zubrytskyi | ef973ce | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 129 | bool EndsWith(std::string_view s, char suffix) { |
| 130 | return !s.empty() && s.back() == suffix; |
Elliott Hughes | 9955af2 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Yurii Zubrytskyi | ef973ce | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 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; |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Yurii Zubrytskyi | ef973ce | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 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; |
Elliott Hughes | 7872ff3 | 2017-01-13 18:51:32 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Elliott Hughes | 80ac17b | 2019-11-18 16:01:21 -0800 | [diff] [blame] | 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 | |
Colin Cross | 4bea655 | 2021-12-21 13:54:40 -0800 | [diff] [blame] | 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 | |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 171 | } // namespace base |
| 172 | } // namespace android |