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