StringHelpFunctions.cpp 5.2 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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
//
//   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.

S
starlord 已提交
18 19
#include "utils/StringHelpFunctions.h"

S
shengjh 已提交
20
#include <fiu-local.h>
G
groot 已提交
21
#include <regex>
S
starlord 已提交
22
#include <string>
G
groot 已提交
23

J
jinhai 已提交
24
namespace milvus {
G
groot 已提交
25 26
namespace server {

S
starlord 已提交
27
void
S
starlord 已提交
28
StringHelpFunctions::TrimStringBlank(std::string& string) {
G
groot 已提交
29 30 31 32 33 34 35
    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 已提交
36
void
S
starlord 已提交
37
StringHelpFunctions::TrimStringQuote(std::string& string, const std::string& qoute) {
G
groot 已提交
38 39 40 41 42 43
    if (!string.empty()) {
        string.erase(0, string.find_first_not_of(qoute));
        string.erase(string.find_last_not_of(qoute) + 1);
    }
}

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

Z
Zhiru Zhu 已提交
51 52 53 54 55 56 57 58 59 60
    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 已提交
61
    }
Z
Zhiru Zhu 已提交
62 63 64 65 66 67 68 69
}

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

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

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

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

    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 已提交
108 109
        fiu_do_on("StringHelpFunctions.SplitStringByQuote.invalid_index", index = std::string::npos);

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

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

G
groot 已提交
121 122 123 124 125 126 127 128
        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 已提交
129
        fiu_do_on("StringHelpFunctions.SplitStringByQuote.last_is_end", last = postfix.length());
G
groot 已提交
130 131

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

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

    if (!process_str.empty()) {
Z
Zhiru Zhu 已提交
141
        SplitStringByDelimeter(process_str, delimeter, result);
G
groot 已提交
142 143
    }

S
starlord 已提交
144
    return Status::OK();
G
groot 已提交
145 146
}

G
groot 已提交
147 148 149 150 151 152 153 154 155 156
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;
157
    if (std::regex_match(target_str, results, pattern)) {
G
groot 已提交
158 159 160 161 162 163
        return true;
    } else {
        return false;
    }
}

S
starlord 已提交
164 165
}  // namespace server
}  // namespace milvus