Skip to content

Commit b1fb9e0

Browse files
committed
corrected the horspool's algorithm to now work correctly
1 parent cf75863 commit b1fb9e0

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

  • Boyer_Moore_Horspool/javascript

Boyer_Moore_Horspool/javascript/bmh.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Boyer–Moore–Horspool algorithm (or Horspool's algorithm) is an algorithm
1+
// Boyer–Moore–Horspool algorithm (or Horspool's algorithm) is an algorithm
22
// for finding substrings in strings.
33

44
function boyer_moore_horspool(haystack, needle) {
@@ -7,7 +7,7 @@ function boyer_moore_horspool(haystack, needle) {
77
last = (needle.length - 1),
88
offset = 0,
99
scan;
10-
10+
1111
// Generate the "bad match table", which is the location of offsets
1212
// to jump forward when a comparison fails
1313
Array.prototype.forEach.call(needle, function (char, i) {
@@ -16,16 +16,16 @@ function boyer_moore_horspool(haystack, needle) {
1616

1717
// Now look for the needle
1818
while (offset <= maxOffset) {
19-
// Search right-to-left, checking to see if the current offset at
20-
// needle and haystack match. If they do, rewind 1, repeat, and if we
19+
// Search right-to-left, checking to see if the current offset at
20+
// needle and haystack match. If they do, rewind 1, repeat, and if we
2121
// eventually match the first character, return the offset.
2222
for (scan=last; needle[scan] === haystack[scan+offset]; scan--) {
2323
if (scan === 0) {
2424
return offset;
2525
}
2626
}
2727

28-
offset += (badMatchTable[haystack[offset + last]] || last);
28+
offset += (badMatchTable[haystack[offset + last]] || last+1);
2929
}
3030

3131
return -1;

0 commit comments

Comments
 (0)