authorgravatar for adria.arrufat@gmail.comAdrià Arrufat <adria.arrufat@gmail.com> 2025-12-04 10:44:27+09:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-12-05 14:31:27+01:00
log1a420a8dca34db8b632b0451d022ef92f78f96ac
tree8206d72e7fe3c32110c0a3ace061c71b3ae243ec
parent032e3c92547e33be0ad0f2c6bef1c1e2a51d505c

std.ascii: rename indexOf functions to find

This aligns with the recent changes in std.mem.find

1 files changed, 25 insertions(+), 16 deletions(-)

lib/std/ascii.zig+25-16
...@@ -156,7 +156,7 @@ test whitespace {...@@ -156,7 +156,7 @@ test whitespace {
156156
157 var i: u8 = 0;157 var i: u8 = 0;
158 while (isAscii(i)) : (i += 1) {158 while (isAscii(i)) : (i += 1) {
159 if (isWhitespace(i)) try std.testing.expect(std.mem.indexOfScalar(u8, &whitespace, i) != null);159 if (isWhitespace(i)) try std.testing.expect(std.mem.findScalar(u8, &whitespace, i) != null);
160 }160 }
161}161}
162162
...@@ -357,19 +357,25 @@ test endsWithIgnoreCase {...@@ -357,19 +357,25 @@ test endsWithIgnoreCase {
357 try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));357 try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
358}358}
359359
360/// Deprecated in favor of `findIgnoreCase`.
361pub const indexOfIgnoreCase = findIgnoreCase;
362
360/// Finds `needle` in `haystack`, ignoring case, starting at index 0.363/// Finds `needle` in `haystack`, ignoring case, starting at index 0.
361pub fn indexOfIgnoreCase(haystack: []const u8, needle: []const u8) ?usize {364pub fn findIgnoreCase(haystack: []const u8, needle: []const u8) ?usize {
362 return indexOfIgnoreCasePos(haystack, 0, needle);365 return findIgnoreCasePos(haystack, 0, needle);
363}366}
364367
368/// Deprecated in favor of `findIgnoreCasePos`.
369pub const indexOfIgnoreCasePos = findIgnoreCasePos;
370
365/// Finds `needle` in `haystack`, ignoring case, starting at `start_index`.371/// Finds `needle` in `haystack`, ignoring case, starting at `start_index`.
366/// Uses Boyer-Moore-Horspool algorithm on large inputs; `indexOfIgnoreCasePosLinear` on small inputs.372/// Uses Boyer-Moore-Horspool algorithm on large inputs; `findIgnoreCasePosLinear` on small inputs.
367pub fn indexOfIgnoreCasePos(haystack: []const u8, start_index: usize, needle: []const u8) ?usize {373pub fn findIgnoreCasePos(haystack: []const u8, start_index: usize, needle: []const u8) ?usize {
368 if (needle.len > haystack.len) return null;374 if (needle.len > haystack.len) return null;
369 if (needle.len == 0) return start_index;375 if (needle.len == 0) return start_index;
370376
371 if (haystack.len < 52 or needle.len <= 4)377 if (haystack.len < 52 or needle.len <= 4)
372 return indexOfIgnoreCasePosLinear(haystack, start_index, needle);378 return findIgnoreCasePosLinear(haystack, start_index, needle);
373379
374 var skip_table: [256]usize = undefined;380 var skip_table: [256]usize = undefined;
375 boyerMooreHorspoolPreprocessIgnoreCase(needle, skip_table[0..]);381 boyerMooreHorspoolPreprocessIgnoreCase(needle, skip_table[0..]);
...@@ -383,9 +389,12 @@ pub fn indexOfIgnoreCasePos(haystack: []const u8, start_index: usize, needle: []...@@ -383,9 +389,12 @@ pub fn indexOfIgnoreCasePos(haystack: []const u8, start_index: usize, needle: []
383 return null;389 return null;
384}390}
385391
386/// Consider using `indexOfIgnoreCasePos` instead of this, which will automatically use a392/// Deprecated in favor of `findIgnoreCaseLinear`.
393pub const indexOfIgnoreCasePosLinear = findIgnoreCasePosLinear;
394
395/// Consider using `findIgnoreCasePos` instead of this, which will automatically use a
387/// more sophisticated algorithm on larger inputs.396/// more sophisticated algorithm on larger inputs.
388pub fn indexOfIgnoreCasePosLinear(haystack: []const u8, start_index: usize, needle: []const u8) ?usize {397pub fn findIgnoreCasePosLinear(haystack: []const u8, start_index: usize, needle: []const u8) ?usize {
389 var i: usize = start_index;398 var i: usize = start_index;
390 const end = haystack.len - needle.len;399 const end = haystack.len - needle.len;
391 while (i <= end) : (i += 1) {400 while (i <= end) : (i += 1) {
...@@ -407,15 +416,15 @@ fn boyerMooreHorspoolPreprocessIgnoreCase(pattern: []const u8, table: *[256]usiz...@@ -407,15 +416,15 @@ fn boyerMooreHorspoolPreprocessIgnoreCase(pattern: []const u8, table: *[256]usiz
407 }416 }
408}417}
409418
410test indexOfIgnoreCase {419test findIgnoreCase {
411 try std.testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);420 try std.testing.expect(findIgnoreCase("one Two Three Four", "foUr").? == 14);
412 try std.testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);421 try std.testing.expect(findIgnoreCase("one two three FouR", "gOur") == null);
413 try std.testing.expect(indexOfIgnoreCase("foO", "Foo").? == 0);422 try std.testing.expect(findIgnoreCase("foO", "Foo").? == 0);
414 try std.testing.expect(indexOfIgnoreCase("foo", "fool") == null);423 try std.testing.expect(findIgnoreCase("foo", "fool") == null);
415 try std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);424 try std.testing.expect(findIgnoreCase("FOO foo", "fOo").? == 0);
416425
417 try std.testing.expect(indexOfIgnoreCase("one two three four five six seven eight nine ten eleven", "ThReE fOUr").? == 8);426 try std.testing.expect(findIgnoreCase("one two three four five six seven eight nine ten eleven", "ThReE fOUr").? == 8);
418 try std.testing.expect(indexOfIgnoreCase("one two three four five six seven eight nine ten eleven", "Two tWo") == null);427 try std.testing.expect(findIgnoreCase("one two three four five six seven eight nine ten eleven", "Two tWo") == null);
419}428}
420429
421/// Returns the lexicographical order of two slices. O(n).430/// Returns the lexicographical order of two slices. O(n).