Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .claude/commands/reviewing-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Work through the function body from the `def` statement through the last line be

### UAX#9 context for bidi.py

The data model centers on `CharacterInfo` — `original_type` is immutable, `character_type` is the working copy mutated per phase. Phase-orchestrating functions delegate to `_apply_*` helpers; the W and N phases (`apply_weak_rules`, `apply_neutral_rules`) also call `get_isolating_run_sequences`. `print_table` handles verbose output uniformly. Flag any divergence from these conventions.
The data model centers on `CharacterInfo` — `original_type` is immutable, `character_type` is the working copy mutated per phase. Phase-orchestrating functions delegate to `_apply_*` helpers; `apply_bidi_algorithm` computes the isolating run sequences once (X10, `get_isolating_run_sequences`) and passes them to the W and N phases (`apply_weak_rules`, `apply_neutral_rules`); the I phase works on the whole paragraph, since I1/I2 are per-character. `print_table` handles verbose output uniformly. Flag any divergence from these conventions.

## Step 3 — Produce the findings table

Expand Down
7 changes: 4 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,27 @@ levels, reordered, para_level, visual_order = apply_bidi_algorithm("Hello שלו

Rule P1 is `split_into_paragraphs`, which works on the raw string and runs before everything else: it splits after each type-B character (CR LF counts as one), keeping the separator at the end of its paragraph. `apply_bidi_algorithm` handles **one paragraph** (a B may only be its last character); callers with longer text call it once per paragraph. The conformance files contain no B except at the end, so P1 is not covered by them.

`apply_bidi_algorithm` orchestrates six phases in order, each corresponding to a section of UAX #9:
`apply_bidi_algorithm` orchestrates these steps in order, each corresponding to a section of UAX #9:

| Phase | Function | UAX #9 rules |
|-------|----------|--------------|
| P | `determine_paragraph_embedding_level` | P2–P3 (P1 is applied beforehand) |
| X | `apply_explicit_levels` | X1–X9 |
| X10 | `get_isolating_run_sequences` | X10 (BD13) |
| W | `apply_weak_rules` | W1–W7 |
| N | `apply_neutral_rules` | N0–N2 |
| I | `apply_implicit_rules` | I1–I2 |
| L | `apply_reorder_rules` | L1–L4 |

Phases W and N operate on **isolating run sequences** (BD13), computed by `get_isolating_run_sequences`. Each IRS chains contiguous level runs that are linked by isolate initiators (LRI/RLI/FSI) to their matching PDI, and carries `sos`/`eos` context types.
Phases W and N operate on **isolating run sequences** (BD13), computed by `get_isolating_run_sequences`. Each IRS chains contiguous level runs that are linked by isolate initiators (LRI/RLI/FSI) to their matching PDI, and carries `sos`/`eos` context types. Characters removed by X9 (`character_type` BN) stay in `chars` but are left out of every level run and IRS: the characters on either side of them count as adjacent, and `sos`/`eos` skip them when looking at neighbouring levels. This matches the reference implementation's trace (removed characters have level `x` and belong to no run). As a result, the W and N helpers contain no BN handling; only phase I (which runs over the whole paragraph and skips BN) and the L rules (which use `original_type`) still meet removed characters. `apply_bidi_algorithm` computes the list once (rule X10), right after phase X, and passes it to `apply_weak_rules` and `apply_neutral_rules`. The spec takes each IRS through W, N and I in turn, but the code runs each phase across all sequences. This is equivalent because no rule reads outside its own IRS (except through `sos`/`eos`). Phase I is not given the sequences: I1/I2 depend only on each character's own type and level, so `apply_implicit_rules` runs over the whole paragraph. Comments in `apply_bidi_algorithm` and `apply_implicit_rules` explain both points.

The W and N phases each delegate to private per-rule helpers (`_apply_w1`…`_apply_w7`, `_apply_n0`, `_apply_n1`, `_apply_n2`). The L phase similarly delegates to `_apply_l1`…`_apply_l4`.

Bracket pair detection for N0 is handled by `_find_bracket_pairs`, using `OPEN_BRACKET_TO_CLOSE` / `CLOSE_BRACKET_TO_OPEN` tables. L4 mirroring uses `BIDI_MIRROR_TABLE`. All three tables are imported from `ucd_data.py`, which builds them at import time: `load_bracket_pairs` reads `ucd/BidiBrackets.txt` and `load_mirror_pairs` reads `ucd/BidiMirroring.txt`, both through `read_ucd_file`. A comment above the import in `bidi.py` describes the tables and the rules that use them.

### Verbose mode

Every phase function (and `_apply_i1`/`_apply_i2`) accepts `verbose=True`; the per-rule W, N and L helpers do not, and their phase function prints on their behalf. When enabled, `print_table` (in `verbose_table.py`, together with the helpers below) prints a before/after table for each rule application, with rows for character index, code point, the character itself, the type (or level, or position) before and after the rule, and embedding level. `print_table` decides "(no changes)" by comparing only the Before/After rows, so Phase X prints two tables: "X1-X8 (levels)" (Before is the paragraph level from X1) and "X1-X9 (types)" (overrides, FSI resolution, X9's BN). Without the levels table, an isolate or embedding with no override would show as "(no changes)". Columns are always in logical order, including the L2 and L4 tables, where Before/After give each character's position before and after reordering.
Every phase function (and `_apply_i1`/`_apply_i2`) accepts `verbose=True`; the per-rule W, N and L helpers do not, and their phase function prints on their behalf. When enabled, `print_table` (in `verbose_table.py`, together with the helpers below) prints a before/after table for each rule application, with rows for character index, code point, the character itself, the type (or level, or position) before and after the rule, and embedding level. `print_table` decides "(no changes)" by comparing only the Before/After rows, so Phase X prints two tables: "X1-X8 (levels)" (Before is the paragraph level from X1) and "X1-X9 (types)" (overrides, FSI resolution, X9's BN). Without the levels table, an isolate or embedding with no override would show as "(no changes)". After phase X, `apply_bidi_algorithm` calls `print_isolating_run_sequences` (also in `verbose_table.py`), which prints the X10 sequences one line each (level, character indices as ranges, `sos`/`eos`), then the indices of the characters removed by X9. Columns are always in logical order, including the L2 and L4 tables, where Before/After give each character's position before and after reordering.

Characters in table cells go through `format_char_for_table`: controls and whitespace are left blank, combining marks are shown on a dotted circle, and each R/AL character is followed by an invisible LEFT-TO-RIGHT MARK so that bidi-aware viewers (Markdown previews, GitHub) don't reverse runs of the row. Cells are sized and centered by `display_width`/`center_in_column`, not `len()`, because those marks take no column on screen. The tracing example in README.md is copied from real output (LRMs included); update it whenever the table format changes.

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ for paragraph in split_into_paragraphs("שלום\nHello"):

## Tracing the algorithm

Pass `verbose=True` to see every rule as it runs. Rules that change nothing print a single line; rules that do change something print a table showing each character's index, its code point, the character itself, its type (or level) before and after the rule, and its embedding level. Changed cells are highlighted in the terminal. Phase X prints two tables, one for the levels it assigns and one for the types it changes, so that an embedding or isolate shows up even when no type changes.
Pass `verbose=True` to see every rule as it runs. Rules that change nothing print a single line; rules that do change something print a table showing each character's index, its code point, the character itself, its type (or level) before and after the rule, and its embedding level. Changed cells are highlighted in the terminal. Phase X prints two tables, one for the levels it assigns and one for the types it changes, so that an embedding or isolate shows up even when no type changes. Rule X10 then lists the isolating run sequences, one line each, with their level, characters and `sos`/`eos`, followed by the characters removed by X9, which belong to no sequence.

```python
apply_bidi_algorithm("שלום 123 Hi", verbose=True)
Expand Down Expand Up @@ -101,12 +101,13 @@ Here N1 turns the space between the Hebrew word and the digits into `R`, because

## How the code is organised

Rule P1 comes first: `split_into_paragraphs` cuts the text into paragraphs after each paragraph separator. `apply_bidi_algorithm` then runs six phases in order on one paragraph. Each matches a section of UAX #9:
Rule P1 comes first: `split_into_paragraphs` cuts the text into paragraphs after each paragraph separator. `apply_bidi_algorithm` then runs these steps in order on one paragraph. Each matches a section of UAX #9:

| Phase | Function | UAX #9 rules | What it does |
|-------|----------|--------------|--------------|
| P | `determine_paragraph_embedding_level` | P2–P3 | Picks the paragraph direction |
| X | `apply_explicit_levels` | X1–X9 | Applies explicit embeddings, overrides and isolates (LRE, RLO, LRI, PDF, PDI, …) |
| X10 | `get_isolating_run_sequences` | X10 (BD13) | Splits the paragraph into isolating run sequences |
| W | `apply_weak_rules` | W1–W7 | Resolves weak types such as numbers and separators |
| N | `apply_neutral_rules` | N0–N2 | Resolves neutrals: brackets first (N0), then spaces and punctuation |
| I | `apply_implicit_rules` | I1–I2 | Turns the resolved types into final embedding levels |
Expand All @@ -115,7 +116,8 @@ Rule P1 comes first: `split_into_paragraphs` cuts the text into paragraphs after
Some things to know when reading the code:

- **`CharacterInfo`** is the record passed through every phase. `original_type` is the character's Unicode bidi class and never changes; `character_type` is a working copy that the rules update.
- **Isolating run sequences** (definition BD13) are what phases W and N work on. `get_isolating_run_sequences` builds them by linking runs of the same level across an isolate (LRI/RLI/FSI … PDI), and attaches the `sos`/`eos` types at each end.
- **Isolating run sequences** (definition BD13) are what phases W and N work on. Rule X10 finds them: `get_isolating_run_sequences` builds them by linking runs of the same level across an isolate (LRI/RLI/FSI … PDI), and attaches the `sos`/`eos` types at each end. `apply_bidi_algorithm` computes them once and passes them to both phases; verbose mode lists them. Characters removed by rule X9 (embedding controls such as LRE and PDF, and type BN) are kept in the text, but they belong to no sequence, so the W and N rules never see them, just as if they had been removed. UAX #9 also applies rules I1–I2 per sequence, but they look only at each character's own type and level, so `apply_implicit_rules` applies them to the whole paragraph at once, with the same result.
- UAX #9 takes each sequence through W, N and I before moving to the next one. The code instead runs phase W on every sequence, then phase N, then phase I. Each sequence's rules look only at that sequence's characters (and its `sos`/`eos`), so the order makes no difference.
- **Bracket pairs** for rule N0 are found by `_find_bracket_pairs`, using pairs read from [ucd/BidiBrackets.txt](ucd/BidiBrackets.txt).
- **Mirrored characters** for rule L4 are read from [ucd/BidiMirroring.txt](ucd/BidiMirroring.txt).
- Both files are copied unchanged from the [Unicode Character Database](https://www.unicode.org/Public/17.0.0/ucd/) (currently Unicode 17.0.0). To update them, replace them with newer versions.
Expand Down
Loading
Loading