StringHelpFunctions.cpp 5.4 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
J
jinhai 已提交
5
//
6 7 8 9 10
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.
J
jinhai 已提交
11

S
starlord 已提交
12 13
#include "utils/StringHelpFunctions.h"

S
shengjh 已提交
14
#include <fiu-local.h>
15
#include <algorithm>
G
groot 已提交
16
#include <regex>
S
starlord 已提交
17
#include <string>
G
groot 已提交
18

19 20
#include "utils/ValidationUtil.h"

J
jinhai 已提交
21
namespace milvus {
G
groot 已提交
22 23
namespace server {

S
starlord 已提交
24
void
S
starlord 已提交
25
StringHelpFunctions::TrimStringBlank(std::string& string) {
G
groot 已提交
26 27 28 29 30 31 32
    if (!string.empty()) {
        static std::string s_format(" \n\r\t");
        string.erase(0, string.find_first_not_of(s_format));
        string.erase(string.find_last_not_of(s_format) + 1);
    }
}

S
starlord 已提交
33
void
S
starlord 已提交
34
StringHelpFunctions::TrimStringQuote(std::string& string, const std::string& qoute) {
G
groot 已提交
35 36 37 38 39 40
    if (!string.empty()) {
        string.erase(0, string.find_first_not_of(qoute));
        string.erase(string.find_last_not_of(qoute) + 1);
    }
}

Z
Zhiru Zhu 已提交
41
void
S
starlord 已提交
42 43
StringHelpFunctions::SplitStringByDelimeter(const std::string& str, const std::string& delimeter,
                                            std::vector<std::string>& result) {
S
starlord 已提交
44
    if (str.empty()) {
Z
Zhiru Zhu 已提交
45
        return;
S
starlord 已提交
46 47
    }

Z
Zhiru Zhu 已提交
48 49 50 51 52 53 54 55 56 57
    size_t prev = 0, pos = 0;
    while (true) {
        pos = str.find_first_of(delimeter, prev);
        if (pos == std::string::npos) {
            result.emplace_back(str.substr(prev));
            break;
        } else {
            result.emplace_back(str.substr(prev, pos - prev));
            prev = pos + 1;
        }
G
groot 已提交
58
    }
Z
Zhiru Zhu 已提交
59 60 61 62 63 64 65 66
}

void
StringHelpFunctions::MergeStringWithDelimeter(const std::vector<std::string>& strs, const std::string& delimeter,
                                              std::string& result) {
    if (strs.empty()) {
        result = "";
        return;
G
groot 已提交
67 68
    }

Z
Zhiru Zhu 已提交
69 70 71 72
    result = strs[0];
    for (size_t i = 1; i < strs.size(); i++) {
        result = result + delimeter + strs[i];
    }
G
groot 已提交
73 74
}

S
starlord 已提交
75
Status
S
starlord 已提交
76 77
StringHelpFunctions::SplitStringByQuote(const std::string& str, const std::string& delimeter, const std::string& quote,
                                        std::vector<std::string>& result) {
G
groot 已提交
78
    if (quote.empty()) {
Z
Zhiru Zhu 已提交
79 80
        SplitStringByDelimeter(str, delimeter, result);
        return Status::OK();
G
groot 已提交
81 82 83 84 85
    }

    size_t last = 0;
    size_t index = str.find_first_of(quote, last);
    if (index == std::string::npos) {
Z
Zhiru Zhu 已提交
86 87
        SplitStringByDelimeter(str, delimeter, result);
        return Status::OK();
G
groot 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    }

    std::string process_str = str;
    while (index != std::string::npos) {
        std::string prefix = process_str.substr(last, index - last);
        std::string append_prefix;
        if (!prefix.empty()) {
            std::vector<std::string> prefix_split;
            SplitStringByDelimeter(prefix, delimeter, prefix_split);
            for (size_t i = 0; i < prefix_split.size() - 1; i++) {
                result.push_back(prefix_split[i]);
            }
            append_prefix = prefix_split[prefix_split.size() - 1];
        }
        last = index + 1;
        std::string postfix = process_str.substr(last);
        index = postfix.find_first_of(quote, 0);
S
shengjh 已提交
105 106
        fiu_do_on("StringHelpFunctions.SplitStringByQuote.invalid_index", index = std::string::npos);

G
groot 已提交
107
        if (index == std::string::npos) {
S
starlord 已提交
108
            return Status(SERVER_UNEXPECTED_ERROR, "");
G
groot 已提交
109 110 111 112 113 114
        }
        std::string quoted_text = postfix.substr(0, index);
        append_prefix += quoted_text;

        last = index + 1;
        index = postfix.find_first_of(delimeter, last);
S
shengjh 已提交
115 116 117
        fiu_do_on("StringHelpFunctions.SplitStringByQuote.index_gt_last", last = 0);
        fiu_do_on("StringHelpFunctions.SplitStringByQuote.invalid_index2", index = std::string::npos);

G
groot 已提交
118 119 120 121 122 123 124 125
        if (index != std::string::npos) {
            if (index > last) {
                append_prefix += postfix.substr(last, index - last);
            }
        } else {
            append_prefix += postfix.substr(last);
        }
        result.emplace_back(append_prefix);
S
shengjh 已提交
126
        fiu_do_on("StringHelpFunctions.SplitStringByQuote.last_is_end", last = postfix.length());
G
groot 已提交
127 128

        if (last == postfix.length()) {
S
starlord 已提交
129
            return Status::OK();
G
groot 已提交
130 131 132 133 134 135 136 137
        }

        process_str = postfix.substr(index + 1);
        last = 0;
        index = process_str.find_first_of(quote, last);
    }

    if (!process_str.empty()) {
Z
Zhiru Zhu 已提交
138
        SplitStringByDelimeter(process_str, delimeter, result);
G
groot 已提交
139 140
    }

S
starlord 已提交
141
    return Status::OK();
G
groot 已提交
142 143
}

G
groot 已提交
144 145 146 147 148 149 150 151 152 153
bool
StringHelpFunctions::IsRegexMatch(const std::string& target_str, const std::string& pattern_str) {
    // if target_str equals pattern_str, return true
    if (target_str == pattern_str) {
        return true;
    }

    // regex match
    std::regex pattern(pattern_str);
    std::smatch results;
154 155 156 157 158 159 160 161
    return std::regex_match(target_str, results, pattern);
}

Status
StringHelpFunctions::ConvertToBoolean(const std::string& str, bool& value) {
    auto status = ValidationUtil::ValidateStringIsBool(str);
    if (!status.ok()) {
        return status;
G
groot 已提交
162
    }
163 164 165 166 167 168

    std::string s = str;
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
    value = s == "true" || s == "on" || s == "yes" || s == "1";

    return Status::OK();
G
groot 已提交
169 170
}

S
starlord 已提交
171 172
}  // namespace server
}  // namespace milvus