feature. See also
. The project being documented here (as the example) is the Zig library itself.
AstGen.strLitAsString
fn strLitAsString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !IndexSlice
File
Code
fn strLitAsString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !IndexSlice {
const gpa = astgen.gpa;
const string_bytes = &astgen.string_bytes;
const str_index: u32 = @intCast(string_bytes.items.len);
const token_bytes = astgen.tree.tokenSlice(str_lit_token);
try astgen.parseStrLit(str_lit_token, string_bytes, token_bytes, 0);
const key: []const u8 = string_bytes.items[str_index..];
if (std.mem.findScalar(u8, key, 0)) |_| return .{
.index = @fromBackingInt(@intCast(str_index)),
.len = @intCast(key.len),
};
const gop = try astgen.string_table.getOrPutContextAdapted(gpa, key, StringIndexAdapter{
.bytes = string_bytes,
}, StringIndexContext{
.bytes = string_bytes,
});
if (gop.found_existing) {
string_bytes.shrinkRetainingCapacity(str_index);
return .{
.index = @fromBackingInt(@intCast(gop.key_ptr.*)),
.len = @intCast(key.len),
};
} else {
gop.key_ptr.* = str_index;
// to lookup null terminated strings, so if we get a match, it has to
// be null terminated for that to work.
try string_bytes.append(gpa, 0);
return .{
.index = @fromBackingInt(@intCast(str_index)),
.len = @intCast(key.len),
};
}
}