blob: fc5c1ce36b2653f70dbfd92017cd4783db53b3a3 [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 Hughese3c5a2a2018-06-26 17:17:41 -070017#pragma once
Dan Albert00716d72015-03-13 22:57:40 -070018
Dan Albert0d20cc92015-05-21 18:37:36 -070019#include <sstream>
Dan Albert00716d72015-03-13 22:57:40 -070020#include <string>
21#include <vector>
22
23namespace android {
24namespace base {
25
Dan Albert0d716d02015-03-19 13:24:26 -070026// Splits a string into a vector of strings.
27//
Elliott Hughes658bd7b2015-04-17 17:08:16 -070028// The string is split at each occurrence of a character in delimiters.
Dan Albert0d716d02015-03-19 13:24:26 -070029//
Dan Albert0d716d02015-03-19 13:24:26 -070030// The empty string is not a valid delimiter list.
31std::vector<std::string> Split(const std::string& s,
32 const std::string& delimiters);
Dan Albert00716d72015-03-13 22:57:40 -070033
34// Trims whitespace off both ends of the given string.
35std::string Trim(const std::string& s);
36
Dan Albert0d20cc92015-05-21 18:37:36 -070037// Joins a container of things into a single string, using the given separator.
Casey Dahlinf8557e72015-10-30 18:54:38 -070038template <typename ContainerT, typename SeparatorT>
39std::string Join(const ContainerT& things, SeparatorT separator) {
Dan Albert0d20cc92015-05-21 18:37:36 -070040 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.
53extern template std::string Join(const std::vector<std::string>&, char);
54extern template std::string Join(const std::vector<const char*>&, char);
Casey Dahlinf8557e72015-10-30 18:54:38 -070055extern template std::string Join(const std::vector<std::string>&, const std::string&);
56extern template std::string Join(const std::vector<const char*>&, const std::string&);
Dan Albert00716d72015-03-13 22:57:40 -070057
58// Tests whether 's' starts with 'prefix'.
Elliott Hughes9955af22017-12-20 09:41:00 -080059// TODO: string_view
Dan Albert00716d72015-03-13 22:57:40 -070060bool StartsWith(const std::string& s, const char* prefix);
Elliott Hughes85484272016-10-25 14:56:04 -070061bool StartsWithIgnoreCase(const std::string& s, const char* prefix);
Elliott Hughes9955af22017-12-20 09:41:00 -080062bool StartsWith(const std::string& s, const std::string& prefix);
63bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix);
Yabin Cuia87b3882018-10-30 15:49:53 -070064bool StartsWith(const std::string& s, char 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);
Erik Kline226df752018-02-27 16:12:23 +090070bool EndsWith(const std::string& s, const std::string& suffix);
71bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix);
Yabin Cuia87b3882018-10-30 15:49:53 -070072bool EndsWith(const std::string& s, char suffix);
Dan Albert00716d72015-03-13 22:57:40 -070073
Elliott Hughes7872ff32017-01-13 18:51:32 -080074// Tests whether 'lhs' equals 'rhs', ignoring case.
75bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs);
76
Dan Albert00716d72015-03-13 22:57:40 -070077} // namespace base
78} // namespace android