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 library itself.

CodePageLookup

ast.CodePageLookup
pub const CodePageLookup = struct

File

Code

pub const CodePageLookup = struct {
    lookup: std.ArrayList(SupportedCodePage) = .empty,
    allocator: Allocator,
    default_code_page: SupportedCodePage,

    pub fn init(allocator: Allocator, default_code_page: SupportedCodePage) CodePageLookup {
        return .{
            .allocator = allocator,
            .default_code_page = default_code_page,
        };
    }

    pub fn deinit(self: *CodePageLookup) void {
        self.lookup.deinit(self.allocator);
    }

    /// line_num is 1-indexed
    pub fn setForLineNum(self: *CodePageLookup, line_num: usize, code_page: SupportedCodePage) !void {
        const index = line_num - 1;
        if (index >= self.lookup.items.len) {
            const new_size = line_num;
            const missing_lines_start_index = self.lookup.items.len;
            try self.lookup.resize(self.allocator, new_size);

            // If there are any gaps created, we need to fill them in with the value of the
            // last line before the gap. This can happen for e.g. string literals that
            // span multiple lines, or if the start of a file has multiple empty lines.
            const fill_value = if (missing_lines_start_index > 0)
                self.lookup.items[missing_lines_start_index - 1]
            else
                self.default_code_page;
            var i: usize = missing_lines_start_index;
            while (i < new_size - 1) : (i += 1) {
                self.lookup.items[i] = fill_value;
            }
        }
        self.lookup.items[index] = code_page;
    }

    pub fn setForToken(self: *CodePageLookup, token: Token, code_page: SupportedCodePage) !void {
        return self.setForLineNum(token.line_number, code_page);
    }

    /// line_num is 1-indexed
    pub fn getForLineNum(self: CodePageLookup, line_num: usize) SupportedCodePage {
        return self.lookup.items[line_num - 1];
    }

    pub fn getForToken(self: CodePageLookup, token: Token) SupportedCodePage {
        return self.getForLineNum(token.line_number);
    }
}