/root/doris/be/src/runtime/string_search.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <cstring> |
21 | | #include <functional> |
22 | | #include <vector> |
23 | | |
24 | | #include "common/logging.h" |
25 | | #include "vec/common/string_ref.h" |
26 | | #include "vec/common/string_searcher.h" |
27 | | |
28 | | namespace doris { |
29 | | |
30 | | class StringSearch { |
31 | | public: |
32 | 393 | virtual ~StringSearch() = default; |
33 | 18 | StringSearch() : _pattern(nullptr) {} |
34 | | |
35 | 381 | StringSearch(const StringRef* pattern) { set_pattern(pattern); } |
36 | | |
37 | 393 | void set_pattern(const StringRef* pattern) { |
38 | 393 | _pattern = pattern; |
39 | 393 | _str_searcher.reset(new ASCIICaseSensitiveStringSearcher(pattern->data, pattern->size)); |
40 | 393 | } |
41 | | |
42 | | // search for this pattern in str. |
43 | | // Returns the offset into str if the pattern exists |
44 | | // Returns -1 if the pattern is not found |
45 | 679 | int search(const StringRef* str) const { |
46 | 679 | auto it = search(str->data, str->size); |
47 | 679 | if (it == str->data + str->size) { |
48 | 351 | return -1; |
49 | 351 | } else { |
50 | 328 | return it - str->data; |
51 | 328 | } |
52 | 679 | } |
53 | | |
54 | 0 | int search(const StringRef& str) const { |
55 | 0 | auto it = search(str.data, str.size); |
56 | 0 | if (it == str.data + str.size) { |
57 | 0 | return -1; |
58 | 0 | } else { |
59 | 0 | return it - str.data; |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | | // search for this pattern in str. |
64 | | // Returns the offset into str if the pattern exists |
65 | | // Returns str+len if the pattern is not found |
66 | 683 | const char* search(const char* str, size_t len) const { |
67 | 683 | if (!str || !_pattern || _pattern->size == 0) { |
68 | 0 | return str + len; |
69 | 0 | } |
70 | | |
71 | 683 | return _str_searcher->search(str, len); |
72 | 683 | } |
73 | | |
74 | 4 | inline size_t get_pattern_length() { return _pattern ? _pattern->size : 0; } |
75 | | |
76 | | private: |
77 | | const StringRef* _pattern; |
78 | | std::unique_ptr<ASCIICaseSensitiveStringSearcher> _str_searcher; |
79 | | }; |
80 | | |
81 | | } // namespace doris |