blob: 224a46f5b080c3bf2845c7fbc98097cf64e9978b [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
19#include <string>
20#include <vector>
21
22namespace android {
23namespace base {
24
25void Split(const std::string& s, char separator,
26 std::vector<std::string>* result) {
27 const char* p = s.data();
28 const char* end = p + s.size();
29 while (p != end) {
30 if (*p == separator) {
31 ++p;
32 } else {
33 const char* start = p;
34 while (++p != end && *p != separator) {
35 // Skip to the next occurrence of the separator.
36 }
37 result->push_back(std::string(start, p - start));
38 }
39 }
40}
41
42std::string Trim(const std::string& s) {
43 std::string result;
44
45 if (s.size() == 0) {
46 return result;
47 }
48
49 size_t start_index = 0;
50 size_t end_index = s.size() - 1;
51
52 // Skip initial whitespace.
53 while (start_index < s.size()) {
54 if (!isspace(s[start_index])) {
55 break;
56 }
57 start_index++;
58 }
59
60 // Skip terminating whitespace.
61 while (end_index >= start_index) {
62 if (!isspace(s[end_index])) {
63 break;
64 }
65 end_index--;
66 }
67
68 // All spaces, no beef.
69 if (end_index < start_index) {
70 return "";
71 }
72 // Start_index is the first non-space, end_index is the last one.
73 return s.substr(start_index, end_index - start_index + 1);
74}
75
76template <typename StringT>
77std::string Join(const std::vector<StringT>& strings, char separator) {
78 if (strings.empty()) {
79 return "";
80 }
81
82 std::string result(strings[0]);
83 for (size_t i = 1; i < strings.size(); ++i) {
84 result += separator;
85 result += strings[i];
86 }
87 return result;
88}
89
90// Explicit instantiations.
91template std::string Join<std::string>(const std::vector<std::string>& strings,
92 char separator);
93template std::string Join<const char*>(const std::vector<const char*>& strings,
94 char separator);
95
96bool StartsWith(const std::string& s, const char* prefix) {
97 return s.compare(0, strlen(prefix), prefix) == 0;
98}
99
100bool EndsWith(const std::string& s, const char* suffix) {
101 size_t suffix_length = strlen(suffix);
102 size_t string_length = s.size();
103 if (suffix_length > string_length) {
104 return false;
105 }
106 size_t offset = string_length - suffix_length;
107 return s.compare(offset, suffix_length, suffix) == 0;
108}
109
110} // namespace base
111} // namespace android