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 | | #include "common/compile_check_begin.h" |
27 | 252 | StringRef StringRef::trim() const { |
28 | | // Remove leading and trailing spaces. |
29 | 252 | int64_t begin = 0; |
30 | | |
31 | 253 | while (begin < size && data[begin] == ' ') { |
32 | 1 | ++begin; |
33 | 1 | } |
34 | | |
35 | 252 | int64_t end = size - 1; |
36 | | |
37 | 252 | while (end > begin && data[end] == ' ') { |
38 | 0 | --end; |
39 | 0 | } |
40 | | |
41 | 252 | return StringRef(data + begin, end - begin + 1); |
42 | 252 | } |
43 | | |
44 | 0 | StringRef StringRef::trim_tail_padding_zero() const { |
45 | | // Remove trailing padding zero. |
46 | 0 | int64_t end = size - 1; |
47 | |
|
48 | 0 | while (end >= 0 && data[end] == '\0') { |
49 | 0 | --end; |
50 | 0 | } |
51 | |
|
52 | 0 | return StringRef(data, end + 1); |
53 | 0 | } |
54 | | |
55 | 126 | StringRef StringRef::trim_whitespace() const { |
56 | | // Remove leading and trailing whitespace. |
57 | 126 | int64_t begin = 0; |
58 | | |
59 | 188 | while (begin < size && std::isspace(data[begin])) { |
60 | 62 | ++begin; |
61 | 62 | } |
62 | | |
63 | 126 | int64_t end = size - 1; |
64 | | |
65 | 126 | while (end > begin && std::isspace(data[end])) { |
66 | 0 | --end; |
67 | 0 | } |
68 | | |
69 | 126 | return StringRef(data + begin, end - begin + 1); |
70 | 126 | } |
71 | | |
72 | 124 | StringRef StringRef::trim_quote() const { |
73 | 124 | if (size < 2) { |
74 | 35 | return *this; |
75 | 35 | } |
76 | 89 | if (data[0] == '\'' && data[size - 1] == '\'') { |
77 | 2 | return StringRef(data + 1, size - 2); |
78 | 87 | } else if (data[0] == '"' && data[size - 1] == '"') { |
79 | 26 | return StringRef(data + 1, size - 2); |
80 | 26 | } |
81 | 61 | return *this; |
82 | 89 | } |
83 | | |
84 | | // TODO: rewrite in AVX2 |
85 | 152 | size_t StringRef::find_first_of(char c) const { |
86 | 152 | const char* p = static_cast<const char*>(memchr(data, c, size)); |
87 | 152 | return p == nullptr ? -1 : p - data; |
88 | 152 | } |
89 | | |
90 | 1 | StringRef StringRef::min_string_val() { |
91 | 1 | return StringRef((char*)(&StringRef::MIN_CHAR), 0); |
92 | 1 | } |
93 | | |
94 | 1 | StringRef StringRef::max_string_val() { |
95 | 1 | return StringRef((char*)(&StringRef::MAX_CHAR), 1); |
96 | 1 | } |
97 | | |
98 | 0 | bool StringRef::start_with(char ch) const { |
99 | 0 | if (UNLIKELY(size == 0)) { |
100 | 0 | return false; |
101 | 0 | } |
102 | 0 | return data[0] == ch; |
103 | 0 | } |
104 | 0 | bool StringRef::end_with(char ch) const { |
105 | 0 | if (UNLIKELY(size == 0)) { |
106 | 0 | return false; |
107 | 0 | } |
108 | 0 | return data[size - 1] == ch; |
109 | 0 | } |
110 | | |
111 | 49 | bool StringRef::start_with(const StringRef& search_string) const { |
112 | 49 | if (search_string.size == 0) { |
113 | 0 | return true; |
114 | 0 | } |
115 | | |
116 | 49 | if (UNLIKELY(size < search_string.size)) { |
117 | 0 | return false; |
118 | 0 | } |
119 | | |
120 | 49 | #if defined(__SSE2__) || defined(__aarch64__) |
121 | 49 | return memequalSSE2Wide(data, search_string.data, search_string.size); |
122 | | #else |
123 | | return 0 == memcmp(data, search_string.data, search_string.size); |
124 | | #endif |
125 | 49 | } |
126 | 65 | bool StringRef::end_with(const StringRef& search_string) const { |
127 | 65 | DCHECK(size >= search_string.size); |
128 | 65 | if (search_string.size == 0) { |
129 | 0 | return true; |
130 | 0 | } |
131 | | |
132 | 65 | #if defined(__SSE2__) || defined(__aarch64__) |
133 | 65 | return memequalSSE2Wide(data + size - search_string.size, search_string.data, |
134 | 65 | search_string.size); |
135 | | #else |
136 | | return 0 == memcmp(data + size - search_string.size, search_string.data, search_string.size); |
137 | | #endif |
138 | 65 | } |
139 | | #include "common/compile_check_end.h" |
140 | | } // namespace doris |