blob: 6f698d9a72881a064899b7f1154e41b7a25ae348 [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
17#include "base/strings.h"
18
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
35 std::vector<std::string> split;
36 if (s.size() == 0) {
37 // Split("", d) returns {} rather than {""}.
38 return split;
Dan Albert0f1e5442015-03-13 22:57:40 -070039 }
Dan Albert47328c92015-03-19 13:24:26 -070040
41 size_t base = 0;
42 size_t found;
43 do {
44 found = s.find_first_of(delimiters, base);
45 if (found != base) {
46 split.push_back(s.substr(base, found - base));
47 }
48
49 base = found + 1;
50 } while (found != s.npos);
51
52 return split;
Dan Albert0f1e5442015-03-13 22:57:40 -070053}
54
55std::string Trim(const std::string& s) {
56 std::string result;
57
58 if (s.size() == 0) {
59 return result;
60 }
61
62 size_t start_index = 0;
63 size_t end_index = s.size() - 1;
64
65 // Skip initial whitespace.
66 while (start_index < s.size()) {
67 if (!isspace(s[start_index])) {
68 break;
69 }
70 start_index++;
71 }
72
73 // Skip terminating whitespace.
74 while (end_index >= start_index) {
75 if (!isspace(s[end_index])) {
76 break;
77 }
78 end_index--;
79 }
80
81 // All spaces, no beef.
82 if (end_index < start_index) {
83 return "";
84 }
85 // Start_index is the first non-space, end_index is the last one.
86 return s.substr(start_index, end_index - start_index + 1);
87}
88
89template <typename StringT>
90std::string Join(const std::vector<StringT>& strings, char separator) {
91 if (strings.empty()) {
92 return "";
93 }
94
95 std::string result(strings[0]);
96 for (size_t i = 1; i < strings.size(); ++i) {
97 result += separator;
98 result += strings[i];
99 }
100 return result;
101}
102
103// Explicit instantiations.
104template std::string Join<std::string>(const std::vector<std::string>& strings,
105 char separator);
106template std::string Join<const char*>(const std::vector<const char*>& strings,
107 char separator);
108
109bool StartsWith(const std::string& s, const char* prefix) {
110 return s.compare(0, strlen(prefix), prefix) == 0;
111}
112
113bool EndsWith(const std::string& s, const char* suffix) {
114 size_t suffix_length = strlen(suffix);
115 size_t string_length = s.size();
116 if (suffix_length > string_length) {
117 return false;
118 }
119 size_t offset = string_length - suffix_length;
120 return s.compare(offset, suffix_length, suffix) == 0;
121}
122
123} // namespace base
124} // namespace android