Repository navigation
Conversation
Byte offsets were used where character offsets are expected in the parsers, producing two failures on non-ASCII (e.g. Cyrillic) input. Closes leboiko#29. 1. Panic — `strip_keyword_prefix` (parser/common.rs) sliced `line[..keyword.len()]` at a byte index without a char-boundary check. Multi-byte lines whose byte at `keyword.len()` fell inside a character panicked in the sequence/state parsers (e.g. a Cyrillic `participant`, `loop`, `alt`/`else`). Fixed with `str::get` (returns None instead of panicking on a non-boundary index) + `as_bytes().get` for the trailing-whitespace check (preserving the length guard). 2. Silent label corruption — `try_consume_pipe_label` (parser/flowchart.rs) documented a char count but returned a byte offset; the tokenizer walks a `Vec<char>` and advances the char cursor by it, so a multi-byte edge label over-advanced by `byte_len - char_len`, eating the start of the next node token (`A[Start] -->|да| B[End]` -> label `[End]`, node lost). Fixed by returning `chars().count()`. Same byte-as-char-count fix applied to the compact-arrow form in `try_consume_inline_compact_arrow`. Adds regression tests for all three (the sequence test panics before the fix, the two flowchart tests corrupt the label) and a crate-local CHANGELOG entry. Full crate suite green. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…#30) @vshylov opened PR #30 with the correct fix before this maintainer wrote and merged the superseding #31 without checking for an existing PR. The shipped mermaid-text 0.56.1 fix is substantially their work; correct the CHANGELOG credit from "reported by" to "reported and originally fixed by", naming PR #30. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Hi @vshylov — I owe you an apology. You opened this PR with the correct fix on 2026-07-15, before I started. When I picked up #29 I went straight from the issue to the code and failed to check for an open PR — so I never saw #30, wrote a parallel fix in #31, and merged it, orphaning your work. You then closed this PR. That's entirely on me, and it's not the experience you'd earned after such a careful report and patch. For the record: the fix that shipped in mermaid-text 0.56.1 is substantially your implementation — I've corrected the CHANGELOG to credit you as reported and originally fixed by @vshylov (#29, #30) rather than just the reporter (diff). I'm reopening this briefly to acknowledge it properly, then closing it since the change is already on |
Summary
mermaid-textused byte offsets where character offsets are expected in the parsers, producing two failures on non-ASCII (e.g. Cyrillic — but any multi-byte UTF-8) diagram text. This fixes both. Closes #29.Changes
strip_keyword_prefix(parser/common.rs). It slicedline[..keyword.len()]at a byte index before checking the match and without a char-boundary check, so a multi-byte line whose byte atkeyword.len()fell inside a character panicked in the sequence/state parsers (participant Оркестратор,loop до …,alt …/else …). Fixed withstr::get(..len)(returnsNoneinstead of panicking on a non-boundary index) +as_bytes().get(len)for the trailing-whitespace check (preserves the original length guard and ASCII-fast path).try_consume_pipe_label(parser/flowchart.rs). It documented a char count but returned a byte offset; the tokenizer walks aVec<char>and advances the char cursor by it, so a multi-byte edge label over-advanced bybyte_len − char_lenand ate the start of the next node token —A[Start] -->|да| B[End]produced label[End]with the target node text lost. Fixed by returningchars().count().-. label .->,== label ==>) intry_consume_inline_compact_arrow, which had the identical2 + end + 3byte-as-char-count pattern.The change is minimal and behaviour-preserving for ASCII input; the two flowchart tests differ from the passing ASCII cases only in the edge label.
Test Plan
cargo test --workspace— fullmermaid-textsuite green, 0 failures; no snapshot churn)non_ascii_lines_do_not_panic(sequence),non_ascii_pipe_label_does_not_leak_into_next_node+non_ascii_compact_arrow_label_does_not_leak(flowchart). All three fail before the fix (the sequence test panics atcommon.rs:45; the two flowchart tests assert the leaked bracket / lost node) and pass after.cargo clippy --all-targets -- -D warnings— cleancargo fmt --all -- --check— clean&strhandling with no platform surface.Checklist
crates/mermaid-text/CHANGELOG.md,[Unreleased] → Fixed)clippywarningsCargo.tomlif user-visible — left to you, since you own the release/publish cadence. Happy to bump if you'd prefer it in this PR.Linked Issue
Closes #29
Note on scope
The two issues in #29 are the ones a real corpus hit, but the underlying byte-offset-as-char-offset pattern recurs across
src/parser/(state.rs,class.rs,er.rs,gantt.rs,git_graph.rs,journey.rs,pie.rs,sankey.rs,architecture.rs). I kept this PR focused on the two confirmed user-facing bugs (+ the closely-related compact-arrow variant) so it's easy to review; a broader audit of that pattern with a couple of non-ASCII fixtures per diagram type would likely surface more. Glad to follow up if useful.Context: I evaluated the crate for rendering LLM-generated Mermaid in a Russian-language Rust/ratatui TUI chat client — the crate's shape is a great fit and ASCII sequence diagrams look excellent, but these two bugs blocked non-ASCII use. Thanks for the crate!