@@ -330,10 +330,12 @@ unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
330
330
// find something. When we find something the `finger` will be set
331
331
// to a UTF8 boundary.
332
332
self . finger += index + 1 ;
333
- let found_char = self . finger - self . utf8_size ;
334
- if let Some ( slice) = self . haystack . as_bytes ( ) . get ( found_char..self . finger ) {
335
- if slice == & self . utf8_encoded [ 0 ..self . utf8_size ] {
336
- return Some ( ( found_char, self . finger ) ) ;
333
+ if self . finger >= self . utf8_size {
334
+ let found_char = self . finger - self . utf8_size ;
335
+ if let Some ( slice) = self . haystack . as_bytes ( ) . get ( found_char..self . finger ) {
336
+ if slice == & self . utf8_encoded [ 0 ..self . utf8_size ] {
337
+ return Some ( ( found_char, self . finger ) ) ;
338
+ }
337
339
}
338
340
}
339
341
} else {
@@ -386,12 +388,15 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
386
388
// char in the paradigm of reverse iteration). For
387
389
// multibyte chars we need to skip down by the number of more
388
390
// bytes they have than ASCII
389
- let found_char = index - ( self . utf8_size - 1 ) ;
390
- if let Some ( slice) = haystack. get ( found_char..( found_char + self . utf8_size ) ) {
391
- if slice == & self . utf8_encoded [ 0 ..self . utf8_size ] {
392
- // move finger to before the character found (i.e. at its start index)
393
- self . finger_back = found_char;
394
- return Some ( ( self . finger_back , self . finger_back + self . utf8_size ) ) ;
391
+ let shift = self . utf8_size - 1 ;
392
+ if index >= shift {
393
+ let found_char = index - shift;
394
+ if let Some ( slice) = haystack. get ( found_char..( found_char + self . utf8_size ) ) {
395
+ if slice == & self . utf8_encoded [ 0 ..self . utf8_size ] {
396
+ // move finger to before the character found (i.e. at its start index)
397
+ self . finger_back = found_char;
398
+ return Some ( ( self . finger_back , self . finger_back + self . utf8_size ) ) ;
399
+ }
395
400
}
396
401
}
397
402
// We can't use finger_back = index - size + 1 here. If we found the last char
0 commit comments