Function parseWrite [src]
Parses bytes as a Zig string literal and writes the result to the std.io.Writer type.
Asserts bytes has '"' at beginning and end.
Prototype
pub fn parseWrite(writer: anytype, bytes: []const u8) error{OutOfMemory}!Result
Parameters
bytes: []const u8
Possible Errors
Source
pub fn parseWrite(writer: anytype, bytes: []const u8) error{OutOfMemory}!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 Result{ .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 Result{ .failure = err },
}
},
'\n' => return Result{ .failure = .{ .invalid_character = index } },
'"' => return Result.success,
else => {
try writer.writeByte(b);
index += 1;
},
}
}
}