I'm trying to write a toy parser in Zig, capable of parsing literals from some input string.
I'm unable to figure out how to define the function (and testing it tbh) to account for this.
So here's what I want:
/// Attempt to parse a literal from the text,
/// returning the length of the literal on success,
/// otherwise 0.
fn parseLiteral(literal: *const []u8, text: []u8) usize {
// ...
}
This doesn't work for literal: *const []u8, as the following test fails:
test "parseLiteral: del -> 3" {
var data = "del";
const results = parseLiteral("del", &data);
try std.testing.expectEqual(3, results);
}
Error:
error: expected type '*const []u8', found '*const [3:0]u8'
const results = parseLiteral("del", &data);
^~~~~
note: pointer type child 'u8' cannot cast into pointer type child '[]u8'
note: parameter type declared here
fn parseLiteral(literal: *const []u8, data: []u8) usize {
Since the literals will not all be the same length, is there some way to be generic over the length of the constant, or what is the best way to work with constant literals of varying lengths in function parameters?