Skip to content

Commit 9a43492

Browse files
committed
Improve TwoWaySearcher comments.
1 parent 9419e92 commit 9a43492

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/libcore/str.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,13 @@ impl TwoWaySearcher {
439439
let byteset = needle.iter()
440440
.fold(0, |a, &b| (1 << ((b & 0x3f) as uint)) | a);
441441

442-
// Check if the needle is periodic. If so, during searching when we
443-
// find a mismatch, we must only advance the position by the length
444-
// of the period, not the length of the entire needle
442+
443+
// The logic here (calculating critPos and period, the final if statement to see which
444+
// period to use for the TwoWaySearcher) is essentially an implementation of the
445+
// "small-period" function from the paper (p. 670)
446+
//
447+
// In the paper they check whether `needle.slice_to(critPos)` is a suffix of
448+
// `needle.slice(critPos, critPos + period)`, which is precisely what this does
445449
if needle.slice_to(critPos) == needle.slice(period, period + critPos) {
446450
TwoWaySearcher {
447451
critPos: critPos,
@@ -513,6 +517,9 @@ impl TwoWaySearcher {
513517
}
514518
}
515519

520+
// returns (i, p) where i is the "critical position", the starting index of
521+
// of maximal suffix, and p is the period of the suffix
522+
// see p. 668 of the paper
516523
#[inline]
517524
fn maximal_suffix(arr: &[u8], reversed: bool) -> (uint, uint) {
518525
let mut left = -1; // Corresponds to i in the paper

0 commit comments

Comments
 (0)