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.

parseQuotedStringAsWideString

Parses any string type into a wide string. If the string is declared as a wide string (L""), then it is handled normally. Otherwise, things are fairly normal with the exception of escaped integers. Escaped integers are handled by:

literals.parseQuotedStringAsWideString
pub fn parseQuotedStringAsWideString(allocator: std.mem.Allocator, bytes: SourceBytes, options: StringParseOptions) ![:0]u16

File

lib/compiler/resinator/literals.zig:557

Code

pub fn parseQuotedStringAsWideString(allocator: std.mem.Allocator, bytes: SourceBytes, options: StringParseOptions) ![:0]u16 {
    std.debug.assert(bytes.slice.len >= 2); // ""

    if (bytes.slice[0] == 'l' or bytes.slice[0] == 'L') {
        return parseQuotedWideString(allocator, bytes, options);
    }

    // Note: We're only handling the case of parsing an ASCII string into a wide string from here on out.
    // TODO: The logic below is similar to that in AcceleratorKeyCodepointTranslator, might be worth merging the two

    var buf = try std.ArrayList(u16).initCapacity(allocator, bytes.slice.len);
    errdefer buf.deinit(allocator);

    var iterative_parser = IterativeStringParser.init(bytes, options);

    while (try iterative_parser.next()) |parsed| {
        const c = parsed.codepoint;
        if (parsed.from_escaped_integer) {
            std.debug.assert(c != code_pages.Codepoint.invalid);
            const byte_to_interpret: u8 = @truncate(c);
            const code_unit_to_encode: u16 = switch (options.output_code_page) {
                .windows1252 => windows1252.toCodepoint(byte_to_interpret),
                .utf8 => if (byte_to_interpret > 0x7F) '�' else byte_to_interpret,
            };
            try buf.append(allocator, std.mem.nativeToLittle(u16, code_unit_to_encode));
        } else if (c == code_pages.Codepoint.invalid) {
            try buf.append(allocator, std.mem.nativeToLittle(u16, '�'));
        } else if (c < 0x10000) {
            const short: u16 = @intCast(c);
            try buf.append(allocator, std.mem.nativeToLittle(u16, short));
        } else {
            if (!parsed.escaped_surrogate_pair) {
                const high = @as(u16, @intCast((c - 0x10000) >> 10)) + 0xD800;
                try buf.append(allocator, std.mem.nativeToLittle(u16, high));
            }
            const low = @as(u16, @intCast(c & 0x3FF)) + 0xDC00;
            try buf.append(allocator, std.mem.nativeToLittle(u16, low));
        }
    }

    return buf.toOwnedSliceSentinel(allocator, 0);
}