173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe/*
273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * Copyright (C) 2015 The Android Open Source Project
373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe *
473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * Licensed under the Apache License, Version 2.0 (the "License");
573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * you may not use this file except in compliance with the License.
673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * You may obtain a copy of the License at
773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe *
873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe *      http://www.apache.org/licenses/LICENSE-2.0
973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe *
1073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * Unless required by applicable law or agreed to in writing, software
1173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * distributed under the License is distributed on an "AS IS" BASIS,
1273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * See the License for the specific language governing permissions and
1473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * limitations under the License.
1573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe */
1673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
1773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#ifndef ART_OTAPREOPT_STRING_HELPERS_H_
1873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#define ART_OTAPREOPT_STRING_HELPERS_H_
1973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
2073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#include <sstream>
2173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#include <string>
2273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
2373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#include <android-base/macros.h>
2473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
2573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampenamespace android {
2673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampenamespace installd {
2773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
2873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampestatic inline bool StringStartsWith(const std::string& target,
2973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe                                    const char* prefix) {
3073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    return target.compare(0, strlen(prefix), prefix) == 0;
3173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe}
3273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
3373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe// Split the input according to the separator character. Doesn't honor quotation.
3473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampestatic inline std::vector<std::string> Split(const std::string& in, const char separator) {
3573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    if (in.empty()) {
3673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe        return std::vector<std::string>();
3773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    }
3873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
3973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    std::vector<std::string> ret;
4073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    std::stringstream strstr(in);
4173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    std::string token;
4273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
4373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    while (std::getline(strstr, token, separator)) {
4473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe        ret.push_back(token);
4573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    }
4673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
4773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    return ret;
4873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe}
4973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
5073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampetemplate <typename StringT>
5173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampestatic inline std::string Join(const std::vector<StringT>& strings, char separator) {
5273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    if (strings.empty()) {
5373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe        return "";
5473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    }
5573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
5673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    std::string result(strings[0]);
5773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    for (size_t i = 1; i < strings.size(); ++i) {
5873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe        result += separator;
5973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe        result += strings[i];
6073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    }
6173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    return result;
6273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe}
6373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
6473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe}  // namespace installd
6573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe}  // namespace android
6673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
6773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#endif  // ART_OTAPREOPT_STRING_HELPERS_H_
68