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

Side by Side Diff: src/string-search.h

Issue 1324803003: Fix two byte string-search on big endian platforms (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@speedup-stringsearch
Patch Set: Fix windows warning and simplify code Created 5 years, 3 months 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project 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 4
5 #ifndef V8_STRING_SEARCH_H_ 5 #ifndef V8_STRING_SEARCH_H_
6 #define V8_STRING_SEARCH_H_ 6 #define V8_STRING_SEARCH_H_
7 7
8 #include "src/isolate.h" 8 #include "src/isolate.h"
9 #include "src/vector.h" 9 #include "src/vector.h"
10 10
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 Isolate* isolate_; 183 Isolate* isolate_;
184 // The pattern to search for. 184 // The pattern to search for.
185 Vector<const PatternChar> pattern_; 185 Vector<const PatternChar> pattern_;
186 // Pointer to implementation of the search. 186 // Pointer to implementation of the search.
187 SearchFunction strategy_; 187 SearchFunction strategy_;
188 // Cache value of Max(0, pattern_length() - kBMMaxShift) 188 // Cache value of Max(0, pattern_length() - kBMMaxShift)
189 int start_; 189 int start_;
190 }; 190 };
191 191
192 192
193 template <typename T, typename U>
194 inline T AlignDown(T value, U alignment) {
195 return reinterpret_cast<T>(
196 (reinterpret_cast<uintptr_t>(value) & ~(alignment - 1)));
197 }
198
199
200 inline uint8_t GetHighestValueByte(uc16 character) {
201 return Max(static_cast<uint8_t>(character & 0xFF),
202 static_cast<uint8_t>(character >> 8));
203 }
204
205
206 inline uint8_t GetHighestValueByte(uint8_t character) { return character; }
207
208
193 template <typename PatternChar, typename SubjectChar> 209 template <typename PatternChar, typename SubjectChar>
194 int FindFirstCharacter(Vector<const PatternChar> pattern, 210 inline int FindFirstCharacter(Vector<const PatternChar> pattern,
195 Vector<const SubjectChar> subject, int index) { 211 Vector<const SubjectChar> subject, int index) {
196 PatternChar pattern_first_char = pattern[0]; 212 const PatternChar pattern_first_char = pattern[0];
197 const int max_n = (subject.length() - pattern.length() + 1); 213 const int max_n = (subject.length() - pattern.length() + 1);
198 214
199 if (sizeof(SubjectChar) == 1 && sizeof(PatternChar) == 1) { 215 const uint8_t search_byte = GetHighestValueByte(pattern_first_char);
200 DCHECK_GE(max_n - index, 0); 216 const SubjectChar search_char = static_cast<SubjectChar>(pattern_first_char);
217 int pos = index;
218 do {
219 DCHECK_GE(max_n - pos, 0);
201 const SubjectChar* char_pos = reinterpret_cast<const SubjectChar*>( 220 const SubjectChar* char_pos = reinterpret_cast<const SubjectChar*>(
202 memchr(subject.start() + index, pattern_first_char, max_n - index)); 221 memchr(subject.start() + pos, search_byte,
222 (max_n - pos) * sizeof(SubjectChar)));
203 if (char_pos == NULL) return -1; 223 if (char_pos == NULL) return -1;
204 return static_cast<int>(char_pos - subject.start()); 224 char_pos = AlignDown(char_pos, sizeof(SubjectChar));
205 } else { 225 pos = static_cast<int>(char_pos - subject.start());
206 const uint8_t search_low_byte = 226 if (subject[pos] == search_char) return pos;
207 static_cast<uint8_t>(pattern_first_char & 0xFF); 227 } while (++pos < max_n);
208 const SubjectChar search_char = 228
209 static_cast<SubjectChar>(pattern_first_char);
210 int pos = index;
211 do {
212 DCHECK_GE(max_n - pos, 0);
213 const SubjectChar* char_pos = reinterpret_cast<const SubjectChar*>(
214 memchr(subject.start() + pos, search_low_byte,
215 (max_n - pos) * sizeof(SubjectChar)));
216 if (char_pos == NULL) return -1;
217 pos = static_cast<int>(char_pos - subject.start());
218 if (IsAligned(reinterpret_cast<uintptr_t>(char_pos),
219 sizeof(SubjectChar))) {
220 if (subject[pos] == search_char) return pos;
221 }
222 } while (++pos < max_n);
223 }
224 return -1; 229 return -1;
225 } 230 }
226 231
227 232
228 //--------------------------------------------------------------------- 233 //---------------------------------------------------------------------
229 // Single Character Pattern Search Strategy 234 // Single Character Pattern Search Strategy
230 //--------------------------------------------------------------------- 235 //---------------------------------------------------------------------
231 236
232 template <typename PatternChar, typename SubjectChar> 237 template <typename PatternChar, typename SubjectChar>
233 int StringSearch<PatternChar, SubjectChar>::SingleCharSearch( 238 int StringSearch<PatternChar, SubjectChar>::SingleCharSearch(
234 StringSearch<PatternChar, SubjectChar>* search, 239 StringSearch<PatternChar, SubjectChar>* search,
235 Vector<const SubjectChar> subject, 240 Vector<const SubjectChar> subject,
236 int index) { 241 int index) {
237 DCHECK_EQ(1, search->pattern_.length()); 242 DCHECK_EQ(1, search->pattern_.length());
238 PatternChar pattern_first_char = search->pattern_[0]; 243 PatternChar pattern_first_char = search->pattern_[0];
239 if (sizeof(SubjectChar) == 1 && sizeof(PatternChar) == 1) { 244 if (sizeof(PatternChar) > sizeof(SubjectChar)) {
240 return FindFirstCharacter(search->pattern_, subject, index); 245 if (exceedsOneByte(pattern_first_char)) {
241 } else { 246 return -1;
242 if (sizeof(PatternChar) > sizeof(SubjectChar)) {
243 if (exceedsOneByte(pattern_first_char)) {
244 return -1;
245 }
246 } 247 }
247 return FindFirstCharacter(search->pattern_, subject, index);
248 } 248 }
249 return FindFirstCharacter(search->pattern_, subject, index);
249 } 250 }
250 251
251 //--------------------------------------------------------------------- 252 //---------------------------------------------------------------------
252 // Linear Search Strategy 253 // Linear Search Strategy
253 //--------------------------------------------------------------------- 254 //---------------------------------------------------------------------
254 255
255 256
256 template <typename PatternChar, typename SubjectChar> 257 template <typename PatternChar, typename SubjectChar>
257 inline bool CharCompare(const PatternChar* pattern, 258 inline bool CharCompare(const PatternChar* pattern,
258 const SubjectChar* subject, 259 const SubjectChar* subject,
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 Vector<const SubjectChar> subject, 559 Vector<const SubjectChar> subject,
559 Vector<const PatternChar> pattern, 560 Vector<const PatternChar> pattern,
560 int start_index) { 561 int start_index) {
561 StringSearch<PatternChar, SubjectChar> search(isolate, pattern); 562 StringSearch<PatternChar, SubjectChar> search(isolate, pattern);
562 return search.Search(subject, start_index); 563 return search.Search(subject, start_index);
563 } 564 }
564 565
565 }} // namespace v8::internal 566 }} // namespace v8::internal
566 567
567 #endif // V8_STRING_SEARCH_H_ 568 #endif // V8_STRING_SEARCH_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698