Sometimes I want to know if a string is present in an array of strings.
For example:
const my_fruits = [_][]const u8{
"apple",
"banana",
"avocado",
};
const i_have_banana: bool = indexOfScalar([]const u8, &my_fruits, "banana") == null;
Today, it fails because std.mem.indexOfScalarPos try to compare array:
pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize {
if (start_index >= slice.len) return null;
var i: usize = start_index;
// some low level optimisations I don't care about this time
for (slice[i..], i..) |c, j| {
if (c == value) return j; // here, the fail
}
return null;
}
What if instead it was this:
pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize {
if (start_index >= slice.len) return null;
var i: usize = start_index;
// some low level optimisations I don't care about this time
for (slice[i..], i..) |c, j| {
if (@typeInfo(T) == .pointer and @typeInfo(T).pointer.size == .slice) {
if (std.mem.eql(@typeInfo(T).pointer.child, c, value)) return j;
} else {
if (c == value) return j;
}
}
return null;
}
With this modification, the code mentioned above works.
Do you think I can consider opening an issue and a pull request on GitHub, or there are some downsides and traps I didnāt see ?