std: replace parseAppend with parseWrite in std.zig.string_literal

This commit is contained in:
Stevie Hryciw
2022-11-08 20:42:43 -08:00
committed by Isaac Freund
parent ca9e1760e8
commit e999f9f472
2 changed files with 14 additions and 51 deletions

View File

@@ -231,55 +231,6 @@ test "parseCharLiteral" {
);
}
/// Parses `bytes` as a Zig string literal and appends the result to `buf`.
/// Asserts `bytes` has '"' at beginning and end.
pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory}!Result {
assert(bytes.len >= 2 and bytes[0] == '"' and bytes[bytes.len - 1] == '"');
try buf.ensureUnusedCapacity(bytes.len - 2);
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') {
buf.items.len += utf8Encode(codepoint, buf.unusedCapacitySlice()) catch {
return Result{ .failure = .{ .invalid_unicode_codepoint = escape_char_index + 1 } };
};
} else {
buf.appendAssumeCapacity(@intCast(u8, codepoint));
}
},
.failure => |err| return Result{ .failure = err },
}
},
'\n' => return Result{ .failure = .{ .invalid_character = index } },
'"' => return Result.success,
else => {
try buf.append(b);
index += 1;
},
}
} else unreachable; // TODO should not need else unreachable on while(true)
}
/// Higher level API. Does not return extra info about parse errors.
/// Caller owns returned memory.
pub fn parseAlloc(allocator: std.mem.Allocator, bytes: []const u8) ParseError![]u8 {
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();
switch (try parseWrite(buf.writer(), bytes)) {
.success => return buf.toOwnedSlice(),
.failure => return error.InvalidLiteral,
}
}
/// Parses `bytes` as a Zig string literal and writes the result to the std.io.Writer type.
/// Asserts `bytes` has '"' at beginning and end.
pub fn parseWrite(writer: anytype, bytes: []const u8) error{OutOfMemory}!Result {
@@ -296,7 +247,7 @@ pub fn parseWrite(writer: anytype, bytes: []const u8) error{OutOfMemory}!Result
switch (result) {
.success => |codepoint| {
if (bytes[escape_char_index] == 'u') {
var buf: [3]u8 = undefined;
var buf: [4]u8 = undefined;
const len = utf8Encode(codepoint, &buf) catch {
return Result{ .failure = .{ .invalid_unicode_codepoint = escape_char_index + 1 } };
};
@@ -318,6 +269,18 @@ pub fn parseWrite(writer: anytype, bytes: []const u8) error{OutOfMemory}!Result
} else unreachable; // TODO should not need else unreachable on while(true)
}
/// Higher level API. Does not return extra info about parse errors.
/// Caller owns returned memory.
pub fn parseAlloc(allocator: std.mem.Allocator, bytes: []const u8) ParseError![]u8 {
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();
switch (try parseWrite(buf.writer(), bytes)) {
.success => return buf.toOwnedSlice(),
.failure => return error.InvalidLiteral,
}
}
test "parse" {
const expect = std.testing.expect;
const expectError = std.testing.expectError;

View File

@@ -9969,7 +9969,7 @@ fn parseStrLit(
) InnerError!void {
const raw_string = bytes[offset..];
var buf_managed = buf.toManaged(astgen.gpa);
const result = std.zig.string_literal.parseAppend(&buf_managed, raw_string);
const result = std.zig.string_literal.parseWrite(buf_managed.writer(), raw_string);
buf.* = buf_managed.moveToUnmanaged();
switch (try result) {
.success => return,