DoxigAlpha

strLitSizeHint

Estimates the size of a string node without parsing it.

Function parameters

Parameters

#
node:Ast.Node.Index

Type definitions in this namespace

Types

#

Functions in this namespace

Functions

#
strLitSizeHint
Estimates the size of a string node without parsing it.
parseStrLit
Parses the given node as a string literal.

Source

Implementation

#
pub fn strLitSizeHint(tree: Ast, node: Ast.Node.Index) usize {
    switch (tree.nodeTag(node)) {
        // Parsed string literals are typically around the size of the raw strings.
        .string_literal => {
            const token = tree.nodeMainToken(node);
            const raw_string = tree.tokenSlice(token);
            return raw_string.len;
        },
        // Multiline string literal lengths can be computed exactly.
        .multiline_string_literal => {
            const first_tok, const last_tok = tree.nodeData(node).token_and_token;

            var size = tree.tokenSlice(first_tok)[2..].len;
            for (first_tok + 1..last_tok + 1) |tok_idx| {
                size += 1; // Newline
                size += tree.tokenSlice(@intCast(tok_idx))[2..].len;
            }
            return size;
        },
        else => unreachable,
    }
}