mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std.ascii: rename indexOf functions to find
This aligns with the recent changes in std.mem.find
This commit is contained in:
parent
d73fbcc3ae
commit
f4b09079f8
1 changed files with 25 additions and 16 deletions
|
|
@ -156,7 +156,7 @@ test whitespace {
|
||||||
|
|
||||||
var i: u8 = 0;
|
var i: u8 = 0;
|
||||||
while (isAscii(i)) : (i += 1) {
|
while (isAscii(i)) : (i += 1) {
|
||||||
if (isWhitespace(i)) try std.testing.expect(std.mem.indexOfScalar(u8, &whitespace, i) != null);
|
if (isWhitespace(i)) try std.testing.expect(std.mem.findScalar(u8, &whitespace, i) != null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -357,19 +357,25 @@ test endsWithIgnoreCase {
|
||||||
try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
|
try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deprecated in favor of `findIgnoreCase`.
|
||||||
|
pub const indexOfIgnoreCase = findIgnoreCase;
|
||||||
|
|
||||||
/// Finds `needle` in `haystack`, ignoring case, starting at index 0.
|
/// Finds `needle` in `haystack`, ignoring case, starting at index 0.
|
||||||
pub fn indexOfIgnoreCase(haystack: []const u8, needle: []const u8) ?usize {
|
pub fn findIgnoreCase(haystack: []const u8, needle: []const u8) ?usize {
|
||||||
return indexOfIgnoreCasePos(haystack, 0, needle);
|
return findIgnoreCasePos(haystack, 0, needle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deprecated in favor of `findIgnoreCasePos`.
|
||||||
|
pub const indexOfIgnoreCasePos = findIgnoreCasePos;
|
||||||
|
|
||||||
/// Finds `needle` in `haystack`, ignoring case, starting at `start_index`.
|
/// Finds `needle` in `haystack`, ignoring case, starting at `start_index`.
|
||||||
/// Uses Boyer-Moore-Horspool algorithm on large inputs; `indexOfIgnoreCasePosLinear` on small inputs.
|
/// Uses Boyer-Moore-Horspool algorithm on large inputs; `findIgnoreCasePosLinear` on small inputs.
|
||||||
pub fn indexOfIgnoreCasePos(haystack: []const u8, start_index: usize, needle: []const u8) ?usize {
|
pub fn findIgnoreCasePos(haystack: []const u8, start_index: usize, needle: []const u8) ?usize {
|
||||||
if (needle.len > haystack.len) return null;
|
if (needle.len > haystack.len) return null;
|
||||||
if (needle.len == 0) return start_index;
|
if (needle.len == 0) return start_index;
|
||||||
|
|
||||||
if (haystack.len < 52 or needle.len <= 4)
|
if (haystack.len < 52 or needle.len <= 4)
|
||||||
return indexOfIgnoreCasePosLinear(haystack, start_index, needle);
|
return findIgnoreCasePosLinear(haystack, start_index, needle);
|
||||||
|
|
||||||
var skip_table: [256]usize = undefined;
|
var skip_table: [256]usize = undefined;
|
||||||
boyerMooreHorspoolPreprocessIgnoreCase(needle, skip_table[0..]);
|
boyerMooreHorspoolPreprocessIgnoreCase(needle, skip_table[0..]);
|
||||||
|
|
@ -383,9 +389,12 @@ pub fn indexOfIgnoreCasePos(haystack: []const u8, start_index: usize, needle: []
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consider using `indexOfIgnoreCasePos` instead of this, which will automatically use a
|
/// Deprecated in favor of `findIgnoreCaseLinear`.
|
||||||
|
pub const indexOfIgnoreCasePosLinear = findIgnoreCasePosLinear;
|
||||||
|
|
||||||
|
/// Consider using `findIgnoreCasePos` instead of this, which will automatically use a
|
||||||
/// more sophisticated algorithm on larger inputs.
|
/// more sophisticated algorithm on larger inputs.
|
||||||
pub fn indexOfIgnoreCasePosLinear(haystack: []const u8, start_index: usize, needle: []const u8) ?usize {
|
pub fn findIgnoreCasePosLinear(haystack: []const u8, start_index: usize, needle: []const u8) ?usize {
|
||||||
var i: usize = start_index;
|
var i: usize = start_index;
|
||||||
const end = haystack.len - needle.len;
|
const end = haystack.len - needle.len;
|
||||||
while (i <= end) : (i += 1) {
|
while (i <= end) : (i += 1) {
|
||||||
|
|
@ -407,15 +416,15 @@ fn boyerMooreHorspoolPreprocessIgnoreCase(pattern: []const u8, table: *[256]usiz
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test indexOfIgnoreCase {
|
test findIgnoreCase {
|
||||||
try std.testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);
|
try std.testing.expect(findIgnoreCase("one Two Three Four", "foUr").? == 14);
|
||||||
try std.testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);
|
try std.testing.expect(findIgnoreCase("one two three FouR", "gOur") == null);
|
||||||
try std.testing.expect(indexOfIgnoreCase("foO", "Foo").? == 0);
|
try std.testing.expect(findIgnoreCase("foO", "Foo").? == 0);
|
||||||
try std.testing.expect(indexOfIgnoreCase("foo", "fool") == null);
|
try std.testing.expect(findIgnoreCase("foo", "fool") == null);
|
||||||
try std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);
|
try std.testing.expect(findIgnoreCase("FOO foo", "fOo").? == 0);
|
||||||
|
|
||||||
try std.testing.expect(indexOfIgnoreCase("one two three four five six seven eight nine ten eleven", "ThReE fOUr").? == 8);
|
try std.testing.expect(findIgnoreCase("one two three four five six seven eight nine ten eleven", "ThReE fOUr").? == 8);
|
||||||
try std.testing.expect(indexOfIgnoreCase("one two three four five six seven eight nine ten eleven", "Two tWo") == null);
|
try std.testing.expect(findIgnoreCase("one two three four five six seven eight nine ten eleven", "Two tWo") == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the lexicographical order of two slices. O(n).
|
/// Returns the lexicographical order of two slices. O(n).
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue