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.

nextAllocMax

This function is only available after endInput() (or initCompleteInput()) has been called. See also std.json.Token for documentation of nextAlloc*() function behavior.

Scanner.nextAllocMax
pub fn nextAllocMax(self: *@This(), allocator: Allocator, when: AllocWhen, max_value_len: usize) AllocError!Token

File

lib/std/json/Scanner.zig:130

Code

pub fn nextAllocMax(self: *@This(), allocator: Allocator, when: AllocWhen, max_value_len: usize) AllocError!Token {
    assert(self.is_end_of_input); // This function is not available in streaming mode.
    const token_type = self.peekNextTokenType() catch |e| switch (e) {
        error.BufferUnderrun => unreachable,
        else => |err| return err,
    };
    switch (token_type) {
        .number, .string => {
            var value_list = std.array_list.Managed(u8).init(allocator);
            errdefer {
                value_list.deinit();
            }
            if (self.allocNextIntoArrayListMax(&value_list, when, max_value_len) catch |e| switch (e) {
                error.BufferUnderrun => unreachable,
                else => |err| return err,
            }) |slice| {
                return if (token_type == .number)
                    Token{ .number = slice }
                else
                    Token{ .string = slice };
            } else {
                return if (token_type == .number)
                    Token{ .allocated_number = try value_list.toOwnedSlice() }
                else
                    Token{ .allocated_string = try value_list.toOwnedSlice() };
            }
        },

        // Simple tokens never alloc.
        .object_begin,
        .object_end,
        .array_begin,
        .array_end,
        .true,
        .false,
        .null,
        .end_of_document,
        => return self.next() catch |e| switch (e) {
            error.BufferUnderrun => unreachable,
            else => |err| return err,
        },
    }
}