DoxigAlpha

parseWrite

Parses bytes as a Zig string literal and writes the result to the Writer type.

Asserts bytes has '"' at beginning and end.

Function parameters

Parameters

#
writer:*Writer
bytes:[]const u8

Type definitions in this namespace

Types

#

Asserts the slice starts and ends with single-quotes.

Functions

#
parseCharLiteral
Asserts the slice starts and ends with single-quotes.
parseEscapeSequence
Parse an escape sequence from `slice[offset..]`.
parseWrite
Parses `bytes` as a Zig string literal and writes the result to the `Writer` type.
parseAlloc
Higher level API.

Error sets in this namespace

Error Sets

#

Source

Implementation

#
pub fn parseWrite(writer: *Writer, bytes: []const u8) Writer.Error!Result {
    assert(bytes.len >= 2 and bytes[0] == '"' and bytes[bytes.len - 1] == '"');

    var index: usize = 1;
    while (true) {
        const b = bytes[index];

        switch (b) {
            '\\' => {
                const escape_char_index = index + 1;
                const result = parseEscapeSequence(bytes, &index);
                switch (result) {
                    .success => |codepoint| {
                        if (bytes[escape_char_index] == 'u') {
                            var buf: [4]u8 = undefined;
                            const len = utf8Encode(codepoint, &buf) catch {
                                return .{ .failure = .{ .invalid_unicode_codepoint = escape_char_index + 1 } };
                            };
                            try writer.writeAll(buf[0..len]);
                        } else {
                            try writer.writeByte(@as(u8, @intCast(codepoint)));
                        }
                    },
                    .failure => |err| return .{ .failure = err },
                }
            },
            '\n' => return .{ .failure = .{ .invalid_character = index } },
            '"' => return .success,
            else => {
                try writer.writeByte(b);
                index += 1;
            },
        }
    }
}