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; |
| 39 | do { |
| 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)); |
Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 42 | base = found + 1; |
| 43 | } while (found != s.npos); |
| 44 | |
Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 45 | return result; |
Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | std::string Trim(const std::string& s) { |
| 49 | std::string result; |
| 50 | |
| 51 | if (s.size() == 0) { |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | size_t start_index = 0; |
| 56 | size_t end_index = s.size() - 1; |
| 57 | |
| 58 | // Skip initial whitespace. |
| 59 | while (start_index < s.size()) { |
| 60 | if (!isspace(s[start_index])) { |
| 61 | break; |
| 62 | } |
| 63 | start_index++; |
| 64 | } |
| 65 | |
| 66 | // Skip terminating whitespace. |
| 67 | while (end_index >= start_index) { |
| 68 | if (!isspace(s[end_index])) { |
| 69 | break; |
| 70 | } |
| 71 | end_index--; |
| 72 | } |
| 73 | |
| 74 | // All spaces, no beef. |
| 75 | if (end_index < start_index) { |
| 76 | return ""; |
| 77 | } |
| 78 | // Start_index is the first non-space, end_index is the last one. |
| 79 | return s.substr(start_index, end_index - start_index + 1); |
| 80 | } |
| 81 | |
Dan Albert | e0da8a1 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 82 | // These cases are probably the norm, so we mark them extern in the header to |
| 83 | // aid compile time and binary size. |
| 84 | template std::string Join(const std::vector<std::string>&, char); |
| 85 | template std::string Join(const std::vector<const char*>&, char); |
Casey Dahlin | 5345f1d | 2015-10-30 18:54:38 -0700 | [diff] [blame] | 86 | template std::string Join(const std::vector<std::string>&, const std::string&); |
| 87 | template std::string Join(const std::vector<const char*>&, const std::string&); |
Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 88 | |
| 89 | bool StartsWith(const std::string& s, const char* prefix) { |
| 90 | return s.compare(0, strlen(prefix), prefix) == 0; |
| 91 | } |
| 92 | |
| 93 | bool EndsWith(const std::string& s, const char* suffix) { |
| 94 | size_t suffix_length = strlen(suffix); |
| 95 | size_t string_length = s.size(); |
| 96 | if (suffix_length > string_length) { |
| 97 | return false; |
| 98 | } |
| 99 | size_t offset = string_length - suffix_length; |
| 100 | return s.compare(offset, suffix_length, suffix) == 0; |
| 101 | } |
| 102 | |
| 103 | } // namespace base |
| 104 | } // namespace android |