Coverage Report

Created: 2025-09-11 18:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/runtime/string_search.hpp
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
#pragma once
19
20
#include <cstring>
21
22
#include "common/cast_set.h"
23
#include "vec/common/string_ref.h"
24
#include "vec/common/string_searcher.h"
25
26
namespace doris {
27
#include "common/compile_check_begin.h"
28
29
class StringSearch {
30
public:
31
1.47k
    virtual ~StringSearch() = default;
32
1.17k
    StringSearch() : _pattern(nullptr) {}
33
34
312
    StringSearch(const StringRef* pattern) { set_pattern(pattern); }
35
36
1.27k
    void set_pattern(const StringRef* pattern) {
37
1.27k
        _pattern = pattern;
38
1.27k
        _str_searcher.reset(new ASCIICaseSensitiveStringSearcher(pattern->data, pattern->size));
39
1.27k
    }
40
41
    // search for this pattern in str.
42
    //   Returns the offset into str if the pattern exists
43
    //   Returns -1 if the pattern is not found
44
807
    int search(const StringRef* str) const {
45
807
        const auto* it = search(str->data, str->size);
46
807
        if (it == str->data + str->size) {
47
371
            return -1;
48
436
        } else {
49
436
            return cast_set<int>((it - str->data));
50
436
        }
51
807
    }
52
53
27
    int search(const StringRef& str) const {
54
27
        const auto* it = search(str.data, str.size);
55
27
        if (it == str.data + str.size) {
56
11
            return -1;
57
16
        } else {
58
16
            return cast_set<int>(it - str.data);
59
16
        }
60
27
    }
61
62
    // search for this pattern in str.
63
    //   Returns the offset into str if the pattern exists
64
    //   Returns str+len if the pattern is not found
65
898
    const char* search(const char* str, size_t len) const {
66
898
        if (!str || !_pattern || _pattern->size == 0) {
67
0
            return str + len;
68
0
        }
69
70
898
        return _str_searcher->search(str, len);
71
898
    }
72
73
66
    inline size_t get_pattern_length() { return _pattern ? _pattern->size : 0; }
74
75
private:
76
    const StringRef* _pattern;
77
    std::unique_ptr<ASCIICaseSensitiveStringSearcher> _str_searcher;
78
};
79
#include "common/compile_check_end.h"
80
} // namespace doris