| ... | @@ -1,6 +1,5 @@ | ... | @@ -1,6 +1,5 @@ |
| 1 | const std = @import("../std.zig"); | 1 | const std = @import("../std.zig"); |
| 2 | const assert = std.debug.assert; | 2 | const assert = std.debug.assert; |
| 3 | const utf8Encode = std.unicode.utf8Encode; | | |
| 4 | const Writer = std.Io.Writer; | 3 | const Writer = std.Io.Writer; |
| 5 | | 4 | |
| 6 | pub const ParseError = error{ | 5 | pub const ParseError = error{ |
| ... | @@ -63,7 +62,7 @@ pub const Error = union(enum) { | ... | @@ -63,7 +62,7 @@ pub const Error = union(enum) { |
| 63 | .{self.raw_string[bad_index]}, | 62 | .{self.raw_string[bad_index]}, |
| 64 | ), | 63 | ), |
| 65 | .invalid_unicode_codepoint => try writer.writeAll( | 64 | .invalid_unicode_codepoint => try writer.writeAll( |
| 66 | "unicode escape does not correspond to a valid unicode scalar value", | 65 | "unicode escape does not correspond to a unicode code point", |
| 67 | ), | 66 | ), |
| 68 | .expected_lbrace => |bad_index| try writer.print( | 67 | .expected_lbrace => |bad_index| try writer.print( |
| 69 | "expected '{{', found '{c}'", | 68 | "expected '{{', found '{c}'", |
| ... | @@ -334,8 +333,8 @@ pub fn parseWrite(writer: *Writer, bytes: []const u8) Writer.Error!Result { | ... | @@ -334,8 +333,8 @@ pub fn parseWrite(writer: *Writer, bytes: []const u8) Writer.Error!Result { |
| 334 | .success => |codepoint| { | 333 | .success => |codepoint| { |
| 335 | if (bytes[escape_char_index] == 'u') { | 334 | if (bytes[escape_char_index] == 'u') { |
| 336 | var buf: [4]u8 = undefined; | 335 | var buf: [4]u8 = undefined; |
| 337 | const len = utf8Encode(codepoint, &buf) catch { | 336 | const len = std.unicode.wtf8Encode(codepoint, &buf) catch |err| switch (err) { |
| 338 | return .{ .failure = .{ .invalid_unicode_codepoint = escape_char_index + 1 } }; | 337 | error.CodepointTooLarge => unreachable, // Checked in `parseEscapeSequence`. |
| 339 | }; | 338 | }; |
| 340 | try writer.writeAll(buf[0..len]); | 339 | try writer.writeAll(buf[0..len]); |
| 341 | } else { | 340 | } else { |
| ... | @@ -385,4 +384,5 @@ test parseAlloc { | ... | @@ -385,4 +384,5 @@ test parseAlloc { |
| 385 | try expect(eql(u8, "foo", try parseAlloc(alloc, "\"foo\""))); | 384 | try expect(eql(u8, "foo", try parseAlloc(alloc, "\"foo\""))); |
| 386 | try expect(eql(u8, "foo", try parseAlloc(alloc, "\"f\x6f\x6f\""))); | 385 | try expect(eql(u8, "foo", try parseAlloc(alloc, "\"f\x6f\x6f\""))); |
| 387 | try expect(eql(u8, "f💯", try parseAlloc(alloc, "\"f\u{1f4af}\""))); | 386 | try expect(eql(u8, "f💯", try parseAlloc(alloc, "\"f\u{1f4af}\""))); |
| | 387 | try expect(eql(u8, "\xed\xa0\x80", try parseAlloc(alloc, "\"\\u{d800}\""))); |
| 388 | } | 388 | } |