blob: dc034a83eea87304336128e547173d2fd1fda199 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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_HAVE_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
26namespace absl {
27
28namespace {
29void WritePadding(std::ostream& o, size_t pad) {
30 char fill_buf[32];
31 memset(fill_buf, o.fill(), sizeof(fill_buf));
32 while (pad) {
33 size_t n = std::min(pad, sizeof(fill_buf));
34 o.write(fill_buf, n);
35 pad -= n;
36 }
37}
38
39class LookupTable {
40 public:
41 // For each character in wanted, sets the index corresponding
42 // to the ASCII code of that character. This is used by
43 // the find_.*_of methods below to tell whether or not a character is in
44 // the lookup table in constant time.
45 explicit LookupTable(string_view wanted) {
46 for (char c : wanted) {
47 table_[Index(c)] = true;
48 }
49 }
50 bool operator[](char c) const { return table_[Index(c)]; }
51
52 private:
53 static unsigned char Index(char c) { return static_cast<unsigned char>(c); }
54 bool table_[UCHAR_MAX + 1] = {};
55};
56
57} // namespace
58
59std::ostream& operator<<(std::ostream& o, string_view piece) {
60 std::ostream::sentry sentry(o);
61 if (sentry) {
62 size_t lpad = 0;
63 size_t rpad = 0;
64 if (static_cast<size_t>(o.width()) > piece.size()) {
65 size_t pad = o.width() - piece.size();
66 if ((o.flags() & o.adjustfield) == o.left) {
67 rpad = pad;
68 } else {
69 lpad = pad;
70 }
71 }
72 if (lpad) WritePadding(o, lpad);
73 o.write(piece.data(), piece.size());
74 if (rpad) WritePadding(o, rpad);
75 o.width(0);
76 }
77 return o;
78}
79
80string_view::size_type string_view::find(string_view s, size_type pos) const
81 noexcept {
82 if (empty() || pos > length_) {
83 if (empty() && pos == 0 && s.empty()) return 0;
84 return npos;
85 }
86 const char* result =
87 strings_internal::memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_);
88 return result ? result - ptr_ : npos;
89}
90
91string_view::size_type string_view::find(char c, size_type pos) const noexcept {
92 if (empty() || pos >= length_) {
93 return npos;
94 }
95 const char* result =
96 static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos));
97 return result != nullptr ? result - ptr_ : npos;
98}
99
100string_view::size_type string_view::rfind(string_view s, size_type pos) const
101 noexcept {
102 if (length_ < s.length_) return npos;
103 if (s.empty()) return std::min(length_, pos);
104 const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
105 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
106 return result != last ? result - ptr_ : npos;
107}
108
109// Search range is [0..pos] inclusive. If pos == npos, search everything.
110string_view::size_type string_view::rfind(char c, size_type pos) const
111 noexcept {
112 // Note: memrchr() is not available on Windows.
113 if (empty()) return npos;
114 for (size_type i = std::min(pos, length_ - 1);; --i) {
115 if (ptr_[i] == c) {
116 return i;
117 }
118 if (i == 0) break;
119 }
120 return npos;
121}
122
123string_view::size_type string_view::find_first_of(string_view s,
124 size_type pos) const
125 noexcept {
126 if (empty() || s.empty()) {
127 return npos;
128 }
129 // Avoid the cost of LookupTable() for a single-character search.
130 if (s.length_ == 1) return find_first_of(s.ptr_[0], pos);
131 LookupTable tbl(s);
132 for (size_type i = pos; i < length_; ++i) {
133 if (tbl[ptr_[i]]) {
134 return i;
135 }
136 }
137 return npos;
138}
139
140string_view::size_type string_view::find_first_not_of(string_view s,
141 size_type pos) const
142 noexcept {
143 if (empty()) return npos;
144 // Avoid the cost of LookupTable() for a single-character search.
145 if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos);
146 LookupTable tbl(s);
147 for (size_type i = pos; i < length_; ++i) {
148 if (!tbl[ptr_[i]]) {
149 return i;
150 }
151 }
152 return npos;
153}
154
155string_view::size_type string_view::find_first_not_of(char c,
156 size_type pos) const
157 noexcept {
158 if (empty()) return npos;
159 for (; pos < length_; ++pos) {
160 if (ptr_[pos] != c) {
161 return pos;
162 }
163 }
164 return npos;
165}
166
167string_view::size_type string_view::find_last_of(string_view s,
168 size_type pos) const noexcept {
169 if (empty() || s.empty()) return npos;
170 // Avoid the cost of LookupTable() for a single-character search.
171 if (s.length_ == 1) return find_last_of(s.ptr_[0], pos);
172 LookupTable tbl(s);
173 for (size_type i = std::min(pos, length_ - 1);; --i) {
174 if (tbl[ptr_[i]]) {
175 return i;
176 }
177 if (i == 0) break;
178 }
179 return npos;
180}
181
182string_view::size_type string_view::find_last_not_of(string_view s,
183 size_type pos) const
184 noexcept {
185 if (empty()) return npos;
186 size_type i = std::min(pos, length_ - 1);
187 if (s.empty()) return i;
188 // Avoid the cost of LookupTable() for a single-character search.
189 if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos);
190 LookupTable tbl(s);
191 for (;; --i) {
192 if (!tbl[ptr_[i]]) {
193 return i;
194 }
195 if (i == 0) break;
196 }
197 return npos;
198}
199
200string_view::size_type string_view::find_last_not_of(char c,
201 size_type pos) const
202 noexcept {
203 if (empty()) return npos;
204 size_type i = std::min(pos, length_ - 1);
205 for (;; --i) {
206 if (ptr_[i] != c) {
207 return i;
208 }
209 if (i == 0) break;
210 }
211 return npos;
212}
213
214// MSVC has non-standard behavior that implicitly creates definitions for static
215// const members. These implicit definitions conflict with explicit out-of-class
216// member definitions that are required by the C++ standard, resulting in
217// LNK1169 "multiply defined" errors at link time. __declspec(selectany) asks
218// MSVC to choose only one definition for the symbol it decorates. See details
219// at https://msdn.microsoft.com/en-us/library/34h23df8(v=vs.100).aspx
220#ifdef _MSC_VER
221#define ABSL_STRING_VIEW_SELECTANY __declspec(selectany)
222#else
223#define ABSL_STRING_VIEW_SELECTANY
224#endif
225
226ABSL_STRING_VIEW_SELECTANY
227constexpr string_view::size_type string_view::npos;
228ABSL_STRING_VIEW_SELECTANY
229constexpr string_view::size_type string_view::kMaxSize;
230
231} // namespace absl
232
233#endif // ABSL_HAVE_STD_STRING_VIEW