1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/string_view.h"
16 
17 #ifndef ABSL_USES_STD_STRING_VIEW
18 
19 #include <algorithm>
20 #include <climits>
21 #include <cstring>
22 #include <ostream>
23 
24 #include "absl/strings/internal/memutil.h"
25 
26 namespace absl {
27 ABSL_NAMESPACE_BEGIN
28 
29 namespace {
WritePadding(std::ostream & o,size_t pad)30 void WritePadding(std::ostream& o, size_t pad) {
31   char fill_buf[32];
32   memset(fill_buf, o.fill(), sizeof(fill_buf));
33   while (pad) {
34     size_t n = std::min(pad, sizeof(fill_buf));
35     o.write(fill_buf, n);
36     pad -= n;
37   }
38 }
39 
40 class LookupTable {
41  public:
42   // For each character in wanted, sets the index corresponding
43   // to the ASCII code of that character. This is used by
44   // the find_.*_of methods below to tell whether or not a character is in
45   // the lookup table in constant time.
LookupTable(string_view wanted)46   explicit LookupTable(string_view wanted) {
47     for (char c : wanted) {
48       table_[Index(c)] = true;
49     }
50   }
operator [](char c) const51   bool operator[](char c) const { return table_[Index(c)]; }
52 
53  private:
Index(char c)54   static unsigned char Index(char c) { return static_cast<unsigned char>(c); }
55   bool table_[UCHAR_MAX + 1] = {};
56 };
57 
58 }  // namespace
59 
operator <<(std::ostream & o,string_view piece)60 std::ostream& operator<<(std::ostream& o, string_view piece) {
61   std::ostream::sentry sentry(o);
62   if (sentry) {
63     size_t lpad = 0;
64     size_t rpad = 0;
65     if (static_cast<size_t>(o.width()) > piece.size()) {
66       size_t pad = o.width() - piece.size();
67       if ((o.flags() & o.adjustfield) == o.left) {
68         rpad = pad;
69       } else {
70         lpad = pad;
71       }
72     }
73     if (lpad) WritePadding(o, lpad);
74     o.write(piece.data(), piece.size());
75     if (rpad) WritePadding(o, rpad);
76     o.width(0);
77   }
78   return o;
79 }
80 
find(string_view s,size_type pos) const81 string_view::size_type string_view::find(string_view s, size_type pos) const
82     noexcept {
83   if (empty() || pos > length_) {
84     if (empty() && pos == 0 && s.empty()) return 0;
85     return npos;
86   }
87   const char* result =
88       strings_internal::memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_);
89   return result ? result - ptr_ : npos;
90 }
91 
find(char c,size_type pos) const92 string_view::size_type string_view::find(char c, size_type pos) const noexcept {
93   if (empty() || pos >= length_) {
94     return npos;
95   }
96   const char* result =
97       static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos));
98   return result != nullptr ? result - ptr_ : npos;
99 }
100 
rfind(string_view s,size_type pos) const101 string_view::size_type string_view::rfind(string_view s, size_type pos) const
102     noexcept {
103   if (length_ < s.length_) return npos;
104   if (s.empty()) return std::min(length_, pos);
105   const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
106   const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
107   return result != last ? result - ptr_ : npos;
108 }
109 
110 // Search range is [0..pos] inclusive.  If pos == npos, search everything.
rfind(char c,size_type pos) const111 string_view::size_type string_view::rfind(char c, size_type pos) const
112     noexcept {
113   // Note: memrchr() is not available on Windows.
114   if (empty()) return npos;
115   for (size_type i = std::min(pos, length_ - 1);; --i) {
116     if (ptr_[i] == c) {
117       return i;
118     }
119     if (i == 0) break;
120   }
121   return npos;
122 }
123 
find_first_of(string_view s,size_type pos) const124 string_view::size_type string_view::find_first_of(string_view s,
125                                                   size_type pos) const
126     noexcept {
127   if (empty() || s.empty()) {
128     return npos;
129   }
130   // Avoid the cost of LookupTable() for a single-character search.
131   if (s.length_ == 1) return find_first_of(s.ptr_[0], pos);
132   LookupTable tbl(s);
133   for (size_type i = pos; i < length_; ++i) {
134     if (tbl[ptr_[i]]) {
135       return i;
136     }
137   }
138   return npos;
139 }
140 
find_first_not_of(string_view s,size_type pos) const141 string_view::size_type string_view::find_first_not_of(string_view s,
142                                                       size_type pos) const
143     noexcept {
144   if (empty()) return npos;
145   // Avoid the cost of LookupTable() for a single-character search.
146   if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos);
147   LookupTable tbl(s);
148   for (size_type i = pos; i < length_; ++i) {
149     if (!tbl[ptr_[i]]) {
150       return i;
151     }
152   }
153   return npos;
154 }
155 
find_first_not_of(char c,size_type pos) const156 string_view::size_type string_view::find_first_not_of(char c,
157                                                       size_type pos) const
158     noexcept {
159   if (empty()) return npos;
160   for (; pos < length_; ++pos) {
161     if (ptr_[pos] != c) {
162       return pos;
163     }
164   }
165   return npos;
166 }
167 
find_last_of(string_view s,size_type pos) const168 string_view::size_type string_view::find_last_of(string_view s,
169                                                  size_type pos) const noexcept {
170   if (empty() || s.empty()) return npos;
171   // Avoid the cost of LookupTable() for a single-character search.
172   if (s.length_ == 1) return find_last_of(s.ptr_[0], pos);
173   LookupTable tbl(s);
174   for (size_type i = std::min(pos, length_ - 1);; --i) {
175     if (tbl[ptr_[i]]) {
176       return i;
177     }
178     if (i == 0) break;
179   }
180   return npos;
181 }
182 
find_last_not_of(string_view s,size_type pos) const183 string_view::size_type string_view::find_last_not_of(string_view s,
184                                                      size_type pos) const
185     noexcept {
186   if (empty()) return npos;
187   size_type i = std::min(pos, length_ - 1);
188   if (s.empty()) return i;
189   // Avoid the cost of LookupTable() for a single-character search.
190   if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos);
191   LookupTable tbl(s);
192   for (;; --i) {
193     if (!tbl[ptr_[i]]) {
194       return i;
195     }
196     if (i == 0) break;
197   }
198   return npos;
199 }
200 
find_last_not_of(char c,size_type pos) const201 string_view::size_type string_view::find_last_not_of(char c,
202                                                      size_type pos) const
203     noexcept {
204   if (empty()) return npos;
205   size_type i = std::min(pos, length_ - 1);
206   for (;; --i) {
207     if (ptr_[i] != c) {
208       return i;
209     }
210     if (i == 0) break;
211   }
212   return npos;
213 }
214 
215 // MSVC has non-standard behavior that implicitly creates definitions for static
216 // const members. These implicit definitions conflict with explicit out-of-class
217 // member definitions that are required by the C++ standard, resulting in
218 // LNK1169 "multiply defined" errors at link time. __declspec(selectany) asks
219 // MSVC to choose only one definition for the symbol it decorates. See details
220 // at https://msdn.microsoft.com/en-us/library/34h23df8(v=vs.100).aspx
221 #ifdef _MSC_VER
222 #define ABSL_STRING_VIEW_SELECTANY __declspec(selectany)
223 #else
224 #define ABSL_STRING_VIEW_SELECTANY
225 #endif
226 
227 ABSL_STRING_VIEW_SELECTANY
228 constexpr string_view::size_type string_view::npos;
229 ABSL_STRING_VIEW_SELECTANY
230 constexpr string_view::size_type string_view::kMaxSize;
231 
232 ABSL_NAMESPACE_END
233 }  // namespace absl
234 
235 #endif  // ABSL_USES_STD_STRING_VIEW
236