forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_view.cc
212 lines (175 loc) · 5.34 KB
/
string_view.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/string_view.h"
#include <string.h>
#include <algorithm>
#include <limits>
namespace fml {
constexpr size_t StringView::npos;
namespace {
// For each character in characters_wanted, sets the index corresponding
// to the code for that character to 1 in table. This is used by the
// find_.*_of methods below to tell whether or not a character is in the lookup
// table in constant time.
// The argument `table' must be an array that is large enough to hold all
// the possible values of an unsigned char. Thus it should be be declared
// as follows:
// bool table[std::numeric_limits<unsigned char>::max() + 1]
inline void BuildLookupTable(StringView characters_wanted, bool* table) {
size_t length = characters_wanted.size();
const char* data = characters_wanted.data();
for (size_t i = 0; i < length; ++i) {
table[static_cast<unsigned char>(data[i])] = true;
}
}
} // namespace
int StringView::compare(StringView other) {
size_t len = std::min(size_, other.size_);
int retval = memcmp(data_, other.data_, len);
if (retval == 0) {
if (size_ == other.size_) {
return 0;
}
return size_ < other.size_ ? -1 : 1;
}
return retval;
}
bool operator==(StringView lhs, StringView rhs) {
if (lhs.size() != rhs.size())
return false;
return lhs.compare(rhs) == 0;
}
bool operator!=(StringView lhs, StringView rhs) {
if (lhs.size() != rhs.size())
return true;
return lhs.compare(rhs) != 0;
}
bool operator<(StringView lhs, StringView rhs) {
return lhs.compare(rhs) < 0;
}
bool operator>(StringView lhs, StringView rhs) {
return lhs.compare(rhs) > 0;
}
bool operator<=(StringView lhs, StringView rhs) {
return lhs.compare(rhs) <= 0;
}
bool operator>=(StringView lhs, StringView rhs) {
return lhs.compare(rhs) >= 0;
}
std::ostream& operator<<(std::ostream& o, StringView string_view) {
o.write(string_view.data(), static_cast<std::streamsize>(string_view.size()));
return o;
}
size_t StringView::find(StringView s, size_t pos) const {
if (pos > size_)
return npos;
if (s.empty())
return pos;
auto* result = std::search(begin() + pos, end(), s.begin(), s.end());
if (result == end())
return npos;
return result - begin();
}
size_t StringView::find(char c, size_t pos) const {
if (pos > size_)
return npos;
auto* result = std::find(begin() + pos, end(), c);
if (result == end())
return npos;
return result - begin();
}
size_t StringView::rfind(StringView s, size_t pos) const {
if (size_ < s.size())
return npos;
if (s.empty())
return std::min(pos, size_);
auto* last = begin() + std::min(size_ - s.size(), pos) + s.size();
auto* result = std::find_end(begin(), last, s.begin(), s.end());
if (result == last)
return npos;
return result - begin();
}
size_t StringView::rfind(char c, size_t pos) const {
if (size_ == 0)
return npos;
auto begin = rend() - std::min(size_ - 1, pos) - 1;
auto result = std::find(begin, rend(), c);
if (result == rend())
return npos;
return rend() - result - 1;
}
size_t StringView::find_first_of(StringView s, size_t pos) const {
if (pos >= size_ || s.size() == 0)
return npos;
// Avoid the cost of BuildLookupTable() for a single-character search.
if (s.size() == 1)
return find(s.data()[0], pos);
bool lookup[std::numeric_limits<unsigned char>::max() + 1] = {false};
BuildLookupTable(s, lookup);
for (size_t i = pos; i < size_; ++i) {
if (lookup[static_cast<unsigned char>(data_[i])]) {
return i;
}
}
return npos;
}
size_t StringView::find_last_of(StringView s, size_t pos) const {
if (size_ == 0 || s.size() == 0)
return npos;
// Avoid the cost of BuildLookupTable() for a single-character search.
if (s.size() == 1)
return rfind(s.data()[0], pos);
bool lookup[std::numeric_limits<unsigned char>::max() + 1] = {false};
BuildLookupTable(s, lookup);
for (size_t i = std::min(pos, size_ - 1);; --i) {
if (lookup[static_cast<unsigned char>(data_[i])])
return i;
if (i == 0)
break;
}
return npos;
}
size_t StringView::find_first_not_of(StringView s, size_t pos) const {
if (pos >= size_)
return npos;
// Avoid the cost of BuildLookupTable() for a single-character search.
if (s.size() == 1) {
for (size_t i = pos; i < size_; ++i) {
if (data_[i] != s[0])
return i;
}
return npos;
}
bool lookup[std::numeric_limits<unsigned char>::max() + 1] = {false};
BuildLookupTable(s, lookup);
for (size_t i = pos; i < size_; ++i) {
if (!lookup[static_cast<unsigned char>(data_[i])]) {
return i;
}
}
return npos;
}
size_t StringView::find_last_not_of(StringView s, size_t pos) const {
if (size_ == 0)
return npos;
// Avoid the cost of BuildLookupTable() for a single-character search.
if (s.size() == 1) {
for (size_t i = std::min(pos, size_ - 1);; --i) {
if (data_[i] != s[0])
return i;
if (i == 0)
break;
}
}
bool lookup[std::numeric_limits<unsigned char>::max() + 1] = {false};
BuildLookupTable(s, lookup);
for (size_t i = std::min(pos, size_ - 1);; --i) {
if (!lookup[static_cast<unsigned char>(data_[i])])
return i;
if (i == 0)
break;
}
return npos;
}
} // namespace fml