Coverage Report

Created: 2026-05-09 18:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/core/string_ref.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
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/StringRef.
19
// And modified by Doris
20
21
#include "core/string_ref.h"
22
23
#include "common/compiler_util.h" // IWYU pragma: keep
24
25
namespace doris {
26
252
StringRef StringRef::trim() const {
27
    // Remove leading and trailing spaces.
28
252
    int64_t begin = 0;
29
30
253
    while (begin < size && data[begin] == ' ') {
31
1
        ++begin;
32
1
    }
33
34
252
    int64_t end = size - 1;
35
36
252
    while (end > begin && data[end] == ' ') {
37
0
        --end;
38
0
    }
39
40
252
    return StringRef(data + begin, end - begin + 1);
41
252
}
42
43
0
StringRef StringRef::trim_tail_padding_zero() const {
44
    // Remove trailing padding zero.
45
0
    int64_t end = size - 1;
46
47
0
    while (end >= 0 && data[end] == '\0') {
48
0
        --end;
49
0
    }
50
51
0
    return StringRef(data, end + 1);
52
0
}
53
54
134
StringRef StringRef::trim_whitespace() const {
55
    // Remove leading and trailing whitespace.
56
134
    int64_t begin = 0;
57
58
196
    while (begin < size && std::isspace(data[begin])) {
59
62
        ++begin;
60
62
    }
61
62
134
    int64_t end = size - 1;
63
64
134
    while (end > begin && std::isspace(data[end])) {
65
0
        --end;
66
0
    }
67
68
134
    return StringRef(data + begin, end - begin + 1);
69
134
}
70
71
130
StringRef StringRef::trim_quote() const {
72
130
    if (size < 2) {
73
35
        return *this;
74
35
    }
75
95
    if (data[0] == '\'' && data[size - 1] == '\'') {
76
2
        return StringRef(data + 1, size - 2);
77
93
    } else if (data[0] == '"' && data[size - 1] == '"') {
78
32
        return StringRef(data + 1, size - 2);
79
32
    }
80
61
    return *this;
81
95
}
82
83
// TODO: rewrite in AVX2
84
152
size_t StringRef::find_first_of(char c) const {
85
152
    const char* p = static_cast<const char*>(memchr(data, c, size));
86
152
    return p == nullptr ? -1 : p - data;
87
152
}
88
89
0
StringRef StringRef::min_string_val() {
90
0
    return StringRef((char*)(&StringRef::MIN_CHAR), 0);
91
0
}
92
93
0
StringRef StringRef::max_string_val() {
94
0
    return StringRef((char*)(&StringRef::MAX_CHAR), 1);
95
0
}
96
97
49
bool StringRef::start_with(const StringRef& search_string) const {
98
49
    if (search_string.size == 0) {
99
0
        return true;
100
0
    }
101
102
49
    if (UNLIKELY(size < search_string.size)) {
103
0
        return false;
104
0
    }
105
106
49
#if defined(__SSE2__) || defined(__aarch64__)
107
49
    return memequalSSE2Wide(data, search_string.data, search_string.size);
108
#else
109
    return 0 == memcmp(data, search_string.data, search_string.size);
110
#endif
111
49
}
112
65
bool StringRef::end_with(const StringRef& search_string) const {
113
65
    DCHECK(size >= search_string.size);
114
65
    if (search_string.size == 0) {
115
0
        return true;
116
0
    }
117
118
65
#if defined(__SSE2__) || defined(__aarch64__)
119
65
    return memequalSSE2Wide(data + size - search_string.size, search_string.data,
120
65
                            search_string.size);
121
#else
122
    return 0 == memcmp(data + size - search_string.size, search_string.data, search_string.size);
123
#endif
124
65
}
125
} // namespace doris