szn

Bug 503 — cellHasMaiHanAkat raw-codepoint check compares table index to raw codepoint

503. Competing conventions: raw codepoint branch is dead and risks false positives

Severity: LOW
Status: 🟢 FIXED
Files: src/thai.zig:96-105, src/char_width.zig:198-219

Problem

In src/thai.zig, cellHasMaiHanAkat checks whether a cell contains MAI HAN AKAT (U+0E31):

pub fn cellHasMaiHanAkat(cell: Cell) bool {
    if (cell.char == 0x0E31) return true;
    if (cell.comb1 != 0) {
        if (cell.comb1 == 0x0E31 or char_width.combiningCodepoint(cell.comb1) == 0x0E31) return true;
    }
    if (cell.comb2 != 0) {
        if (cell.comb2 == 0x0E31 or char_width.combiningCodepoint(cell.comb2) == 0x0E31) return true;
    }
    return false;
}

The fields Cell.comb1 and Cell.comb2 are defined as u13 (13 bits, range 0..8191). In the actual terminal parser and grid pipeline (src/screen.zig:808, 856), combining codepoints are converted to 1-based table indices using char_width.combiningIndex(cp):

prev_cell.comb1 = char_width.combiningIndex(cp);

All subsystems (rendering in src/server/render.zig, TTY output in src/tty/tty.zig, copy mode in src/mode_copy.zig, and libthai conversion in src/thai.zig:522) resolve combining marks via char_width.combiningCodepoint(cell.comb1).

Comparing cell.comb1 == 0x0E31 directly:

  1. Is completely dead in production because comb1 holds an index (~300), not the raw codepoint (0x0E31 = 3633).
  2. Is conceptually hazardous: if the combining mark table ever grew to contain 3,633 entries, whatever unrelated mark mapped to index 3,633 would be falsely identified as MAI HAN AKAT.
  3. Only existed due to a few ad-hoc unit test initializations in src/grid.zig that bypassed combiningIndex.

Proposed Fix

  1. In src/thai.zig, simplify cellHasMaiHanAkat to consistently check only via char_width.combiningCodepoint:
    pub fn cellHasMaiHanAkat(cell: Cell) bool {
        if (cell.char == 0x0E31) return true;
        if (cell.comb1 != 0 and char_width.combiningCodepoint(cell.comb1) == 0x0E31) return true;
        if (cell.comb2 != 0 and char_width.combiningCodepoint(cell.comb2) == 0x0E31) return true;
        return false;
    }
    
  2. Ensure all tests store combining marks via char_width.combiningIndex(cp) instead of raw codepoint literals.

Resolution