/root/doris/be/src/gutil/strings/stringpiece.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2004 and onwards Google Inc. |
2 | | // |
3 | | // |
4 | | |
5 | | #include "gutil/strings/stringpiece.h" |
6 | | |
7 | | // IWYU pragma: no_include <pstl/glue_algorithm_defs.h> |
8 | | |
9 | | #include "common/logging.h" |
10 | | #include <string.h> |
11 | | #include <algorithm> |
12 | | #include <climits> |
13 | | #include <string> |
14 | | #include <deque> |
15 | | #include <ostream> |
16 | | |
17 | | #include "gutil/stl_util.h" |
18 | | #include "gutil/strings/memutil.h" |
19 | | |
20 | | using std::copy; |
21 | | using std::max; |
22 | | using std::min; |
23 | | using std::reverse; |
24 | | using std::sort; |
25 | | using std::swap; |
26 | | using std::string; |
27 | | |
28 | 0 | std::ostream& operator<<(std::ostream& o, StringPiece piece) { |
29 | 0 | o.write(piece.data(), piece.size()); |
30 | 0 | return o; |
31 | 0 | } |
32 | | |
33 | 0 | StringPiece::StringPiece(StringPiece x, int pos) : ptr_(x.ptr_ + pos), length_(x.length_ - pos) { |
34 | 0 | DCHECK_LE(0, pos); |
35 | 0 | DCHECK_LE(pos, x.length_); |
36 | 0 | } |
37 | | |
38 | | StringPiece::StringPiece(StringPiece x, int pos, int len) |
39 | 0 | : ptr_(x.ptr_ + pos), length_(min(len, x.length_ - pos)) { |
40 | 0 | DCHECK_LE(0, pos); |
41 | 0 | DCHECK_LE(pos, x.length_); |
42 | 0 | DCHECK_GE(len, 0); |
43 | 0 | } |
44 | | |
45 | 0 | void StringPiece::CopyToString(string* target) const { |
46 | 0 | STLAssignToString(target, ptr_, length_); |
47 | 0 | } |
48 | | |
49 | 0 | void StringPiece::AppendToString(string* target) const { |
50 | 0 | STLAppendToString(target, ptr_, length_); |
51 | 0 | } |
52 | | |
53 | 0 | int StringPiece::copy(char* buf, size_type n, size_type pos) const { |
54 | 0 | int ret = min(length_ - pos, n); |
55 | 0 | memcpy(buf, ptr_ + pos, ret); |
56 | 0 | return ret; |
57 | 0 | } |
58 | | |
59 | 0 | bool StringPiece::contains(StringPiece s) const { |
60 | 0 | return find(s, 0) != npos; |
61 | 0 | } |
62 | | |
63 | 3.00k | int StringPiece::find(StringPiece s, size_type pos) const { |
64 | 3.00k | if (length_ <= 0 || pos > static_cast<size_type>(length_)) { |
65 | 44 | if (length_ == 0 && pos == 0 && s.length_ == 0) return 0; |
66 | 44 | return npos; |
67 | 44 | } |
68 | 2.95k | const char* result = memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_); |
69 | 2.95k | return result ? result - ptr_ : npos; |
70 | 3.00k | } |
71 | | |
72 | 0 | int StringPiece::find(char c, size_type pos) const { |
73 | 0 | if (length_ <= 0 || pos >= static_cast<size_type>(length_)) { |
74 | 0 | return npos; |
75 | 0 | } |
76 | 0 | const char* result = static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos)); |
77 | 0 | return result != nullptr ? result - ptr_ : npos; |
78 | 0 | } |
79 | | |
80 | 0 | int StringPiece::rfind(StringPiece s, size_type pos) const { |
81 | 0 | if (length_ < s.length_) return npos; |
82 | 0 | const size_t ulen = length_; |
83 | 0 | if (s.length_ == 0) return min(ulen, pos); |
84 | | |
85 | 0 | const char* last = ptr_ + min(ulen - s.length_, pos) + s.length_; |
86 | 0 | const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); |
87 | 0 | return result != last ? result - ptr_ : npos; |
88 | 0 | } |
89 | | |
90 | | // Search range is [0..pos] inclusive. If pos == npos, search everything. |
91 | 0 | int StringPiece::rfind(char c, size_type pos) const { |
92 | | // Note: memrchr() is not available on Windows. |
93 | 0 | if (length_ <= 0) return npos; |
94 | 0 | for (int i = min(pos, static_cast<size_type>(length_ - 1)); i >= 0; --i) { |
95 | 0 | if (ptr_[i] == c) { |
96 | 0 | return i; |
97 | 0 | } |
98 | 0 | } |
99 | 0 | return npos; |
100 | 0 | } |
101 | | |
102 | | // For each character in characters_wanted, sets the index corresponding |
103 | | // to the ASCII code of that character to 1 in table. This is used by |
104 | | // the find_.*_of methods below to tell whether or not a character is in |
105 | | // the lookup table in constant time. |
106 | | // The argument `table' must be an array that is large enough to hold all |
107 | | // the possible values of an unsigned char. Thus it should be be declared |
108 | | // as follows: |
109 | | // bool table[UCHAR_MAX + 1] |
110 | 0 | static inline void BuildLookupTable(StringPiece characters_wanted, bool* table) { |
111 | 0 | const int length = characters_wanted.length(); |
112 | 0 | const char* const data = characters_wanted.data(); |
113 | 0 | for (int i = 0; i < length; ++i) { |
114 | 0 | table[static_cast<unsigned char>(data[i])] = true; |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | 0 | int StringPiece::find_first_of(StringPiece s, size_type pos) const { |
119 | 0 | if (length_ <= 0 || s.length_ <= 0) { |
120 | 0 | return npos; |
121 | 0 | } |
122 | | // Avoid the cost of BuildLookupTable() for a single-character search. |
123 | 0 | if (s.length_ == 1) return find_first_of(s.ptr_[0], pos); |
124 | | |
125 | 0 | bool lookup[UCHAR_MAX + 1] = {false}; |
126 | 0 | BuildLookupTable(s, lookup); |
127 | 0 | for (int i = pos; i < length_; ++i) { |
128 | 0 | if (lookup[static_cast<unsigned char>(ptr_[i])]) { |
129 | 0 | return i; |
130 | 0 | } |
131 | 0 | } |
132 | 0 | return npos; |
133 | 0 | } |
134 | | |
135 | 0 | int StringPiece::find_first_not_of(StringPiece s, size_type pos) const { |
136 | 0 | if (length_ <= 0) return npos; |
137 | 0 | if (s.length_ <= 0) return 0; |
138 | | // Avoid the cost of BuildLookupTable() for a single-character search. |
139 | 0 | if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos); |
140 | | |
141 | 0 | bool lookup[UCHAR_MAX + 1] = {false}; |
142 | 0 | BuildLookupTable(s, lookup); |
143 | 0 | for (int i = pos; i < length_; ++i) { |
144 | 0 | if (!lookup[static_cast<unsigned char>(ptr_[i])]) { |
145 | 0 | return i; |
146 | 0 | } |
147 | 0 | } |
148 | 0 | return npos; |
149 | 0 | } |
150 | | |
151 | 0 | int StringPiece::find_first_not_of(char c, size_type pos) const { |
152 | 0 | if (length_ <= 0) return npos; |
153 | | |
154 | 0 | for (; pos < static_cast<size_type>(length_); ++pos) { |
155 | 0 | if (ptr_[pos] != c) { |
156 | 0 | return pos; |
157 | 0 | } |
158 | 0 | } |
159 | 0 | return npos; |
160 | 0 | } |
161 | | |
162 | 0 | int StringPiece::find_last_of(StringPiece s, size_type pos) const { |
163 | 0 | if (length_ <= 0 || s.length_ <= 0) return npos; |
164 | | // Avoid the cost of BuildLookupTable() for a single-character search. |
165 | 0 | if (s.length_ == 1) return find_last_of(s.ptr_[0], pos); |
166 | | |
167 | 0 | bool lookup[UCHAR_MAX + 1] = {false}; |
168 | 0 | BuildLookupTable(s, lookup); |
169 | 0 | for (int i = min(pos, static_cast<size_type>(length_ - 1)); i >= 0; --i) { |
170 | 0 | if (lookup[static_cast<unsigned char>(ptr_[i])]) { |
171 | 0 | return i; |
172 | 0 | } |
173 | 0 | } |
174 | 0 | return npos; |
175 | 0 | } |
176 | | |
177 | 0 | int StringPiece::find_last_not_of(StringPiece s, size_type pos) const { |
178 | 0 | if (length_ <= 0) return npos; |
179 | | |
180 | 0 | int i = min(pos, static_cast<size_type>(length_ - 1)); |
181 | 0 | if (s.length_ <= 0) return i; |
182 | | |
183 | | // Avoid the cost of BuildLookupTable() for a single-character search. |
184 | 0 | if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos); |
185 | | |
186 | 0 | bool lookup[UCHAR_MAX + 1] = {false}; |
187 | 0 | BuildLookupTable(s, lookup); |
188 | 0 | for (; i >= 0; --i) { |
189 | 0 | if (!lookup[static_cast<unsigned char>(ptr_[i])]) { |
190 | 0 | return i; |
191 | 0 | } |
192 | 0 | } |
193 | 0 | return npos; |
194 | 0 | } |
195 | | |
196 | 0 | int StringPiece::find_last_not_of(char c, size_type pos) const { |
197 | 0 | if (length_ <= 0) return npos; |
198 | | |
199 | 0 | for (int i = min(pos, static_cast<size_type>(length_ - 1)); i >= 0; --i) { |
200 | 0 | if (ptr_[i] != c) { |
201 | 0 | return i; |
202 | 0 | } |
203 | 0 | } |
204 | 0 | return npos; |
205 | 0 | } |
206 | | |
207 | 0 | StringPiece StringPiece::substr(size_type pos, size_type n) const { |
208 | 0 | if (pos > length_) pos = length_; |
209 | 0 | if (n > length_ - pos) n = length_ - pos; |
210 | 0 | return StringPiece(ptr_ + pos, n); |
211 | 0 | } |
212 | | |
213 | | const StringPiece::size_type StringPiece::npos = size_type(-1); |