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 | e3c5a2a | 2018-06-26 17:17:41 -0700 | [diff] [blame] | 17 | #pragma once |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 18 | |
Dan Albert | 0d20cc9 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 19 | #include <sstream> |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace android { |
| 24 | namespace base { |
| 25 | |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 26 | // Splits a string into a vector of strings. |
| 27 | // |
Elliott Hughes | 658bd7b | 2015-04-17 17:08:16 -0700 | [diff] [blame] | 28 | // The string is split at each occurrence of a character in delimiters. |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 29 | // |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 30 | // The empty string is not a valid delimiter list. |
| 31 | std::vector<std::string> Split(const std::string& s, |
| 32 | const std::string& delimiters); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 33 | |
| 34 | // Trims whitespace off both ends of the given string. |
| 35 | std::string Trim(const std::string& s); |
| 36 | |
Dan Albert | 0d20cc9 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 37 | // Joins a container of things into a single string, using the given separator. |
Casey Dahlin | f8557e7 | 2015-10-30 18:54:38 -0700 | [diff] [blame] | 38 | template <typename ContainerT, typename SeparatorT> |
| 39 | std::string Join(const ContainerT& things, SeparatorT separator) { |
Dan Albert | 0d20cc9 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 40 | if (things.empty()) { |
| 41 | return ""; |
| 42 | } |
| 43 | |
| 44 | std::ostringstream result; |
| 45 | result << *things.begin(); |
| 46 | for (auto it = std::next(things.begin()); it != things.end(); ++it) { |
| 47 | result << separator << *it; |
| 48 | } |
| 49 | return result.str(); |
| 50 | } |
| 51 | |
| 52 | // We instantiate the common cases in strings.cpp. |
| 53 | extern template std::string Join(const std::vector<std::string>&, char); |
| 54 | extern template std::string Join(const std::vector<const char*>&, char); |
Casey Dahlin | f8557e7 | 2015-10-30 18:54:38 -0700 | [diff] [blame] | 55 | extern template std::string Join(const std::vector<std::string>&, const std::string&); |
| 56 | extern template std::string Join(const std::vector<const char*>&, const std::string&); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 57 | |
| 58 | // Tests whether 's' starts with 'prefix'. |
Elliott Hughes | 9955af2 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 59 | // TODO: string_view |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 60 | bool StartsWith(const std::string& s, const char* prefix); |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 61 | bool StartsWithIgnoreCase(const std::string& s, const char* prefix); |
Elliott Hughes | 9955af2 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 62 | bool StartsWith(const std::string& s, const std::string& prefix); |
| 63 | bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix); |
Yabin Cui | a87b388 | 2018-10-30 15:49:53 -0700 | [diff] [blame^] | 64 | bool StartsWith(const std::string& s, char prefix); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 65 | |
| 66 | // Tests whether 's' ends with 'suffix'. |
Elliott Hughes | 9955af2 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 67 | // TODO: string_view |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 68 | bool EndsWith(const std::string& s, const char* suffix); |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 69 | bool EndsWithIgnoreCase(const std::string& s, const char* suffix); |
Erik Kline | 226df75 | 2018-02-27 16:12:23 +0900 | [diff] [blame] | 70 | bool EndsWith(const std::string& s, const std::string& suffix); |
| 71 | bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix); |
Yabin Cui | a87b388 | 2018-10-30 15:49:53 -0700 | [diff] [blame^] | 72 | bool EndsWith(const std::string& s, char suffix); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | 7872ff3 | 2017-01-13 18:51:32 -0800 | [diff] [blame] | 74 | // Tests whether 'lhs' equals 'rhs', ignoring case. |
| 75 | bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs); |
| 76 | |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 77 | } // namespace base |
| 78 | } // namespace android |