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