blob: a8bb2a986e95f6565af92c4cb11edf9d15440290 [file] [log] [blame]
Dan Albert0f1e5442015-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 Hughes4f713192015-12-04 22:00:26 -080017#include "android-base/strings.h"
Dan Albert0f1e5442015-03-13 22:57:40 -070018
Dan Albert47328c92015-03-19 13:24:26 -070019#include <stdlib.h>
Dan Albert4b3f5332015-03-26 23:23:32 -070020#include <string.h>
Dan Albert47328c92015-03-19 13:24:26 -070021
Dan Albert0f1e5442015-03-13 22:57:40 -070022#include <string>
23#include <vector>
24
25namespace android {
26namespace base {
27
Dan Albert47328c92015-03-19 13:24:26 -070028#define CHECK_NE(a, b) \
29 if ((a) == (b)) abort();
30
31std::vector<std::string> Split(const std::string& s,
32 const std::string& delimiters) {
33 CHECK_NE(delimiters.size(), 0U);
34
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -070035 std::vector<std::string> result;
Dan Albert47328c92015-03-19 13:24:26 -070036
37 size_t base = 0;
38 size_t found;
Elliott Hughesbf0dd7c2017-02-07 15:30:24 -080039 while (true) {
Dan Albert47328c92015-03-19 13:24:26 -070040 found = s.find_first_of(delimiters, base);
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -070041 result.push_back(s.substr(base, found - base));
Elliott Hughesbf0dd7c2017-02-07 15:30:24 -080042 if (found == s.npos) break;
Dan Albert47328c92015-03-19 13:24:26 -070043 base = found + 1;
Elliott Hughesbf0dd7c2017-02-07 15:30:24 -080044 }
Dan Albert47328c92015-03-19 13:24:26 -070045
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -070046 return result;
Dan Albert0f1e5442015-03-13 22:57:40 -070047}
48
49std::string Trim(const std::string& s) {
50 std::string result;
51
52 if (s.size() == 0) {
53 return result;
54 }
55
56 size_t start_index = 0;
57 size_t end_index = s.size() - 1;
58
59 // Skip initial whitespace.
60 while (start_index < s.size()) {
61 if (!isspace(s[start_index])) {
62 break;
63 }
64 start_index++;
65 }
66
67 // Skip terminating whitespace.
68 while (end_index >= start_index) {
69 if (!isspace(s[end_index])) {
70 break;
71 }
72 end_index--;
73 }
74
75 // All spaces, no beef.
76 if (end_index < start_index) {
77 return "";
78 }
79 // Start_index is the first non-space, end_index is the last one.
80 return s.substr(start_index, end_index - start_index + 1);
81}
82
Dan Alberte0da8a12015-05-21 18:37:36 -070083// These cases are probably the norm, so we mark them extern in the header to
84// aid compile time and binary size.
85template std::string Join(const std::vector<std::string>&, char);
86template std::string Join(const std::vector<const char*>&, char);
Casey Dahlin5345f1d2015-10-30 18:54:38 -070087template std::string Join(const std::vector<std::string>&, const std::string&);
88template std::string Join(const std::vector<const char*>&, const std::string&);
Dan Albert0f1e5442015-03-13 22:57:40 -070089
90bool StartsWith(const std::string& s, const char* prefix) {
Elliott Hughes42937492016-10-25 14:56:04 -070091 return strncmp(s.c_str(), prefix, strlen(prefix)) == 0;
Dan Albert0f1e5442015-03-13 22:57:40 -070092}
93
Elliott Hughes579e6822017-12-20 09:41:00 -080094bool StartsWith(const std::string& s, const std::string& prefix) {
95 return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0;
96}
97
Elliott Hughes42937492016-10-25 14:56:04 -070098bool StartsWithIgnoreCase(const std::string& s, const char* prefix) {
99 return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0;
100}
101
Elliott Hughes579e6822017-12-20 09:41:00 -0800102bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) {
103 return strncasecmp(s.c_str(), prefix.c_str(), prefix.size()) == 0;
104}
105
106static bool EndsWith(const std::string& s, const char* suffix, size_t suffix_length,
107 bool case_sensitive) {
Dan Albert0f1e5442015-03-13 22:57:40 -0700108 size_t string_length = s.size();
109 if (suffix_length > string_length) {
110 return false;
111 }
112 size_t offset = string_length - suffix_length;
Elliott Hughes42937492016-10-25 14:56:04 -0700113 return (case_sensitive ? strncmp : strncasecmp)(s.c_str() + offset, suffix, suffix_length) == 0;
114}
115
116bool EndsWith(const std::string& s, const char* suffix) {
Elliott Hughes579e6822017-12-20 09:41:00 -0800117 return EndsWith(s, suffix, strlen(suffix), true);
118}
119
120bool EndsWith(const std::string& s, const std::string& suffix) {
121 return EndsWith(s, suffix.c_str(), suffix.size(), true);
Elliott Hughes42937492016-10-25 14:56:04 -0700122}
123
124bool EndsWithIgnoreCase(const std::string& s, const char* suffix) {
Elliott Hughes579e6822017-12-20 09:41:00 -0800125 return EndsWith(s, suffix, strlen(suffix), false);
126}
127
128bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) {
129 return EndsWith(s, suffix.c_str(), suffix.size(), false);
Dan Albert0f1e5442015-03-13 22:57:40 -0700130}
131
Elliott Hughes21407822017-01-13 18:51:32 -0800132bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) {
133 return strcasecmp(lhs.c_str(), rhs.c_str()) == 0;
134}
135
Dan Albert0f1e5442015-03-13 22:57:40 -0700136} // namespace base
137} // namespace android