ReviewOS

also looking at this

den-shell/den

feat(line-editor): UTF-8-aware input, cursor movement, and deletion

#57
Merged chris wants to merge feat/utf8-input-editing into main
1 file +61 -12
8 days ago

What

The line editor was byte-oriented end to end, so multibyte UTF-8 (accented Latin, CJK, emoji) was broken three ways:

  • Input: bytes ≥ 0x80 fell through the key-dispatch else arm and were dropped — you couldn't even type café.
  • Cursor: Left/Right moved one byte, landing inside a codepoint.
  • Deletion: backspace/deleteChar removed one byte → dangling continuation byte (corrupt text); cursor reposition counted bytes not display columns, over-shooting past multibyte chars.

Adds UTF-8 helpers (isUtf8Continuation, utf8SeqLen, prevCodepointLen, curCodepointLen) and:

  • insertUtf8(lead) assembles a full codepoint (reading continuation bytes) and inserts it atomically; wired via a 0xC2...0xF4 dispatch arm.
  • moveCursorLeft/Right step one whole codepoint.
  • backspace/deleteChar remove the whole codepoint at/before the cursor.
  • insert/backspace/delete reposition the cursor by display columns (one per codepoint), not bytes.

ASCII is unchanged (every helper returns 1 for ASCII bytes).

Verification

  • zig build clean (exit 0).
  • PTY: typing echo café prints café; one Backspace → caf; with echo Xé, Left+Left then A → AXé; ASCII regression echo abXcd → mid-line backspace → aXcd still works.

Scope: byte-vs-codepoint correctness. Wide-character (CJK/emoji width-2) column alignment — treating one codepoint as two columns — is a separate follow-up; this treats one codepoint as one column, correct for accented Latin and the common cases.

1 changed file on the files tab, with 0 review threads.

Sign in to comment on this pull request.