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 {
156156
157157 var i: u8 = 0;
158158 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);
160160 }
161161}
162162
......@@ -357,19 +357,25 @@ test endsWithIgnoreCase {
357357 try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
358358}
359359
360/// Deprecated in favor of `findIgnoreCase`.
361pub const indexOfIgnoreCase = findIgnoreCase;
362
360363/// Finds `needle` in `haystack`, ignoring case, starting at index 0.
361pub fn indexOfIgnoreCase(haystack: []const u8, needle: []const u8) ?usize {
362 return indexOfIgnoreCasePos(haystack, 0, needle);
364pub fn findIgnoreCase(haystack: []const u8, needle: []const u8) ?usize {
365 return findIgnoreCasePos(haystack, 0, needle);
363366}
364367
368/// Deprecated in favor of `findIgnoreCasePos`.
369pub const indexOfIgnoreCasePos = findIgnoreCasePos;
370
365371/// Finds `needle` in `haystack`, ignoring case, starting at `start_index`.
366/// Uses Boyer-Moore-Horspool algorithm on large inputs; `indexOfIgnoreCasePosLinear` on small inputs.
367pub fn indexOfIgnoreCasePos(haystack: []const u8, start_index: usize, needle: []const u8) ?usize {
372/// Uses Boyer-Moore-Horspool algorithm on large inputs; `findIgnoreCasePosLinear` on small inputs.
373pub fn findIgnoreCasePos(haystack: []const u8, start_index: usize, needle: []const u8) ?usize {
368374 if (needle.len > haystack.len) return null;
369375 if (needle.len == 0) return start_index;
370376
371377 if (haystack.len < 52 or needle.len <= 4)
372 return indexOfIgnoreCasePosLinear(haystack, start_index, needle);
378 return findIgnoreCasePosLinear(haystack, start_index, needle);
373379
374380 var skip_table: [256]usize = undefined;
375381 boyerMooreHorspoolPreprocessIgnoreCase(needle, skip_table[0..]);
......@@ -383,9 +389,12 @@ pub fn indexOfIgnoreCasePos(haystack: []const u8, start_index: usize, needle: []
383389 return null;
384390}
385391
386/// Consider using `indexOfIgnoreCasePos` instead of this, which will automatically use a
392/// Deprecated in favor of `findIgnoreCaseLinear`.
393pub const indexOfIgnoreCasePosLinear = findIgnoreCasePosLinear;
394
395/// Consider using `findIgnoreCasePos` instead of this, which will automatically use a
387396/// 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 {
389398 var i: usize = start_index;
390399 const end = haystack.len - needle.len;
391400 while (i <= end) : (i += 1) {
......@@ -407,15 +416,15 @@ fn boyerMooreHorspoolPreprocessIgnoreCase(pattern: []const u8, table: *[256]usiz
407416 }
408417}
409418
410test indexOfIgnoreCase {
411 try std.testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);
412 try std.testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);
413 try std.testing.expect(indexOfIgnoreCase("foO", "Foo").? == 0);
414 try std.testing.expect(indexOfIgnoreCase("foo", "fool") == null);
415 try std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);
419test findIgnoreCase {
420 try std.testing.expect(findIgnoreCase("one Two Three Four", "foUr").? == 14);
421 try std.testing.expect(findIgnoreCase("one two three FouR", "gOur") == null);
422 try std.testing.expect(findIgnoreCase("foO", "Foo").? == 0);
423 try std.testing.expect(findIgnoreCase("foo", "fool") == null);
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);
418 try std.testing.expect(indexOfIgnoreCase("one two three four five six seven eight nine ten eleven", "Two tWo") == null);
426 try std.testing.expect(findIgnoreCase("one two three four five six seven eight nine ten eleven", "ThReE fOUr").? == 8);
427 try std.testing.expect(findIgnoreCase("one two three four five six seven eight nine ten eleven", "Two tWo") == null);
419428}
420429
421430/// Returns the lexicographical order of two slices. O(n).