| ... | @@ -1083,7 +1083,7 @@ fn boyerMooreHorspoolPreprocessReverse(pattern: []const u8, table: *[256]usize) | ... | @@ -1083,7 +1083,7 @@ fn boyerMooreHorspoolPreprocessReverse(pattern: []const u8, table: *[256]usize) |
| 1083 | | 1083 | |
| 1084 | var i: usize = pattern.len - 1; | 1084 | var i: usize = pattern.len - 1; |
| 1085 | // The first item is intentionally ignored and the skip size will be pattern.len. | 1085 | // The first item is intentionally ignored and the skip size will be pattern.len. |
| 1086 | // This is the standard way boyer-moore-horspool is implemented. | 1086 | // This is the standard way Boyer-Moore-Horspool is implemented. |
| 1087 | while (i > 0) : (i -= 1) { | 1087 | while (i > 0) : (i -= 1) { |
| 1088 | table[pattern[i]] = i; | 1088 | table[pattern[i]] = i; |
| 1089 | } | 1089 | } |
| ... | @@ -1096,14 +1096,15 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void { | ... | @@ -1096,14 +1096,15 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void { |
| 1096 | | 1096 | |
| 1097 | var i: usize = 0; | 1097 | var i: usize = 0; |
| 1098 | // The last item is intentionally ignored and the skip size will be pattern.len. | 1098 | // The last item is intentionally ignored and the skip size will be pattern.len. |
| 1099 | // This is the standard way boyer-moore-horspool is implemented. | 1099 | // This is the standard way Boyer-Moore-Horspool is implemented. |
| 1100 | while (i < pattern.len - 1) : (i += 1) { | 1100 | while (i < pattern.len - 1) : (i += 1) { |
| 1101 | table[pattern[i]] = pattern.len - 1 - i; | 1101 | table[pattern[i]] = pattern.len - 1 - i; |
| 1102 | } | 1102 | } |
| 1103 | } | 1103 | } |
| | 1104 | |
| 1104 | /// Find the index in a slice of a sub-slice, searching from the end backwards. | 1105 | /// Find the index in a slice of a sub-slice, searching from the end backwards. |
| 1105 | /// To start looking at a different index, slice the haystack first. | 1106 | /// To start looking at a different index, slice the haystack first. |
| 1106 | /// Uses the Reverse boyer-moore-horspool algorithm on large inputs; | 1107 | /// Uses the Reverse Boyer-Moore-Horspool algorithm on large inputs; |
| 1107 | /// `lastIndexOfLinear` on small inputs. | 1108 | /// `lastIndexOfLinear` on small inputs. |
| 1108 | pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { | 1109 | pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { |
| 1109 | if (needle.len > haystack.len) return null; | 1110 | if (needle.len > haystack.len) return null; |
| ... | @@ -1131,7 +1132,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us | ... | @@ -1131,7 +1132,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us |
| 1131 | return null; | 1132 | return null; |
| 1132 | } | 1133 | } |
| 1133 | | 1134 | |
| 1134 | /// Uses Boyer-moore-horspool algorithm on large inputs; `indexOfPosLinear` on small inputs. | 1135 | /// Uses Boyer-Moore-Horspool algorithm on large inputs; `indexOfPosLinear` on small inputs. |
| 1135 | pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { | 1136 | pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { |
| 1136 | if (needle.len > haystack.len) return null; | 1137 | if (needle.len > haystack.len) return null; |
| 1137 | if (needle.len == 0) return start_index; | 1138 | if (needle.len == 0) return start_index; |
| ... | @@ -1183,7 +1184,7 @@ test "indexOf" { | ... | @@ -1183,7 +1184,7 @@ test "indexOf" { |
| 1183 | | 1184 | |
| 1184 | test "indexOf multibyte" { | 1185 | test "indexOf multibyte" { |
| 1185 | { | 1186 | { |
| 1186 | // make haystack and needle long enough to trigger boyer-moore-horspool algorithm | 1187 | // make haystack and needle long enough to trigger Boyer-Moore-Horspool algorithm |
| 1187 | const haystack = [1]u16{0} ** 100 ++ [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff }; | 1188 | const haystack = [1]u16{0} ** 100 ++ [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff }; |
| 1188 | const needle = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee }; | 1189 | const needle = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee }; |
| 1189 | try testing.expectEqual(indexOfPos(u16, &haystack, 0, &needle), 100); | 1190 | try testing.expectEqual(indexOfPos(u16, &haystack, 0, &needle), 100); |
| ... | @@ -1196,7 +1197,7 @@ test "indexOf multibyte" { | ... | @@ -1196,7 +1197,7 @@ test "indexOf multibyte" { |
| 1196 | } | 1197 | } |
| 1197 | | 1198 | |
| 1198 | { | 1199 | { |
| 1199 | // make haystack and needle long enough to trigger boyer-moore-horspool algorithm | 1200 | // make haystack and needle long enough to trigger Boyer-Moore-Horspool algorithm |
| 1200 | const haystack = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff } ++ [1]u16{0} ** 100; | 1201 | const haystack = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff } ++ [1]u16{0} ** 100; |
| 1201 | const needle = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee }; | 1202 | const needle = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee }; |
| 1202 | try testing.expectEqual(lastIndexOf(u16, &haystack, &needle), 0); | 1203 | try testing.expectEqual(lastIndexOf(u16, &haystack, &needle), 0); |