authorgravatar for codroid@gmail.comStevie Hryciw <codroid@gmail.com> 2022-11-08 20:42:43-08:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2022-11-18 19:22:42+00:00
loge999f9f472f6b57214a8a66b69b8928ad28acbfe
tree350f5bd5cc227fdd848498d354f9109ab47af41c
parentca9e1760e8e33d17f44835d93103b940873417cf

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


2 files changed, 8 insertions(+), 45 deletions(-)

lib/std/zig/string_literal.zig+7-44
...@@ -231,11 +231,10 @@ test "parseCharLiteral" {...@@ -231,11 +231,10 @@ test "parseCharLiteral" {
231 );231 );
232}232}
233233
234/// Parses `bytes` as a Zig string literal and appends the result to `buf`.234/// Parses `bytes` as a Zig string literal and writes the result to the std.io.Writer type.
235/// Asserts `bytes` has '"' at beginning and end.235/// Asserts `bytes` has '"' at beginning and end.
236pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory}!Result {236pub fn parseWrite(writer: anytype, bytes: []const u8) error{OutOfMemory}!Result {
237 assert(bytes.len >= 2 and bytes[0] == '"' and bytes[bytes.len - 1] == '"');237 assert(bytes.len >= 2 and bytes[0] == '"' and bytes[bytes.len - 1] == '"');
238 try buf.ensureUnusedCapacity(bytes.len - 2);
239238
240 var index: usize = 1;239 var index: usize = 1;
241 while (true) {240 while (true) {
...@@ -248,11 +247,13 @@ pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory...@@ -248,11 +247,13 @@ pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory
248 switch (result) {247 switch (result) {
249 .success => |codepoint| {248 .success => |codepoint| {
250 if (bytes[escape_char_index] == 'u') {249 if (bytes[escape_char_index] == 'u') {
251 buf.items.len += utf8Encode(codepoint, buf.unusedCapacitySlice()) catch {250 var buf: [4]u8 = undefined;
251 const len = utf8Encode(codepoint, &buf) catch {
252 return Result{ .failure = .{ .invalid_unicode_codepoint = escape_char_index + 1 } };252 return Result{ .failure = .{ .invalid_unicode_codepoint = escape_char_index + 1 } };
253 };253 };
254 try writer.writeAll(buf[0..len]);
254 } else {255 } else {
255 buf.appendAssumeCapacity(@intCast(u8, codepoint));256 try writer.writeByte(@intCast(u8, codepoint));
256 }257 }
257 },258 },
258 .failure => |err| return Result{ .failure = err },259 .failure => |err| return Result{ .failure = err },
...@@ -261,7 +262,7 @@ pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory...@@ -261,7 +262,7 @@ pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory
261 '\n' => return Result{ .failure = .{ .invalid_character = index } },262 '\n' => return Result{ .failure = .{ .invalid_character = index } },
262 '"' => return Result.success,263 '"' => return Result.success,
263 else => {264 else => {
264 try buf.append(b);265 try writer.writeByte(b);
265 index += 1;266 index += 1;
266 },267 },
267 }268 }
...@@ -280,44 +281,6 @@ pub fn parseAlloc(allocator: std.mem.Allocator, bytes: []const u8) ParseError![]...@@ -280,44 +281,6 @@ pub fn parseAlloc(allocator: std.mem.Allocator, bytes: []const u8) ParseError![]
280 }281 }
281}282}
282283
283/// Parses `bytes` as a Zig string literal and writes the result to the std.io.Writer type.
284/// Asserts `bytes` has '"' at beginning and end.
285pub fn parseWrite(writer: anytype, bytes: []const u8) error{OutOfMemory}!Result {
286 assert(bytes.len >= 2 and bytes[0] == '"' and bytes[bytes.len - 1] == '"');
287
288 var index: usize = 1;
289 while (true) {
290 const b = bytes[index];
291
292 switch (b) {
293 '\\' => {
294 const escape_char_index = index + 1;
295 const result = parseEscapeSequence(bytes, &index);
296 switch (result) {
297 .success => |codepoint| {
298 if (bytes[escape_char_index] == 'u') {
299 var buf: [3]u8 = undefined;
300 const len = utf8Encode(codepoint, &buf) catch {
301 return Result{ .failure = .{ .invalid_unicode_codepoint = escape_char_index + 1 } };
302 };
303 try writer.writeAll(buf[0..len]);
304 } else {
305 try writer.writeByte(@intCast(u8, codepoint));
306 }
307 },
308 .failure => |err| return Result{ .failure = err },
309 }
310 },
311 '\n' => return Result{ .failure = .{ .invalid_character = index } },
312 '"' => return Result.success,
313 else => {
314 try writer.writeByte(b);
315 index += 1;
316 },
317 }
318 } else unreachable; // TODO should not need else unreachable on while(true)
319}
320
321test "parse" {284test "parse" {
322 const expect = std.testing.expect;285 const expect = std.testing.expect;
323 const expectError = std.testing.expectError;286 const expectError = std.testing.expectError;
src/AstGen.zig+1-1
...@@ -9969,7 +9969,7 @@ fn parseStrLit(...@@ -9969,7 +9969,7 @@ fn parseStrLit(
9969) InnerError!void {9969) InnerError!void {
9970 const raw_string = bytes[offset..];9970 const raw_string = bytes[offset..];
9971 var buf_managed = buf.toManaged(astgen.gpa);9971 var buf_managed = buf.toManaged(astgen.gpa);
9972 const result = std.zig.string_literal.parseAppend(&buf_managed, raw_string);9972 const result = std.zig.string_literal.parseWrite(buf_managed.writer(), raw_string);
9973 buf.* = buf_managed.moveToUnmanaged();9973 buf.* = buf_managed.moveToUnmanaged();
9974 switch (try result) {9974 switch (try result) {
9975 .success => return,9975 .success => return,