1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker
15*9356374aSAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
16*9356374aSAndroid Build Coastguard Worker
17*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_USES_STD_STRING_VIEW
18*9356374aSAndroid Build Coastguard Worker
19*9356374aSAndroid Build Coastguard Worker #include <algorithm>
20*9356374aSAndroid Build Coastguard Worker #include <climits>
21*9356374aSAndroid Build Coastguard Worker #include <cstring>
22*9356374aSAndroid Build Coastguard Worker #include <ostream>
23*9356374aSAndroid Build Coastguard Worker
24*9356374aSAndroid Build Coastguard Worker #include "absl/base/nullability.h"
25*9356374aSAndroid Build Coastguard Worker
26*9356374aSAndroid Build Coastguard Worker namespace absl {
27*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
28*9356374aSAndroid Build Coastguard Worker
29*9356374aSAndroid Build Coastguard Worker namespace {
30*9356374aSAndroid Build Coastguard Worker
31*9356374aSAndroid Build Coastguard Worker // This is significantly faster for case-sensitive matches with very
32*9356374aSAndroid Build Coastguard Worker // few possible matches.
memmatch(absl::Nullable<const char * > phaystack,size_t haylen,absl::Nullable<const char * > pneedle,size_t neelen)33*9356374aSAndroid Build Coastguard Worker absl::Nullable<const char*> memmatch(absl::Nullable<const char*> phaystack,
34*9356374aSAndroid Build Coastguard Worker size_t haylen,
35*9356374aSAndroid Build Coastguard Worker absl::Nullable<const char*> pneedle,
36*9356374aSAndroid Build Coastguard Worker size_t neelen) {
37*9356374aSAndroid Build Coastguard Worker if (0 == neelen) {
38*9356374aSAndroid Build Coastguard Worker return phaystack; // even if haylen is 0
39*9356374aSAndroid Build Coastguard Worker }
40*9356374aSAndroid Build Coastguard Worker if (haylen < neelen) return nullptr;
41*9356374aSAndroid Build Coastguard Worker
42*9356374aSAndroid Build Coastguard Worker const char* match;
43*9356374aSAndroid Build Coastguard Worker const char* hayend = phaystack + haylen - neelen + 1;
44*9356374aSAndroid Build Coastguard Worker // A static cast is used here as memchr returns a const void *, and pointer
45*9356374aSAndroid Build Coastguard Worker // arithmetic is not allowed on pointers to void.
46*9356374aSAndroid Build Coastguard Worker while (
47*9356374aSAndroid Build Coastguard Worker (match = static_cast<const char*>(memchr(
48*9356374aSAndroid Build Coastguard Worker phaystack, pneedle[0], static_cast<size_t>(hayend - phaystack))))) {
49*9356374aSAndroid Build Coastguard Worker if (memcmp(match, pneedle, neelen) == 0)
50*9356374aSAndroid Build Coastguard Worker return match;
51*9356374aSAndroid Build Coastguard Worker else
52*9356374aSAndroid Build Coastguard Worker phaystack = match + 1;
53*9356374aSAndroid Build Coastguard Worker }
54*9356374aSAndroid Build Coastguard Worker return nullptr;
55*9356374aSAndroid Build Coastguard Worker }
56*9356374aSAndroid Build Coastguard Worker
WritePadding(std::ostream & o,size_t pad)57*9356374aSAndroid Build Coastguard Worker void WritePadding(std::ostream& o, size_t pad) {
58*9356374aSAndroid Build Coastguard Worker char fill_buf[32];
59*9356374aSAndroid Build Coastguard Worker memset(fill_buf, o.fill(), sizeof(fill_buf));
60*9356374aSAndroid Build Coastguard Worker while (pad) {
61*9356374aSAndroid Build Coastguard Worker size_t n = std::min(pad, sizeof(fill_buf));
62*9356374aSAndroid Build Coastguard Worker o.write(fill_buf, static_cast<std::streamsize>(n));
63*9356374aSAndroid Build Coastguard Worker pad -= n;
64*9356374aSAndroid Build Coastguard Worker }
65*9356374aSAndroid Build Coastguard Worker }
66*9356374aSAndroid Build Coastguard Worker
67*9356374aSAndroid Build Coastguard Worker class LookupTable {
68*9356374aSAndroid Build Coastguard Worker public:
69*9356374aSAndroid Build Coastguard Worker // For each character in wanted, sets the index corresponding
70*9356374aSAndroid Build Coastguard Worker // to the ASCII code of that character. This is used by
71*9356374aSAndroid Build Coastguard Worker // the find_.*_of methods below to tell whether or not a character is in
72*9356374aSAndroid Build Coastguard Worker // the lookup table in constant time.
LookupTable(string_view wanted)73*9356374aSAndroid Build Coastguard Worker explicit LookupTable(string_view wanted) {
74*9356374aSAndroid Build Coastguard Worker for (char c : wanted) {
75*9356374aSAndroid Build Coastguard Worker table_[Index(c)] = true;
76*9356374aSAndroid Build Coastguard Worker }
77*9356374aSAndroid Build Coastguard Worker }
operator [](char c) const78*9356374aSAndroid Build Coastguard Worker bool operator[](char c) const { return table_[Index(c)]; }
79*9356374aSAndroid Build Coastguard Worker
80*9356374aSAndroid Build Coastguard Worker private:
Index(char c)81*9356374aSAndroid Build Coastguard Worker static unsigned char Index(char c) { return static_cast<unsigned char>(c); }
82*9356374aSAndroid Build Coastguard Worker bool table_[UCHAR_MAX + 1] = {};
83*9356374aSAndroid Build Coastguard Worker };
84*9356374aSAndroid Build Coastguard Worker
85*9356374aSAndroid Build Coastguard Worker } // namespace
86*9356374aSAndroid Build Coastguard Worker
operator <<(std::ostream & o,string_view piece)87*9356374aSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& o, string_view piece) {
88*9356374aSAndroid Build Coastguard Worker std::ostream::sentry sentry(o);
89*9356374aSAndroid Build Coastguard Worker if (sentry) {
90*9356374aSAndroid Build Coastguard Worker size_t lpad = 0;
91*9356374aSAndroid Build Coastguard Worker size_t rpad = 0;
92*9356374aSAndroid Build Coastguard Worker if (static_cast<size_t>(o.width()) > piece.size()) {
93*9356374aSAndroid Build Coastguard Worker size_t pad = static_cast<size_t>(o.width()) - piece.size();
94*9356374aSAndroid Build Coastguard Worker if ((o.flags() & o.adjustfield) == o.left) {
95*9356374aSAndroid Build Coastguard Worker rpad = pad;
96*9356374aSAndroid Build Coastguard Worker } else {
97*9356374aSAndroid Build Coastguard Worker lpad = pad;
98*9356374aSAndroid Build Coastguard Worker }
99*9356374aSAndroid Build Coastguard Worker }
100*9356374aSAndroid Build Coastguard Worker if (lpad) WritePadding(o, lpad);
101*9356374aSAndroid Build Coastguard Worker o.write(piece.data(), static_cast<std::streamsize>(piece.size()));
102*9356374aSAndroid Build Coastguard Worker if (rpad) WritePadding(o, rpad);
103*9356374aSAndroid Build Coastguard Worker o.width(0);
104*9356374aSAndroid Build Coastguard Worker }
105*9356374aSAndroid Build Coastguard Worker return o;
106*9356374aSAndroid Build Coastguard Worker }
107*9356374aSAndroid Build Coastguard Worker
find(string_view s,size_type pos) const108*9356374aSAndroid Build Coastguard Worker string_view::size_type string_view::find(string_view s,
109*9356374aSAndroid Build Coastguard Worker size_type pos) const noexcept {
110*9356374aSAndroid Build Coastguard Worker if (empty() || pos > length_) {
111*9356374aSAndroid Build Coastguard Worker if (empty() && pos == 0 && s.empty()) return 0;
112*9356374aSAndroid Build Coastguard Worker return npos;
113*9356374aSAndroid Build Coastguard Worker }
114*9356374aSAndroid Build Coastguard Worker const char* result = memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_);
115*9356374aSAndroid Build Coastguard Worker return result ? static_cast<size_type>(result - ptr_) : npos;
116*9356374aSAndroid Build Coastguard Worker }
117*9356374aSAndroid Build Coastguard Worker
find(char c,size_type pos) const118*9356374aSAndroid Build Coastguard Worker string_view::size_type string_view::find(char c, size_type pos) const noexcept {
119*9356374aSAndroid Build Coastguard Worker if (empty() || pos >= length_) {
120*9356374aSAndroid Build Coastguard Worker return npos;
121*9356374aSAndroid Build Coastguard Worker }
122*9356374aSAndroid Build Coastguard Worker const char* result =
123*9356374aSAndroid Build Coastguard Worker static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos));
124*9356374aSAndroid Build Coastguard Worker return result != nullptr ? static_cast<size_type>(result - ptr_) : npos;
125*9356374aSAndroid Build Coastguard Worker }
126*9356374aSAndroid Build Coastguard Worker
rfind(string_view s,size_type pos) const127*9356374aSAndroid Build Coastguard Worker string_view::size_type string_view::rfind(string_view s,
128*9356374aSAndroid Build Coastguard Worker size_type pos) const noexcept {
129*9356374aSAndroid Build Coastguard Worker if (length_ < s.length_) return npos;
130*9356374aSAndroid Build Coastguard Worker if (s.empty()) return std::min(length_, pos);
131*9356374aSAndroid Build Coastguard Worker const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
132*9356374aSAndroid Build Coastguard Worker const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
133*9356374aSAndroid Build Coastguard Worker return result != last ? static_cast<size_type>(result - ptr_) : npos;
134*9356374aSAndroid Build Coastguard Worker }
135*9356374aSAndroid Build Coastguard Worker
136*9356374aSAndroid Build Coastguard Worker // Search range is [0..pos] inclusive. If pos == npos, search everything.
rfind(char c,size_type pos) const137*9356374aSAndroid Build Coastguard Worker string_view::size_type string_view::rfind(char c,
138*9356374aSAndroid Build Coastguard Worker size_type pos) const noexcept {
139*9356374aSAndroid Build Coastguard Worker // Note: memrchr() is not available on Windows.
140*9356374aSAndroid Build Coastguard Worker if (empty()) return npos;
141*9356374aSAndroid Build Coastguard Worker for (size_type i = std::min(pos, length_ - 1);; --i) {
142*9356374aSAndroid Build Coastguard Worker if (ptr_[i] == c) {
143*9356374aSAndroid Build Coastguard Worker return i;
144*9356374aSAndroid Build Coastguard Worker }
145*9356374aSAndroid Build Coastguard Worker if (i == 0) break;
146*9356374aSAndroid Build Coastguard Worker }
147*9356374aSAndroid Build Coastguard Worker return npos;
148*9356374aSAndroid Build Coastguard Worker }
149*9356374aSAndroid Build Coastguard Worker
find_first_of(string_view s,size_type pos) const150*9356374aSAndroid Build Coastguard Worker string_view::size_type string_view::find_first_of(
151*9356374aSAndroid Build Coastguard Worker string_view s, size_type pos) const noexcept {
152*9356374aSAndroid Build Coastguard Worker if (empty() || s.empty()) {
153*9356374aSAndroid Build Coastguard Worker return npos;
154*9356374aSAndroid Build Coastguard Worker }
155*9356374aSAndroid Build Coastguard Worker // Avoid the cost of LookupTable() for a single-character search.
156*9356374aSAndroid Build Coastguard Worker if (s.length_ == 1) return find_first_of(s.ptr_[0], pos);
157*9356374aSAndroid Build Coastguard Worker LookupTable tbl(s);
158*9356374aSAndroid Build Coastguard Worker for (size_type i = pos; i < length_; ++i) {
159*9356374aSAndroid Build Coastguard Worker if (tbl[ptr_[i]]) {
160*9356374aSAndroid Build Coastguard Worker return i;
161*9356374aSAndroid Build Coastguard Worker }
162*9356374aSAndroid Build Coastguard Worker }
163*9356374aSAndroid Build Coastguard Worker return npos;
164*9356374aSAndroid Build Coastguard Worker }
165*9356374aSAndroid Build Coastguard Worker
find_first_not_of(string_view s,size_type pos) const166*9356374aSAndroid Build Coastguard Worker string_view::size_type string_view::find_first_not_of(
167*9356374aSAndroid Build Coastguard Worker string_view s, size_type pos) const noexcept {
168*9356374aSAndroid Build Coastguard Worker if (empty()) return npos;
169*9356374aSAndroid Build Coastguard Worker // Avoid the cost of LookupTable() for a single-character search.
170*9356374aSAndroid Build Coastguard Worker if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos);
171*9356374aSAndroid Build Coastguard Worker LookupTable tbl(s);
172*9356374aSAndroid Build Coastguard Worker for (size_type i = pos; i < length_; ++i) {
173*9356374aSAndroid Build Coastguard Worker if (!tbl[ptr_[i]]) {
174*9356374aSAndroid Build Coastguard Worker return i;
175*9356374aSAndroid Build Coastguard Worker }
176*9356374aSAndroid Build Coastguard Worker }
177*9356374aSAndroid Build Coastguard Worker return npos;
178*9356374aSAndroid Build Coastguard Worker }
179*9356374aSAndroid Build Coastguard Worker
find_first_not_of(char c,size_type pos) const180*9356374aSAndroid Build Coastguard Worker string_view::size_type string_view::find_first_not_of(
181*9356374aSAndroid Build Coastguard Worker char c, size_type pos) const noexcept {
182*9356374aSAndroid Build Coastguard Worker if (empty()) return npos;
183*9356374aSAndroid Build Coastguard Worker for (; pos < length_; ++pos) {
184*9356374aSAndroid Build Coastguard Worker if (ptr_[pos] != c) {
185*9356374aSAndroid Build Coastguard Worker return pos;
186*9356374aSAndroid Build Coastguard Worker }
187*9356374aSAndroid Build Coastguard Worker }
188*9356374aSAndroid Build Coastguard Worker return npos;
189*9356374aSAndroid Build Coastguard Worker }
190*9356374aSAndroid Build Coastguard Worker
find_last_of(string_view s,size_type pos) const191*9356374aSAndroid Build Coastguard Worker string_view::size_type string_view::find_last_of(string_view s,
192*9356374aSAndroid Build Coastguard Worker size_type pos) const noexcept {
193*9356374aSAndroid Build Coastguard Worker if (empty() || s.empty()) return npos;
194*9356374aSAndroid Build Coastguard Worker // Avoid the cost of LookupTable() for a single-character search.
195*9356374aSAndroid Build Coastguard Worker if (s.length_ == 1) return find_last_of(s.ptr_[0], pos);
196*9356374aSAndroid Build Coastguard Worker LookupTable tbl(s);
197*9356374aSAndroid Build Coastguard Worker for (size_type i = std::min(pos, length_ - 1);; --i) {
198*9356374aSAndroid Build Coastguard Worker if (tbl[ptr_[i]]) {
199*9356374aSAndroid Build Coastguard Worker return i;
200*9356374aSAndroid Build Coastguard Worker }
201*9356374aSAndroid Build Coastguard Worker if (i == 0) break;
202*9356374aSAndroid Build Coastguard Worker }
203*9356374aSAndroid Build Coastguard Worker return npos;
204*9356374aSAndroid Build Coastguard Worker }
205*9356374aSAndroid Build Coastguard Worker
find_last_not_of(string_view s,size_type pos) const206*9356374aSAndroid Build Coastguard Worker string_view::size_type string_view::find_last_not_of(
207*9356374aSAndroid Build Coastguard Worker string_view s, size_type pos) const noexcept {
208*9356374aSAndroid Build Coastguard Worker if (empty()) return npos;
209*9356374aSAndroid Build Coastguard Worker size_type i = std::min(pos, length_ - 1);
210*9356374aSAndroid Build Coastguard Worker if (s.empty()) return i;
211*9356374aSAndroid Build Coastguard Worker // Avoid the cost of LookupTable() for a single-character search.
212*9356374aSAndroid Build Coastguard Worker if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos);
213*9356374aSAndroid Build Coastguard Worker LookupTable tbl(s);
214*9356374aSAndroid Build Coastguard Worker for (;; --i) {
215*9356374aSAndroid Build Coastguard Worker if (!tbl[ptr_[i]]) {
216*9356374aSAndroid Build Coastguard Worker return i;
217*9356374aSAndroid Build Coastguard Worker }
218*9356374aSAndroid Build Coastguard Worker if (i == 0) break;
219*9356374aSAndroid Build Coastguard Worker }
220*9356374aSAndroid Build Coastguard Worker return npos;
221*9356374aSAndroid Build Coastguard Worker }
222*9356374aSAndroid Build Coastguard Worker
find_last_not_of(char c,size_type pos) const223*9356374aSAndroid Build Coastguard Worker string_view::size_type string_view::find_last_not_of(
224*9356374aSAndroid Build Coastguard Worker char c, size_type pos) const noexcept {
225*9356374aSAndroid Build Coastguard Worker if (empty()) return npos;
226*9356374aSAndroid Build Coastguard Worker size_type i = std::min(pos, length_ - 1);
227*9356374aSAndroid Build Coastguard Worker for (;; --i) {
228*9356374aSAndroid Build Coastguard Worker if (ptr_[i] != c) {
229*9356374aSAndroid Build Coastguard Worker return i;
230*9356374aSAndroid Build Coastguard Worker }
231*9356374aSAndroid Build Coastguard Worker if (i == 0) break;
232*9356374aSAndroid Build Coastguard Worker }
233*9356374aSAndroid Build Coastguard Worker return npos;
234*9356374aSAndroid Build Coastguard Worker }
235*9356374aSAndroid Build Coastguard Worker
236*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
237*9356374aSAndroid Build Coastguard Worker constexpr string_view::size_type string_view::npos;
238*9356374aSAndroid Build Coastguard Worker constexpr string_view::size_type string_view::kMaxSize;
239*9356374aSAndroid Build Coastguard Worker #endif
240*9356374aSAndroid Build Coastguard Worker
241*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
242*9356374aSAndroid Build Coastguard Worker } // namespace absl
243*9356374aSAndroid Build Coastguard Worker
244*9356374aSAndroid Build Coastguard Worker #else
245*9356374aSAndroid Build Coastguard Worker
246*9356374aSAndroid Build Coastguard Worker // https://github.com/abseil/abseil-cpp/issues/1465
247*9356374aSAndroid Build Coastguard Worker // CMake builds on Apple platforms error when libraries are empty.
248*9356374aSAndroid Build Coastguard Worker // Our CMake configuration can avoid this error on header-only libraries,
249*9356374aSAndroid Build Coastguard Worker // but since this library is conditionally empty, including a single
250*9356374aSAndroid Build Coastguard Worker // variable is an easy workaround.
251*9356374aSAndroid Build Coastguard Worker #ifdef __APPLE__
252*9356374aSAndroid Build Coastguard Worker namespace absl {
253*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
254*9356374aSAndroid Build Coastguard Worker namespace strings_internal {
255*9356374aSAndroid Build Coastguard Worker extern const char kAvoidEmptyStringViewLibraryWarning;
256*9356374aSAndroid Build Coastguard Worker const char kAvoidEmptyStringViewLibraryWarning = 0;
257*9356374aSAndroid Build Coastguard Worker } // namespace strings_internal
258*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
259*9356374aSAndroid Build Coastguard Worker } // namespace absl
260*9356374aSAndroid Build Coastguard Worker #endif // __APPLE__
261*9356374aSAndroid Build Coastguard Worker
262*9356374aSAndroid Build Coastguard Worker #endif // ABSL_USES_STD_STRING_VIEW
263