106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include "string_utils.h"
1606f6ba60Sopenharmony_ci#include <sstream>
1706f6ba60Sopenharmony_ci#include <iomanip>
1806f6ba60Sopenharmony_ci
1906f6ba60Sopenharmony_cibool StringUtils::EndsWith(const std::string& str, const std::string& postfix)
2006f6ba60Sopenharmony_ci{
2106f6ba60Sopenharmony_ci    if (str.size() < postfix.size()) {
2206f6ba60Sopenharmony_ci        return false;
2306f6ba60Sopenharmony_ci    }
2406f6ba60Sopenharmony_ci    return str.compare(str.size() - postfix.size(), postfix.size(), postfix) == 0;
2506f6ba60Sopenharmony_ci}
2606f6ba60Sopenharmony_ci
2706f6ba60Sopenharmony_cibool StringUtils::StartsWith(const std::string& str, const std::string& prefix)
2806f6ba60Sopenharmony_ci{
2906f6ba60Sopenharmony_ci    if (str.size() < prefix.size()) {
3006f6ba60Sopenharmony_ci        return false;
3106f6ba60Sopenharmony_ci    }
3206f6ba60Sopenharmony_ci    return str.compare(0, prefix.size(), prefix) == 0;
3306f6ba60Sopenharmony_ci}
3406f6ba60Sopenharmony_ci
3506f6ba60Sopenharmony_cistd::string StringUtils::ConvertToHex(uint64_t num)
3606f6ba60Sopenharmony_ci{
3706f6ba60Sopenharmony_ci    std::stringstream ss;
3806f6ba60Sopenharmony_ci    ss << std::hex << num;
3906f6ba60Sopenharmony_ci    return ss.str();
4006f6ba60Sopenharmony_ci}
4106f6ba60Sopenharmony_ci
4206f6ba60Sopenharmony_cibool StringUtils::Contains(const std::string& str, const std::string& target)
4306f6ba60Sopenharmony_ci{
4406f6ba60Sopenharmony_ci    return str.find(target) != std::string::npos;
4506f6ba60Sopenharmony_ci}
4606f6ba60Sopenharmony_ci
4706f6ba60Sopenharmony_cistd::string StringUtils::Strip(const std::string& str)
4806f6ba60Sopenharmony_ci{
4906f6ba60Sopenharmony_ci    std::string blanks = " \f\v\t\r\n";
5006f6ba60Sopenharmony_ci
5106f6ba60Sopenharmony_ci    auto first = str.find_first_not_of(blanks);
5206f6ba60Sopenharmony_ci    if (first == std::string::npos) {
5306f6ba60Sopenharmony_ci        return "";
5406f6ba60Sopenharmony_ci    }
5506f6ba60Sopenharmony_ci
5606f6ba60Sopenharmony_ci    auto last = str.find_last_not_of(blanks);
5706f6ba60Sopenharmony_ci    if (last == std::string::npos) {
5806f6ba60Sopenharmony_ci        return "";
5906f6ba60Sopenharmony_ci    }
6006f6ba60Sopenharmony_ci    return str.substr(first, last - first + 1);
6106f6ba60Sopenharmony_ci}
6206f6ba60Sopenharmony_ci
6306f6ba60Sopenharmony_cistd::string StringUtils::Join(const std::vector<std::string>& strs, const std::string& sep)
6406f6ba60Sopenharmony_ci{
6506f6ba60Sopenharmony_ci    std::string result;
6606f6ba60Sopenharmony_ci    for (auto& s : strs) {
6706f6ba60Sopenharmony_ci        result += s;
6806f6ba60Sopenharmony_ci        result += sep;
6906f6ba60Sopenharmony_ci    }
7006f6ba60Sopenharmony_ci    if (result.size() > sep.size()) {
7106f6ba60Sopenharmony_ci        result.resize(result.size() - sep.size());
7206f6ba60Sopenharmony_ci    }
7306f6ba60Sopenharmony_ci    return result;
7406f6ba60Sopenharmony_ci}
7506f6ba60Sopenharmony_ci
7606f6ba60Sopenharmony_cistd::vector<std::string> StringUtils::Split(const std::string& str, const std::string& sep)
7706f6ba60Sopenharmony_ci{
7806f6ba60Sopenharmony_ci    std::vector<std::string> result;
7906f6ba60Sopenharmony_ci    if (str.empty() || sep.empty() || str.size() < sep.size()) {
8006f6ba60Sopenharmony_ci        return result;
8106f6ba60Sopenharmony_ci    }
8206f6ba60Sopenharmony_ci    size_t start = 0; // start index
8306f6ba60Sopenharmony_ci    size_t pos = 0;
8406f6ba60Sopenharmony_ci    while (start < str.size()) {
8506f6ba60Sopenharmony_ci        pos = str.find(sep, start);
8606f6ba60Sopenharmony_ci        if (pos != std::string::npos) {
8706f6ba60Sopenharmony_ci            result.push_back(str.substr(start, pos - start));
8806f6ba60Sopenharmony_ci            start = pos + sep.size(); // next start index
8906f6ba60Sopenharmony_ci        } else {
9006f6ba60Sopenharmony_ci            // the last part
9106f6ba60Sopenharmony_ci            result.push_back(str.substr(start));
9206f6ba60Sopenharmony_ci            break;
9306f6ba60Sopenharmony_ci        }
9406f6ba60Sopenharmony_ci    }
9506f6ba60Sopenharmony_ci    return result;
9606f6ba60Sopenharmony_ci}