Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › zig › AstGen › strLitAsString Private Function strLitAsString Doc Source Private Function std.zig.AstGen.strLitAsString
fn strLitAsString (astgen : *AstGen , str_lit_token : Ast .TokenIndex ) !IndexSlice Params astgen : *AstGen str_lit_token : Ast .TokenIndex File zig/AstGen.zig: 11003
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036 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 ;
// Still need a null byte because we are using the same table
// 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 ),
};
}
}