Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: base/string_piece.cc

Issue 8659047: De-duplicate common code from StringPiece, StringPiece16, and their tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // Copied from strings/stringpiece.cc with modifications 4 // Copied from strings/stringpiece.cc with modifications
5 5
6 #include <algorithm> 6 #include <algorithm>
7 #include <ostream> 7 #include <ostream>
8 8
9 #include "base/string_piece.h" 9 #include "base/string_piece.h"
10 10
11 namespace base { 11 namespace base {
12 12
13 // MSVC doesn't like complex extern templates and DLLs.
14 #if !defined(COMPILER_MSVC)
15 namespace internal {
16 template class StringPieceDetail<std::string>;
17 template class StringPieceDetail<string16>;
18 } // namespace internal
19
20 template class BasicStringPiece<std::string>;
21 template class BasicStringPiece<string16>;
22 #endif
23
13 typedef StringPiece::size_type size_type; 24 typedef StringPiece::size_type size_type;
14 25
15 bool operator==(const StringPiece& x, const StringPiece& y) { 26 bool operator==(const StringPiece& x, const StringPiece& y) {
16 if (x.size() != y.size()) 27 if (x.size() != y.size())
17 return false; 28 return false;
18 29
19 return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0; 30 return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0;
20 } 31 }
21 32
22 void StringPiece::CopyToString(std::string* target) const { 33 void BasicStringPiece<std::string>::CopyToString(std::string* target) const {
23 target->assign(!empty() ? data() : "", size()); 34 target->assign(!empty() ? data() : "", size());
24 } 35 }
25 36
26 void StringPiece::AppendToString(std::string* target) const { 37 void BasicStringPiece<std::string>::AppendToString(std::string* target) const {
27 if (!empty()) 38 if (!empty())
28 target->append(data(), size()); 39 target->append(data(), size());
29 } 40 }
30 41
31 size_type StringPiece::copy(char* buf, size_type n, size_type pos) const { 42 size_type BasicStringPiece<std::string>::copy(char* buf,
43 size_type n,
44 size_type pos) const {
32 size_type ret = std::min(length_ - pos, n); 45 size_type ret = std::min(length_ - pos, n);
33 memcpy(buf, ptr_ + pos, ret); 46 memcpy(buf, ptr_ + pos, ret);
34 return ret; 47 return ret;
35 } 48 }
36 49
37 size_type StringPiece::find(const StringPiece& s, size_type pos) const { 50 size_type BasicStringPiece<std::string>::find(const BasicStringPiece& s,
51 size_type pos) const {
38 if (pos > length_) 52 if (pos > length_)
39 return npos; 53 return npos;
40 54
41 const char* result = std::search(ptr_ + pos, ptr_ + length_, 55 const char* result = std::search(ptr_ + pos, ptr_ + length_,
42 s.ptr_, s.ptr_ + s.length_); 56 s.ptr_, s.ptr_ + s.length_);
43 const size_type xpos = result - ptr_; 57 const size_type xpos = result - ptr_;
44 return xpos + s.length_ <= length_ ? xpos : npos; 58 return xpos + s.length_ <= length_ ? xpos : npos;
45 } 59 }
46 60
47 size_type StringPiece::find(char c, size_type pos) const { 61 size_type BasicStringPiece<std::string>::find(char c, size_type pos) const {
48 if (pos >= length_) 62 if (pos >= length_)
49 return npos; 63 return npos;
50 64
51 const char* result = std::find(ptr_ + pos, ptr_ + length_, c); 65 const char* result = std::find(ptr_ + pos, ptr_ + length_, c);
52 return result != ptr_ + length_ ? static_cast<size_t>(result - ptr_) : npos; 66 return result != ptr_ + length_ ? static_cast<size_t>(result - ptr_) : npos;
53 } 67 }
54 68
55 size_type StringPiece::rfind(const StringPiece& s, size_type pos) const { 69 size_type BasicStringPiece<std::string>::rfind(const BasicStringPiece& s,
70 size_type pos) const {
56 if (length_ < s.length_) 71 if (length_ < s.length_)
57 return npos; 72 return npos;
58 73
59 if (s.empty()) 74 if (s.empty())
60 return std::min(length_, pos); 75 return std::min(length_, pos);
61 76
62 const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_; 77 const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
63 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); 78 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
64 return result != last ? static_cast<size_t>(result - ptr_) : npos; 79 return result != last ? static_cast<size_t>(result - ptr_) : npos;
65 } 80 }
66 81
67 size_type StringPiece::rfind(char c, size_type pos) const { 82 size_type BasicStringPiece<std::string>::rfind(char c, size_type pos) const {
68 if (length_ == 0) 83 if (length_ == 0)
69 return npos; 84 return npos;
70 85
71 for (size_type i = std::min(pos, length_ - 1); ; --i) { 86 for (size_type i = std::min(pos, length_ - 1); ; --i) {
72 if (ptr_[i] == c) 87 if (ptr_[i] == c)
73 return i; 88 return i;
74 if (i == 0) 89 if (i == 0)
75 break; 90 break;
76 } 91 }
77 return npos; 92 return npos;
78 } 93 }
79 94
80 // For each character in characters_wanted, sets the index corresponding 95 // For each character in characters_wanted, sets the index corresponding
81 // to the ASCII code of that character to 1 in table. This is used by 96 // to the ASCII code of that character to 1 in table. This is used by
82 // the find_.*_of methods below to tell whether or not a character is in 97 // the find_.*_of methods below to tell whether or not a character is in
83 // the lookup table in constant time. 98 // the lookup table in constant time.
84 // The argument `table' must be an array that is large enough to hold all 99 // The argument `table' must be an array that is large enough to hold all
85 // the possible values of an unsigned char. Thus it should be be declared 100 // the possible values of an unsigned char. Thus it should be be declared
86 // as follows: 101 // as follows:
87 // bool table[UCHAR_MAX + 1] 102 // bool table[UCHAR_MAX + 1]
88 static inline void BuildLookupTable(const StringPiece& characters_wanted, 103 static inline void BuildLookupTable(const StringPiece& characters_wanted,
89 bool* table) { 104 bool* table) {
90 const size_type length = characters_wanted.length(); 105 const size_type length = characters_wanted.length();
91 const char* const data = characters_wanted.data(); 106 const char* const data = characters_wanted.data();
92 for (size_type i = 0; i < length; ++i) { 107 for (size_type i = 0; i < length; ++i) {
93 table[static_cast<unsigned char>(data[i])] = true; 108 table[static_cast<unsigned char>(data[i])] = true;
94 } 109 }
95 } 110 }
96 111
97 size_type StringPiece::find_first_of(const StringPiece& s, 112 size_type BasicStringPiece<std::string>::find_first_of(
98 size_type pos) const { 113 const BasicStringPiece& s, size_type pos) const {
99 if (length_ == 0 || s.length_ == 0) 114 if (length_ == 0 || s.length_ == 0)
100 return npos; 115 return npos;
101 116
102 // Avoid the cost of BuildLookupTable() for a single-character search. 117 // Avoid the cost of BuildLookupTable() for a single-character search.
103 if (s.length_ == 1) 118 if (s.length_ == 1)
104 return find_first_of(s.ptr_[0], pos); 119 return find_first_of(s.ptr_[0], pos);
105 120
106 bool lookup[UCHAR_MAX + 1] = { false }; 121 bool lookup[UCHAR_MAX + 1] = { false };
107 BuildLookupTable(s, lookup); 122 BuildLookupTable(s, lookup);
108 for (size_type i = pos; i < length_; ++i) { 123 for (size_type i = pos; i < length_; ++i) {
109 if (lookup[static_cast<unsigned char>(ptr_[i])]) { 124 if (lookup[static_cast<unsigned char>(ptr_[i])]) {
110 return i; 125 return i;
111 } 126 }
112 } 127 }
113 return npos; 128 return npos;
114 } 129 }
115 130
116 size_type StringPiece::find_first_not_of(const StringPiece& s, 131 size_type BasicStringPiece<std::string>::find_first_not_of(
117 size_type pos) const { 132 const BasicStringPiece& s, size_type pos) const {
118 if (length_ == 0) 133 if (length_ == 0)
119 return npos; 134 return npos;
120 135
121 if (s.length_ == 0) 136 if (s.length_ == 0)
122 return 0; 137 return 0;
123 138
124 // Avoid the cost of BuildLookupTable() for a single-character search. 139 // Avoid the cost of BuildLookupTable() for a single-character search.
125 if (s.length_ == 1) 140 if (s.length_ == 1)
126 return find_first_not_of(s.ptr_[0], pos); 141 return find_first_not_of(s.ptr_[0], pos);
127 142
128 bool lookup[UCHAR_MAX + 1] = { false }; 143 bool lookup[UCHAR_MAX + 1] = { false };
129 BuildLookupTable(s, lookup); 144 BuildLookupTable(s, lookup);
130 for (size_type i = pos; i < length_; ++i) { 145 for (size_type i = pos; i < length_; ++i) {
131 if (!lookup[static_cast<unsigned char>(ptr_[i])]) { 146 if (!lookup[static_cast<unsigned char>(ptr_[i])]) {
132 return i; 147 return i;
133 } 148 }
134 } 149 }
135 return npos; 150 return npos;
136 } 151 }
137 152
138 size_type StringPiece::find_first_not_of(char c, size_type pos) const { 153 size_type BasicStringPiece<std::string>::find_first_not_of(
154 char c, size_type pos) const {
139 if (length_ == 0) 155 if (length_ == 0)
140 return npos; 156 return npos;
141 157
142 for (; pos < length_; ++pos) { 158 for (; pos < length_; ++pos) {
143 if (ptr_[pos] != c) { 159 if (ptr_[pos] != c) {
144 return pos; 160 return pos;
145 } 161 }
146 } 162 }
147 return npos; 163 return npos;
148 } 164 }
149 165
150 size_type StringPiece::find_last_of(const StringPiece& s, size_type pos) const { 166 size_type BasicStringPiece<std::string>::find_last_of(const StringPiece& s,
167 size_type pos) const {
151 if (length_ == 0 || s.length_ == 0) 168 if (length_ == 0 || s.length_ == 0)
152 return npos; 169 return npos;
153 170
154 // Avoid the cost of BuildLookupTable() for a single-character search. 171 // Avoid the cost of BuildLookupTable() for a single-character search.
155 if (s.length_ == 1) 172 if (s.length_ == 1)
156 return find_last_of(s.ptr_[0], pos); 173 return find_last_of(s.ptr_[0], pos);
157 174
158 bool lookup[UCHAR_MAX + 1] = { false }; 175 bool lookup[UCHAR_MAX + 1] = { false };
159 BuildLookupTable(s, lookup); 176 BuildLookupTable(s, lookup);
160 for (size_type i = std::min(pos, length_ - 1); ; --i) { 177 for (size_type i = std::min(pos, length_ - 1); ; --i) {
161 if (lookup[static_cast<unsigned char>(ptr_[i])]) 178 if (lookup[static_cast<unsigned char>(ptr_[i])])
162 return i; 179 return i;
163 if (i == 0) 180 if (i == 0)
164 break; 181 break;
165 } 182 }
166 return npos; 183 return npos;
167 } 184 }
168 185
169 size_type StringPiece::find_last_not_of(const StringPiece& s, 186 size_type BasicStringPiece<std::string>::find_last_not_of(
170 size_type pos) const { 187 const BasicStringPiece& s, size_type pos) const {
171 if (length_ == 0) 188 if (length_ == 0)
172 return npos; 189 return npos;
173 190
174 size_type i = std::min(pos, length_ - 1); 191 size_type i = std::min(pos, length_ - 1);
175 if (s.length_ == 0) 192 if (s.length_ == 0)
176 return i; 193 return i;
177 194
178 // Avoid the cost of BuildLookupTable() for a single-character search. 195 // Avoid the cost of BuildLookupTable() for a single-character search.
179 if (s.length_ == 1) 196 if (s.length_ == 1)
180 return find_last_not_of(s.ptr_[0], pos); 197 return find_last_not_of(s.ptr_[0], pos);
181 198
182 bool lookup[UCHAR_MAX + 1] = { false }; 199 bool lookup[UCHAR_MAX + 1] = { false };
183 BuildLookupTable(s, lookup); 200 BuildLookupTable(s, lookup);
184 for (; ; --i) { 201 for (; ; --i) {
185 if (!lookup[static_cast<unsigned char>(ptr_[i])]) 202 if (!lookup[static_cast<unsigned char>(ptr_[i])])
186 return i; 203 return i;
187 if (i == 0) 204 if (i == 0)
188 break; 205 break;
189 } 206 }
190 return npos; 207 return npos;
191 } 208 }
192 209
193 size_type StringPiece::find_last_not_of(char c, size_type pos) const { 210 size_type BasicStringPiece<std::string>::find_last_not_of(char c,
211 size_type pos) const {
194 if (length_ == 0) 212 if (length_ == 0)
195 return npos; 213 return npos;
196 214
197 for (size_type i = std::min(pos, length_ - 1); ; --i) { 215 for (size_type i = std::min(pos, length_ - 1); ; --i) {
198 if (ptr_[i] != c) 216 if (ptr_[i] != c)
199 return i; 217 return i;
200 if (i == 0) 218 if (i == 0)
201 break; 219 break;
202 } 220 }
203 return npos; 221 return npos;
204 } 222 }
205 223
206 StringPiece StringPiece::substr(size_type pos, size_type n) const { 224 BasicStringPiece<std::string> BasicStringPiece<std::string>::substr(
225 size_type pos, size_type n) const {
207 if (pos > length_) pos = length_; 226 if (pos > length_) pos = length_;
208 if (n > length_ - pos) n = length_ - pos; 227 if (n > length_ - pos) n = length_ - pos;
209 return StringPiece(ptr_ + pos, n); 228 return StringPiece(ptr_ + pos, n);
210 } 229 }
211 230
212 const StringPiece::size_type StringPiece::npos = size_type(-1);
213
214 } // namespace base 231 } // namespace base
OLDNEW
« base/string_piece.h ('K') | « base/string_piece.h ('k') | base/string_piece_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698