Coverage Report

Created: 2026-04-15 14:28

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
126
StringRef StringRef::trim_whitespace() const {
55
    // Remove leading and trailing whitespace.
56
126
    int64_t begin = 0;
57
58
188
    while (begin < size && std::isspace(data[begin])) {
59
62
        ++begin;
60
62
    }
61
62
126
    int64_t end = size - 1;
63
64
126
    while (end > begin && std::isspace(data[end])) {
65
0
        --end;
66
0
    }
67
68
126
    return StringRef(data + begin, end - begin + 1);
69
126
}
70
71
124
StringRef StringRef::trim_quote() const {
72
124
    if (size < 2) {
73
35
        return *this;
74
35
    }
75
89
    if (data[0] == '\'' && data[size - 1] == '\'') {
76
2
        return StringRef(data + 1, size - 2);
77
87
    } else if (data[0] == '"' && data[size - 1] == '"') {
78
26
        return StringRef(data + 1, size - 2);
79
26
    }
80
61
    return *this;
81
89
}
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
0
bool StringRef::start_with(char ch) const {
98
0
    if (UNLIKELY(size == 0)) {
99
0
        return false;
100
0
    }
101
0
    return data[0] == ch;
102
0
}
103
0
bool StringRef::end_with(char ch) const {
104
0
    if (UNLIKELY(size == 0)) {
105
0
        return false;
106
0
    }
107
0
    return data[size - 1] == ch;
108
0
}
109
110
49
bool StringRef::start_with(const StringRef& search_string) const {
111
49
    if (search_string.size == 0) {
112
0
        return true;
113
0
    }
114
115
49
    if (UNLIKELY(size < search_string.size)) {
116
0
        return false;
117
0
    }
118
119
49
#if defined(__SSE2__) || defined(__aarch64__)
120
49
    return memequalSSE2Wide(data, search_string.data, search_string.size);
121
#else
122
    return 0 == memcmp(data, search_string.data, search_string.size);
123
#endif
124
49
}
125
65
bool StringRef::end_with(const StringRef& search_string) const {
126
65
    DCHECK(size >= search_string.size);
127
65
    if (search_string.size == 0) {
128
0
        return true;
129
0
    }
130
131
65
#if defined(__SSE2__) || defined(__aarch64__)
132
65
    return memequalSSE2Wide(data + size - search_string.size, search_string.data,
133
65
                            search_string.size);
134
#else
135
    return 0 == memcmp(data + size - search_string.size, search_string.data, search_string.size);
136
#endif
137
65
}
138
} // namespace doris