diff options
| author | 2024-02-14 12:11:07 -0400 | |
|---|---|---|
| committer | 2024-02-14 12:11:07 -0400 | |
| commit | 95bc908ed25be9fa597c559791cbf6d5f5a6b8ed (patch) | |
| tree | 0b8e83d4f9c6e701a0c55e134159f2eea1740f8f /src/CodePoint.zig | |
| parent | Removed unreachables from Grapheme (diff) | |
| download | zg-95bc908ed25be9fa597c559791cbf6d5f5a6b8ed.tar.gz zg-95bc908ed25be9fa597c559791cbf6d5f5a6b8ed.tar.xz zg-95bc908ed25be9fa597c559791cbf6d5f5a6b8ed.zip | |
Removed readCodePoint and StreamingGraphemeIterator
Diffstat (limited to 'src/CodePoint.zig')
| -rw-r--r-- | src/CodePoint.zig | 50 |
1 files changed, 0 insertions, 50 deletions
diff --git a/src/CodePoint.zig b/src/CodePoint.zig index e72823b..c03ecac 100644 --- a/src/CodePoint.zig +++ b/src/CodePoint.zig | |||
| @@ -79,53 +79,3 @@ test "CodePointIterator peek" { | |||
| 79 | try std.testing.expectEqual(@as(?CodePoint, null), iter.peek()); | 79 | try std.testing.expectEqual(@as(?CodePoint, null), iter.peek()); |
| 80 | try std.testing.expectEqual(@as(?CodePoint, null), iter.next()); | 80 | try std.testing.expectEqual(@as(?CodePoint, null), iter.next()); |
| 81 | } | 81 | } |
| 82 | |||
| 83 | /// `readCodePoint` returns the next code point code as a `u21` in the given reader, or null at end-of-input. | ||
| 84 | pub fn readCodePoint(reader: anytype) !?u21 { | ||
| 85 | var buf: [4]u8 = undefined; | ||
| 86 | |||
| 87 | buf[0] = reader.readByte() catch |err| switch (err) { | ||
| 88 | error.EndOfStream => return null, | ||
| 89 | else => return err, | ||
| 90 | }; | ||
| 91 | |||
| 92 | if (buf[0] < 128) return @as(u21, buf[0]); | ||
| 93 | |||
| 94 | const len: u3 = switch (buf[0]) { | ||
| 95 | 0b1100_0000...0b1101_1111 => 2, | ||
| 96 | 0b1110_0000...0b1110_1111 => 3, | ||
| 97 | 0b1111_0000...0b1111_0111 => 4, | ||
| 98 | else => return error.InvalidUtf8, | ||
| 99 | }; | ||
| 100 | |||
| 101 | const read = try reader.read(buf[1..len]); | ||
| 102 | |||
| 103 | if (read < len - 1) return error.InvalidUtf8; | ||
| 104 | |||
| 105 | return switch (len) { | ||
| 106 | 2 => (@as(u21, (buf[0] & 0b00011111)) << 6) | (buf[1] & 0b00111111), | ||
| 107 | |||
| 108 | 3 => (((@as(u21, (buf[0] & 0b00001111)) << 6) | | ||
| 109 | (buf[1] & 0b00111111)) << 6) | | ||
| 110 | (buf[2] & 0b00111111), | ||
| 111 | |||
| 112 | 4 => (((((@as(u21, (buf[0] & 0b00000111)) << 6) | | ||
| 113 | (buf[1] & 0b00111111)) << 6) | | ||
| 114 | (buf[2] & 0b00111111)) << 6) | | ||
| 115 | (buf[3] & 0b00111111), | ||
| 116 | |||
| 117 | else => @panic("readCodePoint invalid code point length."), | ||
| 118 | }; | ||
| 119 | } | ||
| 120 | |||
| 121 | test "readCodePoint" { | ||
| 122 | var buf = "abé😹".*; | ||
| 123 | var fis = std.io.fixedBufferStream(&buf); | ||
| 124 | const reader = fis.reader(); | ||
| 125 | |||
| 126 | try std.testing.expectEqual(@as(u21, 'a'), (try readCodePoint(reader)).?); | ||
| 127 | try std.testing.expectEqual(@as(u21, 'b'), (try readCodePoint(reader)).?); | ||
| 128 | try std.testing.expectEqual(@as(u21, 'é'), (try readCodePoint(reader)).?); | ||
| 129 | try std.testing.expectEqual(@as(u21, '😹'), (try readCodePoint(reader)).?); | ||
| 130 | try std.testing.expectEqual(@as(?u21, null), try readCodePoint(reader)); | ||
| 131 | } | ||