Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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
6 #include <algorithm>
7 #include <ostream>
9 #include "base/string_piece.h"
11 typedef StringPiece::size_type size_type;
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 }
18 bool operator==(const StringPiece& x, const StringPiece& y) {
19 if (x.size() != y.size())
20 return false;
22 return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0;
23 }
25 void StringPiece::CopyToString(std::string* target) const {
26 target->assign(!empty() ? data() : "", size());
27 }
29 void StringPiece::AppendToString(std::string* target) const {
30 if (!empty())
31 target->append(data(), size());
32 }
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 }
40 size_type StringPiece::find(const StringPiece& s, size_type pos) const {
41 if (pos > length_)
42 return npos;
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 }
50 size_type StringPiece::find(char c, size_type pos) const {
51 if (pos >= length_)
52 return npos;
54 const char* result = std::find(ptr_ + pos, ptr_ + length_, c);
55 return result != ptr_ + length_ ? result - ptr_ : npos;
56 }
58 size_type StringPiece::rfind(const StringPiece& s, size_type pos) const {
59 if (length_ < s.length_)
60 return npos;
62 if (s.empty())
63 return std::min(length_, pos);
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 }
70 size_type StringPiece::rfind(char c, size_type pos) const {
71 if (length_ == 0)
72 return npos;
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 }
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 }
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;
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);
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 }
119 size_type StringPiece::find_first_not_of(const StringPiece& s,
120 size_type pos) const {
121 if (length_ == 0)
122 return npos;
124 if (s.length_ == 0)
125 return 0;
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);
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 }
141 size_type StringPiece::find_first_not_of(char c, size_type pos) const {
142 if (length_ == 0)
143 return npos;
145 for (; pos < length_; ++pos) {
146 if (ptr_[pos] != c) {
147 return pos;
148 }
149 }
150 return npos;
151 }
153 size_type StringPiece::find_last_of(const StringPiece& s, size_type pos) const {
154 if (length_ == 0 || s.length_ == 0)
155 return npos;
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);
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 }
172 size_type StringPiece::find_last_not_of(const StringPiece& s,
173 size_type pos) const {
174 if (length_ == 0)
175 return npos;
177 size_type i = std::min(pos, length_ - 1);
178 if (s.length_ == 0)
179 return i;
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);
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 }
196 size_type StringPiece::find_last_not_of(char c, size_type pos) const {
197 if (length_ == 0)
198 return npos;
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 }
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 }
215 const StringPiece::size_type StringPiece::npos = size_type(-1);