/root/doris/be/src/util/hyperscan_util.cpp
Line | Count | Source |
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 | | #include "util/hyperscan_util.h" |
19 | | |
20 | | #include <re2/re2.h> |
21 | | #include <re2/stringpiece.h> |
22 | | |
23 | | #include <charconv> |
24 | | |
25 | | namespace doris { |
26 | | namespace { |
27 | | |
28 | 38 | bool is_larger_than_fifty(std::string_view str) { |
29 | 38 | int number = 0; |
30 | 38 | auto [_, error] = std::from_chars(str.data(), str.data() + str.size(), number); |
31 | 38 | return error == std::errc() && number > 50; |
32 | 38 | } |
33 | | |
34 | | class SlowWithHyperscanChecker { |
35 | | public: |
36 | | SlowWithHyperscanChecker() |
37 | 1 | : _searcher_one_repeat(R"(\{\s*([\d]+)\s*,?\s*})"), |
38 | 1 | _searcher_two_repeats(R"(\{\s*([\d]+)\s*,\s*([\d]+)\s*\})") {} |
39 | | |
40 | 266 | bool is_slow(std::string_view regexp) const { |
41 | 266 | return is_slow_one_repeat(regexp) || is_slow_two_repeats(regexp); |
42 | 266 | } |
43 | | |
44 | | private: |
45 | 266 | bool is_slow_one_repeat(std::string_view regexp) const { |
46 | 266 | re2::StringPiece haystack(regexp.data(), regexp.size()); |
47 | 266 | re2::StringPiece matches[2]; |
48 | 266 | size_t start_pos = 0; |
49 | 267 | while (start_pos < haystack.size()) { |
50 | 266 | if (!_searcher_one_repeat.Match(haystack, start_pos, haystack.size(), |
51 | 266 | re2::RE2::Anchor::UNANCHORED, matches, 2)) { |
52 | 261 | break; |
53 | 261 | } |
54 | | |
55 | 5 | start_pos = matches[0].data() - haystack.data() + matches[0].size(); |
56 | 5 | if (is_larger_than_fifty({matches[1].data(), matches[1].size()})) { |
57 | 4 | return true; |
58 | 4 | } |
59 | 5 | } |
60 | 262 | return false; |
61 | 266 | } |
62 | | |
63 | 262 | bool is_slow_two_repeats(std::string_view regexp) const { |
64 | 262 | re2::StringPiece haystack(regexp.data(), regexp.size()); |
65 | 262 | re2::StringPiece matches[3]; |
66 | 262 | size_t start_pos = 0; |
67 | 263 | while (start_pos < haystack.size()) { |
68 | 262 | if (!_searcher_two_repeats.Match(haystack, start_pos, haystack.size(), |
69 | 262 | re2::RE2::Anchor::UNANCHORED, matches, 3)) { |
70 | 238 | break; |
71 | 238 | } |
72 | | |
73 | 24 | start_pos = matches[0].data() - haystack.data() + matches[0].size(); |
74 | 24 | if (is_larger_than_fifty({matches[1].data(), matches[1].size()}) || |
75 | 24 | is_larger_than_fifty({matches[2].data(), matches[2].size()})) { |
76 | 23 | return true; |
77 | 23 | } |
78 | 24 | } |
79 | 239 | return false; |
80 | 262 | } |
81 | | |
82 | | re2::RE2 _searcher_one_repeat; |
83 | | re2::RE2 _searcher_two_repeats; |
84 | | }; |
85 | | |
86 | | } // namespace |
87 | | |
88 | 266 | bool is_hyperscan_regexp_expensive(std::string_view regexp) { |
89 | 266 | static const SlowWithHyperscanChecker slow_with_hyperscan_checker; |
90 | 266 | return slow_with_hyperscan_checker.is_slow(regexp); |
91 | 266 | } |
92 | | |
93 | | } // namespace doris |