From e999f9f472f6b57214a8a66b69b8928ad28acbfe Mon Sep 17 00:00:00 2001 From: Stevie Hryciw Date: Tue, 8 Nov 2022 20:42:43 -0800 Subject: [PATCH] std: replace parseAppend with parseWrite in std.zig.string_literal --- lib/std/zig/string_literal.zig | 51 +++++----------------------------- src/AstGen.zig | 2 +- 2 files changed, 8 insertions(+), 45 deletions(-) diff --git a/lib/std/zig/string_literal.zig b/lib/std/zig/string_literal.zig index 4c12126d1c8a05c4bf898933cbf64d03d5266639..22b8f68fa7cf457be731d29485010848fd71ee00 100644 --- a/lib/std/zig/string_literal.zig +++ b/lib/std/zig/string_literal.zig @@ -231,11 +231,10 @@ test "parseCharLiteral" { ); } -/// Parses `bytes` as a Zig string literal and appends the result to `buf`. +/// 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 parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory}!Result { +pub fn parseWrite(writer: anytype, 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) { @@ -248,11 +247,13 @@ pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory switch (result) { .success => |codepoint| { if (bytes[escape_char_index] == 'u') { - buf.items.len += utf8Encode(codepoint, buf.unusedCapacitySlice()) catch { + 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 { - buf.appendAssumeCapacity(@intCast(u8, codepoint)); + try writer.writeByte(@intCast(u8, codepoint)); } }, .failure => |err| return Result{ .failure = err }, @@ -261,7 +262,7 @@ pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory '\n' => return Result{ .failure = .{ .invalid_character = index } }, '"' => return Result.success, else => { - try buf.append(b); + try writer.writeByte(b); index += 1; }, } @@ -280,44 +281,6 @@ pub fn parseAlloc(allocator: std.mem.Allocator, bytes: []const u8) ParseError![] } } -/// 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 { - 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: [3]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(@intCast(u8, codepoint)); - } - }, - .failure => |err| return Result{ .failure = err }, - } - }, - '\n' => return Result{ .failure = .{ .invalid_character = index } }, - '"' => return Result.success, - else => { - try writer.writeByte(b); - index += 1; - }, - } - } else unreachable; // TODO should not need else unreachable on while(true) -} - test "parse" { const expect = std.testing.expect; const expectError = std.testing.expectError; diff --git a/src/AstGen.zig b/src/AstGen.zig index 2910937d6659ee79814816792a0213302a0f9a0b..b0200aad4ce3f42e4a53b7e6707182d38925eb66 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -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, -- 2.54.0