Skip to content

Commit ca506a6

Browse files
committed
---
yaml --- r: 232496 b: refs/heads/try c: 2b82c07 h: refs/heads/master v: v3
1 parent 3633441 commit ca506a6

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: edeb4f1c86cbf6af8ef9874d4b3af50f721ea1b8
33
refs/heads/snap-stage3: 1af31d4974e33027a68126fa5a5a3c2c6491824f
4-
refs/heads/try: 7ebae85bb8eac495bbc4a463319b23404fdc63a6
4+
refs/heads/try: 2b82c072c75d64c434a62f185b67fb41028d6f71
55
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
66
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
77
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try/src/libcore/str/pattern.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -906,19 +906,25 @@ impl TwoWaySearcher {
906906
{
907907
// `next()` uses `self.position` as its cursor
908908
let old_pos = self.position;
909+
let needle_last = needle.len() - 1;
909910
'search: loop {
910911
// Check that we have room to search in
911-
if needle.len() > haystack.len() - self.position {
912-
self.position = haystack.len();
913-
return S::rejecting(old_pos, self.position);
914-
}
912+
// position + needle_last can not overflow if we assume slices
913+
// are bounded by isize's range.
914+
let tail_byte = match haystack.get(self.position + needle_last) {
915+
Some(&b) => b,
916+
None => {
917+
self.position = haystack.len();
918+
return S::rejecting(old_pos, self.position);
919+
}
920+
};
915921

916922
if S::use_early_reject() && old_pos != self.position {
917923
return S::rejecting(old_pos, self.position);
918924
}
919925

920926
// Quickly skip by large portions unrelated to our substring
921-
if !self.byteset_contains(haystack[self.position + needle.len() - 1]) {
927+
if !self.byteset_contains(tail_byte) {
922928
self.position += needle.len();
923929
if !long_period {
924930
self.memory = 0;
@@ -986,17 +992,23 @@ impl TwoWaySearcher {
986992
let old_end = self.end;
987993
'search: loop {
988994
// Check that we have room to search in
989-
if needle.len() > self.end {
990-
self.end = 0;
991-
return S::rejecting(0, old_end);
992-
}
995+
// end - needle.len() will wrap around when there is no more room,
996+
// but due to slice length limits it can never wrap all the way back
997+
// into the length of haystack.
998+
let front_byte = match haystack.get(self.end.wrapping_sub(needle.len())) {
999+
Some(&b) => b,
1000+
None => {
1001+
self.end = 0;
1002+
return S::rejecting(0, old_end);
1003+
}
1004+
};
9931005

9941006
if S::use_early_reject() && old_end != self.end {
9951007
return S::rejecting(self.end, old_end);
9961008
}
9971009

9981010
// Quickly skip by large portions unrelated to our substring
999-
if !self.byteset_contains(haystack[self.end - needle.len()]) {
1011+
if !self.byteset_contains(front_byte) {
10001012
self.end -= needle.len();
10011013
if !long_period {
10021014
self.memory_back = needle.len();

0 commit comments

Comments
 (0)