Skip to content

Apply rule X10 once and leave X9-removed characters out of the sequences - #16

Merged
isaranga merged 3 commits into
mainfrom
compute-irs-once-x10
Nov 27, 2026
Merged

isaranga merged 3 commits into
mainfrom
compute-irs-once-x10

Conversation

@isaranga

Copy link
Copy Markdown
Owner

Summary

  • X10 is a step of its own. apply_bidi_algorithm computes the isolating run sequences once, right after phase X, and passes them to apply_weak_rules and apply_neutral_rules. Before, each of those two phases computed them itself. Comments explain why running each phase across all sequences matches the spec's sequence-by-sequence order, and why phase I can run over the whole paragraph (I1 and I2 look only at each character's own type and level).
  • Characters removed by X9 belong to no sequence. get_isolating_run_sequences builds level runs only from the characters that X9 did not remove. Before, a BN took the level of the character before it, so the leading LRE in example 6 formed a one-character sequence of its own at level 0. The sequences now match the trace from the reference implementation (bidiref): one sequence, characters 1-10.
  • No more BN handling in the W and N rules. They never see a BN now, so the code that skipped over it is gone: four helpers, and BN branches in W1, W4, N0 and N1. W5 now finds each sequence of ETs and checks its two neighbours, which is how the rule is worded.
  • Verbose output. A new X10 section lists each sequence (level, characters, sos/eos) and the characters removed by X9.

The example 6 and 7 notes in demo.py, README.md and CLAUDE.md are updated to match.

Testing

  • tests/run_bidi_tests.py: 91707 passed, 0 failed
  • tests/run_bidi_type_tests.py: 770241 passed, 0 failed
  • python bidi.py --all runs every example without errors

apply_bidi_algorithm now calls get_isolating_run_sequences once, right
after phase X, and passes the result to apply_weak_rules and
apply_neutral_rules, which used to compute it themselves. Comments explain
why running each phase across all sequences matches the spec's
per-sequence order, and why phase I can run on the whole paragraph.

Verbose mode prints the sequences in a new X10 section
(print_isolating_run_sequences in verbose_table.py). README.md, CLAUDE.md,
example 7's note and the reviewing-functions command are updated to match.

Both conformance suites still pass in full.
get_isolating_run_sequences now builds level runs only from the
characters that X9 did not remove, and skips removed characters when it
computes sos and eos. Before, each BN took the level of the character
before it, so a leading LRE got the paragraph level and formed a
one-character sequence of its own (example 6). The sequences now match
the reference implementation's trace.

Verbose mode lists the removed characters after the X10 sequences.
Results are unchanged: both conformance suites pass in full.
Since X10 leaves the characters removed by X9 out of every isolating run
sequence, the W and N helpers never see a BN. Remove the code that
skipped over BN: the _get_prev_non_bn/_get_next_non_bn and
_get_prev_non_et_bn/_get_next_non_et_bn helpers, and the BN branches in
W1, W4, N0 and N1. W5 is rewritten to find each sequence of ETs and look
at its two neighbours, as the rule is worded.

Both conformance suites still pass in full.
@isaranga
isaranga merged commit 4e8a40c into main Nov 27, 2026
6 checks passed
@isaranga
isaranga deleted the compute-irs-once-x10 branch November 27, 2026 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant