A comptime parser transforms input into program values during compilation. For a wordlist, Zig can count newline-delimited words first, then use that count as the length of the returned array. section 4.1.3
A common pattern is to stream through the input with an iterator, count its values, and write them into an array. The wordlist parser tokenizes twice: once to count and once to fill the array. Zig memoizes side-effect-free comptime calls with the same input. section 4.1.3
Listing 4.4 Parsing the wordlist into an array
fn parseWordlist(
comptime wordlist: []const u8, #1
) [countWordlist(wordlist)][4]u8 { #2
var list: [countWordlist(wordlist)][4]u8 = undefined;
var i: usize = 0;
var word_iter = std.mem.tokenizeScalar(u8, wordlist, '\n');
while (word_iter.next()) |word| : (i += 1) { #3
if (word.len != 4) {
@compileError(std.fmt.comptimePrint( #4
"the word '{s}' is not four characters long.",
.{word},
));
}
list[i] = word[0..4].*; #5
}
return list;
}
The parser returns an array of four-byte words and uses @compileError with std.fmt.comptimePrint to reject a word of the wrong length. section 4.1.3
Listing 4.6 Using our parser
const words = blk: { #1
@setEvalBranchQuota(1_000_000); #2
break :blk parseWordlist(@embedFile("words.txt")); #3
};
A labeled block raises the evaluation branch quota, embeds the wordlist file, and returns the parsed array. section 4.1.3
