Function indexOf [src]
Search for needle in haystack and return the index of the first occurrence.
Uses Boyer-Moore-Horspool algorithm on large inputs; linear search on small inputs.
Returns null if needle is not found.
Prototype
pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize
Parameters
T: type
haystack: []const T
needle: []const T
Example
test indexOf {
try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);
try testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);
try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "two two") == null);
try testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "two two") == null);
try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "").? == 0);
try testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "").? == 48);
try testing.expect(indexOf(u8, "one two three four", "four").? == 14);
try testing.expect(lastIndexOf(u8, "one two three two four", "two").? == 14);
try testing.expect(indexOf(u8, "one two three four", "gour") == null);
try testing.expect(lastIndexOf(u8, "one two three four", "gour") == null);
try testing.expect(indexOf(u8, "foo", "foo").? == 0);
try testing.expect(lastIndexOf(u8, "foo", "foo").? == 0);
try testing.expect(indexOf(u8, "foo", "fool") == null);
try testing.expect(lastIndexOf(u8, "foo", "lfoo") == null);
try testing.expect(lastIndexOf(u8, "foo", "fool") == null);
try testing.expect(indexOf(u8, "foo foo", "foo").? == 0);
try testing.expect(lastIndexOf(u8, "foo foo", "foo").? == 4);
try testing.expect(lastIndexOfAny(u8, "boo, cat", "abo").? == 6);
try testing.expect(lastIndexOfScalar(u8, "boo", 'o').? == 2);
}
Source
pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {
return indexOfPos(T, haystack, 0, needle);
}