| Dan Albert | 0f1e544 | 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 | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/strings.h" | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 18 |  | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 19 | #include <stdlib.h> | 
| Dan Albert | 4b3f533 | 2015-03-26 23:23:32 -0700 | [diff] [blame] | 20 | #include <string.h> | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 21 |  | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 22 | #include <string> | 
|  | 23 | #include <vector> | 
|  | 24 |  | 
|  | 25 | namespace android { | 
|  | 26 | namespace base { | 
|  | 27 |  | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 28 | #define CHECK_NE(a, b) \ | 
|  | 29 | if ((a) == (b)) abort(); | 
|  | 30 |  | 
|  | 31 | std::vector<std::string> Split(const std::string& s, | 
|  | 32 | const std::string& delimiters) { | 
|  | 33 | CHECK_NE(delimiters.size(), 0U); | 
|  | 34 |  | 
| Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 35 | std::vector<std::string> result; | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | size_t base = 0; | 
|  | 38 | size_t found; | 
| Elliott Hughes | bf0dd7c | 2017-02-07 15:30:24 -0800 | [diff] [blame] | 39 | while (true) { | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 40 | found = s.find_first_of(delimiters, base); | 
| Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 41 | result.push_back(s.substr(base, found - base)); | 
| Elliott Hughes | bf0dd7c | 2017-02-07 15:30:24 -0800 | [diff] [blame] | 42 | if (found == s.npos) break; | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 43 | base = found + 1; | 
| Elliott Hughes | bf0dd7c | 2017-02-07 15:30:24 -0800 | [diff] [blame] | 44 | } | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 45 |  | 
| Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 46 | return result; | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
|  | 49 | std::string Trim(const std::string& s) { | 
|  | 50 | std::string result; | 
|  | 51 |  | 
|  | 52 | if (s.size() == 0) { | 
|  | 53 | return result; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | size_t start_index = 0; | 
|  | 57 | size_t end_index = s.size() - 1; | 
|  | 58 |  | 
|  | 59 | // Skip initial whitespace. | 
|  | 60 | while (start_index < s.size()) { | 
|  | 61 | if (!isspace(s[start_index])) { | 
|  | 62 | break; | 
|  | 63 | } | 
|  | 64 | start_index++; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | // Skip terminating whitespace. | 
|  | 68 | while (end_index >= start_index) { | 
|  | 69 | if (!isspace(s[end_index])) { | 
|  | 70 | break; | 
|  | 71 | } | 
|  | 72 | end_index--; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | // All spaces, no beef. | 
|  | 76 | if (end_index < start_index) { | 
|  | 77 | return ""; | 
|  | 78 | } | 
|  | 79 | // Start_index is the first non-space, end_index is the last one. | 
|  | 80 | return s.substr(start_index, end_index - start_index + 1); | 
|  | 81 | } | 
|  | 82 |  | 
| Dan Albert | e0da8a1 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 83 | // These cases are probably the norm, so we mark them extern in the header to | 
|  | 84 | // aid compile time and binary size. | 
|  | 85 | template std::string Join(const std::vector<std::string>&, char); | 
|  | 86 | template std::string Join(const std::vector<const char*>&, char); | 
| Casey Dahlin | 5345f1d | 2015-10-30 18:54:38 -0700 | [diff] [blame] | 87 | template std::string Join(const std::vector<std::string>&, const std::string&); | 
|  | 88 | template std::string Join(const std::vector<const char*>&, const std::string&); | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 89 |  | 
| Yurii Zubrytskyi | 59d876c | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 90 | bool StartsWith(std::string_view s, std::string_view prefix) { | 
|  | 91 | return s.substr(0, prefix.size()) == prefix; | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
| Yurii Zubrytskyi | 59d876c | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 94 | bool StartsWith(std::string_view s, char prefix) { | 
|  | 95 | return !s.empty() && s.front() == prefix; | 
| Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Yurii Zubrytskyi | 59d876c | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 98 | bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix) { | 
|  | 99 | return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0; | 
| Yabin Cui | a656b98 | 2018-10-30 15:49:53 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Yurii Zubrytskyi | 59d876c | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 102 | bool EndsWith(std::string_view s, std::string_view suffix) { | 
|  | 103 | return s.size() >= suffix.size() && s.substr(s.size() - suffix.size(), suffix.size()) == suffix; | 
| Elliott Hughes | 4293749 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Yurii Zubrytskyi | 59d876c | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 106 | bool EndsWith(std::string_view s, char suffix) { | 
|  | 107 | return !s.empty() && s.back() == suffix; | 
| Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
| Yurii Zubrytskyi | 59d876c | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 110 | bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix) { | 
|  | 111 | return s.size() >= suffix.size() && | 
|  | 112 | strncasecmp(s.data() + (s.size() - suffix.size()), suffix.data(), suffix.size()) == 0; | 
| Elliott Hughes | 4293749 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Yurii Zubrytskyi | 59d876c | 2019-03-19 17:03:42 -0700 | [diff] [blame] | 115 | bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs) { | 
|  | 116 | return lhs.size() == rhs.size() && strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0; | 
| Elliott Hughes | 2140782 | 2017-01-13 18:51:32 -0800 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Elliott Hughes | 908e0df | 2019-11-18 16:01:21 -0800 | [diff] [blame] | 119 | std::string StringReplace(std::string_view s, std::string_view from, std::string_view to, | 
|  | 120 | bool all) { | 
|  | 121 | if (from.empty()) return std::string(s); | 
|  | 122 |  | 
|  | 123 | std::string result; | 
|  | 124 | std::string_view::size_type start_pos = 0; | 
|  | 125 | do { | 
|  | 126 | std::string_view::size_type pos = s.find(from, start_pos); | 
|  | 127 | if (pos == std::string_view::npos) break; | 
|  | 128 |  | 
|  | 129 | result.append(s.data() + start_pos, pos - start_pos); | 
|  | 130 | result.append(to.data(), to.size()); | 
|  | 131 |  | 
|  | 132 | start_pos = pos + from.size(); | 
|  | 133 | } while (all); | 
|  | 134 | result.append(s.data() + start_pos, s.size() - start_pos); | 
|  | 135 | return result; | 
|  | 136 | } | 
|  | 137 |  | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 138 | }  // namespace base | 
|  | 139 | }  // namespace android |