blob: c11acb10d9db0ce48fd626bb2a99780a85b498bb [file] [log] [blame]
Dan Albert00716d72015-03-13 22:57:40 -07001/*
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 Hughesf5224432016-03-23 15:04:52 -070017#ifndef ANDROID_BASE_STRINGS_H
18#define ANDROID_BASE_STRINGS_H
Dan Albert00716d72015-03-13 22:57:40 -070019
Dan Albert0d20cc92015-05-21 18:37:36 -070020#include <sstream>
Dan Albert00716d72015-03-13 22:57:40 -070021#include <string>
22#include <vector>
23
24namespace android {
25namespace base {
26
Dan Albert0d716d02015-03-19 13:24:26 -070027// Splits a string into a vector of strings.
28//
Elliott Hughes658bd7b2015-04-17 17:08:16 -070029// The string is split at each occurrence of a character in delimiters.
Dan Albert0d716d02015-03-19 13:24:26 -070030//
Dan Albert0d716d02015-03-19 13:24:26 -070031// The empty string is not a valid delimiter list.
32std::vector<std::string> Split(const std::string& s,
33 const std::string& delimiters);
Dan Albert00716d72015-03-13 22:57:40 -070034
35// Trims whitespace off both ends of the given string.
36std::string Trim(const std::string& s);
37
Dan Albert0d20cc92015-05-21 18:37:36 -070038// Joins a container of things into a single string, using the given separator.
Casey Dahlinf8557e72015-10-30 18:54:38 -070039template <typename ContainerT, typename SeparatorT>
40std::string Join(const ContainerT& things, SeparatorT separator) {
Dan Albert0d20cc92015-05-21 18:37:36 -070041 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.
54extern template std::string Join(const std::vector<std::string>&, char);
55extern template std::string Join(const std::vector<const char*>&, char);
Casey Dahlinf8557e72015-10-30 18:54:38 -070056extern template std::string Join(const std::vector<std::string>&, const std::string&);
57extern template std::string Join(const std::vector<const char*>&, const std::string&);
Dan Albert00716d72015-03-13 22:57:40 -070058
59// Tests whether 's' starts with 'prefix'.
Elliott Hughes9955af22017-12-20 09:41:00 -080060// TODO: string_view
Dan Albert00716d72015-03-13 22:57:40 -070061bool StartsWith(const std::string& s, const char* prefix);
Elliott Hughes85484272016-10-25 14:56:04 -070062bool StartsWithIgnoreCase(const std::string& s, const char* prefix);
Elliott Hughes9955af22017-12-20 09:41:00 -080063bool StartsWith(const std::string& s, const std::string& prefix);
64bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix);
Dan Albert00716d72015-03-13 22:57:40 -070065
66// Tests whether 's' ends with 'suffix'.
Elliott Hughes9955af22017-12-20 09:41:00 -080067// TODO: string_view
Dan Albert00716d72015-03-13 22:57:40 -070068bool EndsWith(const std::string& s, const char* suffix);
Elliott Hughes85484272016-10-25 14:56:04 -070069bool EndsWithIgnoreCase(const std::string& s, const char* suffix);
Elliott Hughes9955af22017-12-20 09:41:00 -080070bool EndsWith(const std::string& s, const std::string& prefix);
71bool EndsWithIgnoreCase(const std::string& s, const std::string& prefix);
Dan Albert00716d72015-03-13 22:57:40 -070072
Elliott Hughes7872ff32017-01-13 18:51:32 -080073// Tests whether 'lhs' equals 'rhs', ignoring case.
74bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs);
75
Dan Albert00716d72015-03-13 22:57:40 -070076} // namespace base
77} // namespace android
78
Elliott Hughesf5224432016-03-23 15:04:52 -070079#endif // ANDROID_BASE_STRINGS_H