StringHelpFunctions.h 2.3 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

G
groot 已提交
12 13
#pragma once

S
starlord 已提交
14
#include "utils/Status.h"
G
groot 已提交
15

S
starlord 已提交
16
#include <string>
S
starlord 已提交
17
#include <vector>
G
groot 已提交
18

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

class StringHelpFunctions {
S
starlord 已提交
23
 private:
G
groot 已提交
24 25
    StringHelpFunctions() = default;

S
starlord 已提交
26
 public:
G
groot 已提交
27 28
    // trim blanks from begin and end
    // "  a b c  "  =>  "a b c"
S
starlord 已提交
29 30
    static void
    TrimStringBlank(std::string& string);
G
groot 已提交
31

G
groot 已提交
32 33
    // trim quotes from begin and end
    // "'abc'"  =>  "abc"
S
starlord 已提交
34 35
    static void
    TrimStringQuote(std::string& string, const std::string& qoute);
G
groot 已提交
36

S
starlord 已提交
37
    // split string by delimeter ','
G
groot 已提交
38 39 40 41 42 43
    // a,b,c            a | b | c
    // a,b,             a | b |
    // ,b,c               | b | c
    // ,b,                | b |
    // ,,                 |   |
    // a                    a
Z
Zhiru Zhu 已提交
44
    static void
S
starlord 已提交
45
    SplitStringByDelimeter(const std::string& str, const std::string& delimeter, std::vector<std::string>& result);
G
groot 已提交
46

G
groot 已提交
47 48
    // merge strings with delimeter
    // "a", "b", "c"  => "a,b,c"
Z
Zhiru Zhu 已提交
49 50 51
    static void
    MergeStringWithDelimeter(const std::vector<std::string>& strs, const std::string& delimeter, std::string& result);

S
starlord 已提交
52
    // assume the table has two columns, quote='\"', delimeter=','
G
groot 已提交
53 54 55 56 57 58
    //  a,b             a | b
    //  "aa,gg,yy",b    aa,gg,yy | b
    //  aa"dd,rr"kk,pp  aadd,rrkk | pp
    //  "aa,bb"         aa,bb
    //  55,1122\"aa,bb\",yyy,\"kkk\"    55 | 1122aa,bb | yyy | kkk
    //  "55,1122"aa,bb",yyy,"kkk"   illegal
S
starlord 已提交
59 60 61
    static Status
    SplitStringByQuote(const std::string& str, const std::string& delimeter, const std::string& quote,
                       std::vector<std::string>& result);
G
groot 已提交
62 63 64 65 66

    // std regex match function
    // regex grammar reference: http://www.cplusplus.com/reference/regex/ECMAScript/
    static bool
    IsRegexMatch(const std::string& target_str, const std::string& pattern);
G
groot 已提交
67 68
};

S
starlord 已提交
69 70
}  // namespace server
}  // namespace milvus