terminal: small stylistic tweaks

This commit is contained in:
Mitchell Hashimoto
2024-02-05 21:20:20 -08:00
parent cd570890f6
commit 0c2a87e5fb
2 changed files with 14 additions and 13 deletions

View File

@ -43,10 +43,6 @@ accumulator: u21 = 0,
// The internal state of the DFA. // The internal state of the DFA.
state: u8 = ACCEPT_STATE, state: u8 = ACCEPT_STATE,
pub fn init() UTF8Decoder {
return .{};
}
/// Takes the next byte in the utf-8 sequence and emits a tuple of /// Takes the next byte in the utf-8 sequence and emits a tuple of
/// - The codepoint that was generated, if there is one. /// - The codepoint that was generated, if there is one.
/// - A boolean that indicates whether the provided byte was consumed. /// - A boolean that indicates whether the provided byte was consumed.
@ -72,7 +68,8 @@ pub inline fn next(self: *UTF8Decoder, byte: u8) struct { ?u21, bool } {
self.state = transitions[self.state + char_class]; self.state = transitions[self.state + char_class];
if (self.state == ACCEPT_STATE) { if (self.state == ACCEPT_STATE) {
defer { self.accumulator = 0; } defer self.accumulator = 0;
// Emit the fully decoded codepoint. // Emit the fully decoded codepoint.
return .{ self.accumulator, true }; return .{ self.accumulator, true };
} else if (self.state == REJECT_STATE) { } else if (self.state == REJECT_STATE) {
@ -88,8 +85,8 @@ pub inline fn next(self: *UTF8Decoder, byte: u8) struct { ?u21, bool } {
} }
test "ASCII" { test "ASCII" {
var d = init(); var d: UTF8Decoder = .{};
var out = std.mem.zeroes([13]u8); var out: [13]u8 = undefined;
for ("Hello, World!", 0..) |byte, i| { for ("Hello, World!", 0..) |byte, i| {
const res = d.next(byte); const res = d.next(byte);
try testing.expect(res[1]); try testing.expect(res[1]);
@ -97,12 +94,13 @@ test "ASCII" {
out[i] = @intCast(codepoint); out[i] = @intCast(codepoint);
} }
} }
try testing.expect(std.mem.eql(u8, &out, "Hello, World!")); try testing.expect(std.mem.eql(u8, &out, "Hello, World!"));
} }
test "Well formed utf-8" { test "Well formed utf-8" {
var d = init(); var d: UTF8Decoder = .{};
var out = std.mem.zeroes([4]u21); var out: [4]u21 = undefined;
var i: usize = 0; var i: usize = 0;
// 4 bytes, 3 bytes, 2 bytes, 1 byte // 4 bytes, 3 bytes, 2 bytes, 1 byte
for ("😄✤ÁA") |byte| { for ("😄✤ÁA") |byte| {
@ -119,12 +117,13 @@ test "Well formed utf-8" {
} }
} }
} }
try testing.expect(std.mem.eql(u21, &out, &[_]u21{ 0x1F604, 0x2724, 0xC1, 0x41 })); try testing.expect(std.mem.eql(u21, &out, &[_]u21{ 0x1F604, 0x2724, 0xC1, 0x41 }));
} }
test "Partially invalid utf-8" { test "Partially invalid utf-8" {
var d = init(); var d: UTF8Decoder = .{};
var out = std.mem.zeroes([5]u21); var out: [5]u21 = undefined;
var i: usize = 0; var i: usize = 0;
// Illegally terminated sequence, valid sequence, illegal surrogate pair. // Illegally terminated sequence, valid sequence, illegal surrogate pair.
for ("\xF0\x9F😄\xED\xA0\x80") |byte| { for ("\xF0\x9F😄\xED\xA0\x80") |byte| {
@ -138,5 +137,6 @@ test "Partially invalid utf-8" {
} }
} }
} }
try testing.expect(std.mem.eql(u21, &out, &[_]u21{ 0xFFFD, 0x1F604, 0xFFFD, 0xFFFD, 0xFFFD })); try testing.expect(std.mem.eql(u21, &out, &[_]u21{ 0xFFFD, 0x1F604, 0xFFFD, 0xFFFD, 0xFFFD }));
} }

View File

@ -67,6 +67,7 @@ pub fn Stream(comptime Handler: type) type {
} }
return; return;
} }
const actions = self.parser.next(c); const actions = self.parser.next(c);
for (actions) |action_opt| { for (actions) |action_opt| {
const action = action_opt orelse continue; const action = action_opt orelse continue;