100600bfbSopenharmony_ci/*
200600bfbSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
300600bfbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
400600bfbSopenharmony_ci * you may not use this file except in compliance with the License.
500600bfbSopenharmony_ci * You may obtain a copy of the License at
600600bfbSopenharmony_ci *
700600bfbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
800600bfbSopenharmony_ci *
900600bfbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1000600bfbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1100600bfbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1200600bfbSopenharmony_ci * See the License for the specific language governing permissions and
1300600bfbSopenharmony_ci * limitations under the License.
1400600bfbSopenharmony_ci */
1500600bfbSopenharmony_ci#include "util/string_utils.h"
1600600bfbSopenharmony_ci#include <cmath>
1700600bfbSopenharmony_ci#include <sstream>
1800600bfbSopenharmony_ci#include <iomanip>
1900600bfbSopenharmony_ci#include <regex>
2000600bfbSopenharmony_ci#include "string_ex.h"
2100600bfbSopenharmony_ciusing namespace std;
2200600bfbSopenharmony_cinamespace OHOS {
2300600bfbSopenharmony_cinamespace HiviewDFX {
2400600bfbSopenharmony_ciStringUtils::StringUtils()
2500600bfbSopenharmony_ci{
2600600bfbSopenharmony_ci}
2700600bfbSopenharmony_ci
2800600bfbSopenharmony_ciStringUtils::~StringUtils()
2900600bfbSopenharmony_ci{
3000600bfbSopenharmony_ci}
3100600bfbSopenharmony_ci
3200600bfbSopenharmony_civoid StringUtils::StringSplit(const string &content, const string &split, vector<string> &result)
3300600bfbSopenharmony_ci{
3400600bfbSopenharmony_ci    SplitStr(content, split, result, false, false);
3500600bfbSopenharmony_ci}
3600600bfbSopenharmony_ci
3700600bfbSopenharmony_cibool StringUtils::IsBegin(const string &content, const string &begin)
3800600bfbSopenharmony_ci{
3900600bfbSopenharmony_ci    if (content.find(begin) == 0) {
4000600bfbSopenharmony_ci        return true;
4100600bfbSopenharmony_ci    }
4200600bfbSopenharmony_ci    return false;
4300600bfbSopenharmony_ci}
4400600bfbSopenharmony_ci
4500600bfbSopenharmony_cibool StringUtils::IsEnd(const string &content, const string &end)
4600600bfbSopenharmony_ci{
4700600bfbSopenharmony_ci    bool result = false;
4800600bfbSopenharmony_ci    if (content.length() >= end.length()) {
4900600bfbSopenharmony_ci        result = (0 == content.compare(content.length() - end.length(), end.length(), end));
5000600bfbSopenharmony_ci    }
5100600bfbSopenharmony_ci    return result;
5200600bfbSopenharmony_ci}
5300600bfbSopenharmony_ci
5400600bfbSopenharmony_cibool StringUtils::IsContain(const string &content, const string &contain)
5500600bfbSopenharmony_ci{
5600600bfbSopenharmony_ci    return IsSubStr(content, contain);
5700600bfbSopenharmony_ci}
5800600bfbSopenharmony_ci
5900600bfbSopenharmony_cibool StringUtils::IsSameStr(const string &first, const string &second)
6000600bfbSopenharmony_ci{
6100600bfbSopenharmony_ci    if (first == second) {
6200600bfbSopenharmony_ci        return true;
6300600bfbSopenharmony_ci    }
6400600bfbSopenharmony_ci    return false;
6500600bfbSopenharmony_ci}
6600600bfbSopenharmony_ci
6700600bfbSopenharmony_civoid StringUtils::ReplaceAll(string &str, const string &oldValue, const string &newValue)
6800600bfbSopenharmony_ci{
6900600bfbSopenharmony_ci    str = ReplaceStr(str, oldValue, newValue);
7000600bfbSopenharmony_ci}
7100600bfbSopenharmony_ci
7200600bfbSopenharmony_cibool StringUtils::IsNum(string str)
7300600bfbSopenharmony_ci{
7400600bfbSopenharmony_ci    return IsNumericStr(str);
7500600bfbSopenharmony_ci}
7600600bfbSopenharmony_ci
7700600bfbSopenharmony_civoid StringUtils::HexToDec(const string &str, uint64_t &value)
7800600bfbSopenharmony_ci{
7900600bfbSopenharmony_ci    size_t l = str.length();
8000600bfbSopenharmony_ci    for (size_t i = 0; i < l; i++) {
8100600bfbSopenharmony_ci        if (str[i] >= '0' && str[i] <= '9')
8200600bfbSopenharmony_ci            value += (str[i] - '0') * pow(HEX_STR, l - 1 - i);
8300600bfbSopenharmony_ci        else
8400600bfbSopenharmony_ci            value += (str[i] - 'A' + DEC_STR) * pow(HEX_STR, l - 1 - i);
8500600bfbSopenharmony_ci    }
8600600bfbSopenharmony_ci}
8700600bfbSopenharmony_ci
8800600bfbSopenharmony_cichar StringUtils::GetBlank()
8900600bfbSopenharmony_ci{
9000600bfbSopenharmony_ci    char blank = ' ';
9100600bfbSopenharmony_ci    return blank;
9200600bfbSopenharmony_ci};
9300600bfbSopenharmony_ci
9400600bfbSopenharmony_cichar StringUtils::GetSeparator()
9500600bfbSopenharmony_ci{
9600600bfbSopenharmony_ci    char separator = '-';
9700600bfbSopenharmony_ci    return separator;
9800600bfbSopenharmony_ci}
9900600bfbSopenharmony_ci
10000600bfbSopenharmony_ci/**
10100600bfbSopenharmony_ci * @description: The character length is insufficient to complement
10200600bfbSopenharmony_ci * @param {string} &str-The string to be filled
10300600bfbSopenharmony_ci * @param {int} &length-The length of the string to be set
10400600bfbSopenharmony_ci * @param {char} &fileStr-A character to be added when the length is insufficient
10500600bfbSopenharmony_ci * @param {bool} &left-true:Add characters on the left,false:Add characters to the right
10600600bfbSopenharmony_ci * @return {*}
10700600bfbSopenharmony_ci */
10800600bfbSopenharmony_ci
10900600bfbSopenharmony_civoid StringUtils::SetWidth(const int &width, const char &fileStr, const bool &left, string &str)
11000600bfbSopenharmony_ci{
11100600bfbSopenharmony_ci    ostringstream s;
11200600bfbSopenharmony_ci    s.clear();
11300600bfbSopenharmony_ci
11400600bfbSopenharmony_ci    if (left) {
11500600bfbSopenharmony_ci        s << setw(width) << setfill(fileStr) << setiosflags(ios::left) << str;
11600600bfbSopenharmony_ci    } else {
11700600bfbSopenharmony_ci        s << setw(width) << setfill(fileStr) << setiosflags(ios::right) << str;
11800600bfbSopenharmony_ci    }
11900600bfbSopenharmony_ci    str = s.str();
12000600bfbSopenharmony_ci}
12100600bfbSopenharmony_ci} // namespace HiviewDFX
12200600bfbSopenharmony_ci} // namespace OHOS
123