StringHelper.cpp revision 1316091aaf89abc73ab11539b61b20f13ee261c4
18d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/*
28d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * Copyright (C) 2016 The Android Open Source Project
38d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
48d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * Licensed under the Apache License, Version 2.0 (the "License");
58d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * you may not use this file except in compliance with the License.
68d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * You may obtain a copy of the License at
78d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
88d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *      http://www.apache.org/licenses/LICENSE-2.0
98d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
108d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * Unless required by applicable law or agreed to in writing, software
118d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * distributed under the License is distributed on an "AS IS" BASIS,
128d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * See the License for the specific language governing permissions and
148d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * limitations under the License.
158d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S */
168d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
178d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S#include "StringHelper.h"
188d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
198d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha Snamespace android {
208d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
218d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S// static
228d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha Sstd::string StringHelper::Upcase(const std::string &in) {
238d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    std::string out{in};
248d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
258d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    for (auto &ch : out) {
268d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        ch = toupper(ch);
278d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    }
288d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
298d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    return out;
308d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S}
318d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
328d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S// static
338d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha Svoid StringHelper::SplitString(
348d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        const std::string &s, char c, std::vector<std::string> *components) {
358d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    components->clear();
368d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
378d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    size_t startPos = 0;
388d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    size_t matchPos;
398d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    while ((matchPos = s.find(c, startPos)) != std::string::npos) {
408d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        components->push_back(s.substr(startPos, matchPos - startPos));
418d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        startPos = matchPos + 1;
428d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    }
438d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
448d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    if (startPos + 1 < s.length()) {
458d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        components->push_back(s.substr(startPos));
468d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    }
478d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S}
488d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
498d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S// static
508d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha Sstd::string StringHelper::JoinStrings(
518d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        const std::vector<std::string> &components,
528d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        const std::string &separator) {
538d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    std::string out;
548d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    bool first = true;
558d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    for (const auto &component : components) {
568d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        if (!first) {
578d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S            out += separator;
588d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        }
598d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        out += component;
608d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
618d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        first = false;
628d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    }
638d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
648d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    return out;
658d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S}
668d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
678d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S}  // namespace android
688d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
698d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S