| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6aa92d5 commit 4401575
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -261,19 +261,19 @@ inline size_t FindFirstCharacter(Vector<const Char> pattern, | |||
| 261 | 261 | const uint8_t search_byte = GetHighestValueByte(pattern_first_char); | |
| 262 | 262 | size_t pos = index; | |
| 263 | 263 | do { | |
| 264 | - size_t bytes_to_search; | ||
| 264 | + const size_t bytes_to_search = (max_n - pos) * sizeof(Char); | ||
| 265 | 265 | const void* void_pos; | |
| 266 | 266 | if (subject.forward()) { | |
| 267 | 267 | // Assert that bytes_to_search won't overflow | |
| 268 | 268 | CHECK_LE(pos, max_n); | |
| 269 | 269 | CHECK_LE(max_n - pos, SIZE_MAX / sizeof(Char)); | |
| 270 | - bytes_to_search = (max_n - pos) * sizeof(Char); | ||
| 271 | 270 | void_pos = memchr(subject.start() + pos, search_byte, bytes_to_search); | |
| 272 | 271 | } else { | |
| 273 | 272 | CHECK_LE(pos, subject.length()); | |
| 274 | 273 | CHECK_LE(subject.length() - pos, SIZE_MAX / sizeof(Char)); | |
| 275 | - bytes_to_search = (subject.length() - pos) * sizeof(Char); | ||
| 276 | - void_pos = MemrchrFill(subject.start(), search_byte, bytes_to_search); | ||
| 274 | + void_pos = MemrchrFill(subject.start() + pattern.length() - 1, | ||
| 275 | + search_byte, | ||
| 276 | + bytes_to_search); | ||
| 277 | 277 | } | |
| 278 | 278 | const Char* char_pos = static_cast<const Char*>(void_pos); | |
| 279 | 279 | if (char_pos == nullptr) | |
@@ -308,7 +308,9 @@ inline size_t FindFirstCharacter(Vector<const uint8_t> pattern, | |||
| 308 | 308 | if (subject.forward()) { | |
| 309 | 309 | pos = memchr(subject.start() + index, pattern_first_char, max_n - index); | |
| 310 | 310 | } else { | |
| 311 | - pos = MemrchrFill(subject.start(), pattern_first_char, subj_len - index); | ||
| 311 | + pos = MemrchrFill(subject.start() + pattern.length() - 1, | ||
| 312 | + pattern_first_char, | ||
| 313 | + max_n - index); | ||
| 312 | 314 | } | |
| 313 | 315 | const uint8_t* char_pos = static_cast<const uint8_t*>(pos); | |
| 314 | 316 | if (char_pos == nullptr) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -359,6 +359,23 @@ assert.equal(13, bufferString.lastIndexOf('a ', -1)); | |||
| 359 | 359 | assert.equal(0, bufferString.lastIndexOf('a ', -27)); | |
| 360 | 360 | assert.equal(-1, bufferString.lastIndexOf('a ', -28)); | |
| 361 | 361 | ||
| 362 | + // Test lastIndexOf for the case that the first character can be found, | ||
| 363 | + // but in a part of the buffer that does not make search to search | ||
| 364 | + // due do length constraints. | ||
| 365 | + const abInUCS2 = Buffer.from('ab', 'ucs2'); | ||
| 366 | + assert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'binary').lastIndexOf('µ')); | ||
| 367 | + assert.strictEqual(-1, Buffer.from('bc').lastIndexOf('ab')); | ||
| 368 | + assert.strictEqual(-1, Buffer.from('abc').lastIndexOf('qa')); | ||
| 369 | + assert.strictEqual(-1, Buffer.from('abcdef').lastIndexOf('qabc')); | ||
| 370 | + assert.strictEqual(-1, Buffer.from('bc').lastIndexOf(Buffer.from('ab'))); | ||
| 371 | + assert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf('ab', 'ucs2')); | ||
| 372 | + assert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf(abInUCS2)); | ||
| 373 | + | ||
| 374 | + assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab')); | ||
| 375 | + assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 1)); | ||
| 376 | + assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 2)); | ||
| 377 | + assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 3)); | ||
| 378 | + | ||
| 362 | 379 | // The above tests test the LINEAR and SINGLE-CHAR strategies. | |
| 363 | 380 | // Now, we test the BOYER-MOORE-HORSPOOL strategy. | |
| 364 | 381 | // Test lastIndexOf on a long buffer w multiple matches: | |
| Back | FazBrowse Home | New Git URL |
0 commit comments