blob: d3375d9faca718dced39593406c9dbe721f8c302 [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
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;
39 do {
40 found = s.find_first_of(delimiters, base);
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -070041 result.push_back(s.substr(base, found - base));
Dan Albert47328c92015-03-19 13:24:26 -070042 base = found + 1;
43 } while (found != s.npos);
44
Elliott Hughes8d5fa6d2015-04-24 23:02:00 -070045 return result;
Dan Albert0f1e5442015-03-13 22:57:40 -070046}
47
48std::string Trim(const std::string& s) {
49 std::string result;
50
51 if (s.size() == 0) {
52 return result;
53 }
54
55 size_t start_index = 0;
56 size_t end_index = s.size() - 1;
57
58 // Skip initial whitespace.
59 while (start_index < s.size()) {
60 if (!isspace(s[start_index])) {
61 break;
62 }
63 start_index++;
64 }
65
66 // Skip terminating whitespace.
67 while (end_index >= start_index) {
68 if (!isspace(s[end_index])) {
69 break;
70 }
71 end_index--;
72 }
73
74 // All spaces, no beef.
75 if (end_index < start_index) {
76 return "";
77 }
78 // Start_index is the first non-space, end_index is the last one.
79 return s.substr(start_index, end_index - start_index + 1);
80}
81
82template <typename StringT>
83std::string Join(const std::vector<StringT>& strings, char separator) {
84 if (strings.empty()) {
85 return "";
86 }
87
88 std::string result(strings[0]);
89 for (size_t i = 1; i < strings.size(); ++i) {
90 result += separator;
91 result += strings[i];
92 }
93 return result;
94}
95
96// Explicit instantiations.
97template std::string Join<std::string>(const std::vector<std::string>& strings,
98 char separator);
99template std::string Join<const char*>(const std::vector<const char*>& strings,
100 char separator);
101
102bool StartsWith(const std::string& s, const char* prefix) {
103 return s.compare(0, strlen(prefix), prefix) == 0;
104}
105
106bool EndsWith(const std::string& s, const char* suffix) {
107 size_t suffix_length = strlen(suffix);
108 size_t string_length = s.size();
109 if (suffix_length > string_length) {
110 return false;
111 }
112 size_t offset = string_length - suffix_length;
113 return s.compare(offset, suffix_length, suffix) == 0;
114}
115
116} // namespace base
117} // namespace android