| ... | ... | @@ -1346,6 +1346,7 @@ pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const |
| 1346 | 1346 | /// Consider using `indexOfPos` instead of this, which will automatically use a |
| 1347 | 1347 | /// more sophisticated algorithm on larger inputs. |
| 1348 | 1348 | pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { |
| 1349 | if (needle.len > haystack.len) return null; |
| 1349 | 1350 | var i: usize = start_index; |
| 1350 | 1351 | const end = haystack.len - needle.len; |
| 1351 | 1352 | while (i <= end) : (i += 1) { |
| ... | ... | @@ -1354,6 +1355,26 @@ pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usiz |
| 1354 | 1355 | return null; |
| 1355 | 1356 | } |
| 1356 | 1357 | |
| 1358 | test indexOfPosLinear { |
| 1359 | try testing.expectEqual(0, indexOfPosLinear(u8, "", 0, "")); |
| 1360 | try testing.expectEqual(0, indexOfPosLinear(u8, "123", 0, "")); |
| 1361 | |
| 1362 | try testing.expectEqual(null, indexOfPosLinear(u8, "", 0, "1")); |
| 1363 | try testing.expectEqual(0, indexOfPosLinear(u8, "1", 0, "1")); |
| 1364 | try testing.expectEqual(null, indexOfPosLinear(u8, "2", 0, "1")); |
| 1365 | try testing.expectEqual(1, indexOfPosLinear(u8, "21", 0, "1")); |
| 1366 | try testing.expectEqual(null, indexOfPosLinear(u8, "222", 0, "1")); |
| 1367 | |
| 1368 | try testing.expectEqual(null, indexOfPosLinear(u8, "", 0, "12")); |
| 1369 | try testing.expectEqual(null, indexOfPosLinear(u8, "1", 0, "12")); |
| 1370 | try testing.expectEqual(null, indexOfPosLinear(u8, "2", 0, "12")); |
| 1371 | try testing.expectEqual(0, indexOfPosLinear(u8, "12", 0, "12")); |
| 1372 | try testing.expectEqual(null, indexOfPosLinear(u8, "21", 0, "12")); |
| 1373 | try testing.expectEqual(1, indexOfPosLinear(u8, "212", 0, "12")); |
| 1374 | try testing.expectEqual(0, indexOfPosLinear(u8, "122", 0, "12")); |
| 1375 | try testing.expectEqual(1, indexOfPosLinear(u8, "212112", 0, "12")); |
| 1376 | } |
| 1377 | |
| 1357 | 1378 | fn boyerMooreHorspoolPreprocessReverse(pattern: []const u8, table: *[256]usize) void { |
| 1358 | 1379 | for (table) |*c| { |
| 1359 | 1380 | c.* = pattern.len; |