blob: d36d942b3a7c15f90c968cfda1266398e3ff0fa8 [file] [log] [blame]
Steven Morelandaf440142016-09-07 10:09:11 -07001/*
2 * Copyright (C) 2016 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 "StringHelper.h"
18
19namespace android {
20
21// static
22std::string StringHelper::Upcase(const std::string &in) {
23 std::string out{in};
24
25 for (auto &ch : out) {
26 ch = toupper(ch);
27 }
28
29 return out;
30}
31
32// static
33void StringHelper::SplitString(
34 const std::string &s, char c, std::vector<std::string> *components) {
35 components->clear();
36
37 size_t startPos = 0;
38 size_t matchPos;
39 while ((matchPos = s.find(c, startPos)) != std::string::npos) {
40 components->push_back(s.substr(startPos, matchPos - startPos));
41 startPos = matchPos + 1;
42 }
43
44 if (startPos + 1 < s.length()) {
45 components->push_back(s.substr(startPos));
46 }
47}
48
49// static
50std::string StringHelper::JoinStrings(
51 const std::vector<std::string> &components,
52 const std::string &separator) {
53 std::string out;
54 bool first = true;
55 for (const auto &component : components) {
56 if (!first) {
57 out += separator;
58 }
59 out += component;
60
61 first = false;
62 }
63
64 return out;
65}
66
67} // namespace android
68