Commit e999f9f472

Stevie Hryciw <codroid@gmail.com>
2022-11-09 05:42:43
std: replace parseAppend with parseWrite in std.zig.string_literal
1 parent ca9e176
Changed files (2)
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;
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,