Function parseWrite [src]
Parses bytes as a Zig string literal and writes the result to the Writer type.
Asserts bytes has '"' at beginning and end.
Prototype
pub fn parseWrite(writer: *Writer, bytes: []const u8) Writer.Error!Result
Parameters
writer: *Writer
bytes: []const u8
Source
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;
},
}
}
}