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" {
231231 );
232232}
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.
235235/// 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 {
237237 assert(bytes.len >= 2 and bytes[0] == '"' and bytes[bytes.len - 1] == '"');
238 try buf.ensureUnusedCapacity(bytes.len - 2);
239238
240239 var index: usize = 1;
241240 while (true) {
......@@ -248,11 +247,13 @@ pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory
248247 switch (result) {
249248 .success => |codepoint| {
250249 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 {
252252 return Result{ .failure = .{ .invalid_unicode_codepoint = escape_char_index + 1 } };
253253 };
254 try writer.writeAll(buf[0..len]);
254255 } else {
255 buf.appendAssumeCapacity(@intCast(u8, codepoint));
256 try writer.writeByte(@intCast(u8, codepoint));
256257 }
257258 },
258259 .failure => |err| return Result{ .failure = err },
......@@ -261,7 +262,7 @@ pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory
261262 '\n' => return Result{ .failure = .{ .invalid_character = index } },
262263 '"' => return Result.success,
263264 else => {
264 try buf.append(b);
265 try writer.writeByte(b);
265266 index += 1;
266267 },
267268 }
......@@ -280,44 +281,6 @@ pub fn parseAlloc(allocator: std.mem.Allocator, bytes: []const u8) ParseError![]
280281 }
281282}
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
321284test "parse" {
322285 const expect = std.testing.expect;
323286 const expectError = std.testing.expectError;
src/AstGen.zig+1-1
......@@ -9969,7 +9969,7 @@ fn parseStrLit(
99699969) InnerError!void {
99709970 const raw_string = bytes[offset..];
99719971 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);
99739973 buf.* = buf_managed.moveToUnmanaged();
99749974 switch (try result) {
99759975 .success => return,