Skip to content

engine: keep case-insensitive replace from dropping text when folding shifts byte boundaries - #1364

Merged
aiongg merged 1 commit into
storytold:mainfrom
midasmatterhorn:fix/replace-ci-casefold-boundaries
Oct 22, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
midasmatterhorn:fix/replace-ci-casefold-boundaries

Conversation

@midasmatterhorn

Copy link
Copy Markdown
Contributor

Case-insensitive Find & Replace can silently truncate a cell whose text folds to the same total byte length but with shifted character boundaries: replace_ci sliced the original text at byte offsets found in the lowercased copy, and a fold such as U+212A KELVIN SIGN (3→1 bytes) or U+0130 (2→3 bytes) moves those offsets off character boundaries, so text.get(..) returned None and unwrap_or("") dropped the tail (with an empty replacement, input_to_cell("") deletes the cell outright).

The fix adds folds_in_place, which allows the slice-based fast path only when every character folds to exactly one character of the same UTF-8 length; foldings that can move boundaries fall back to the existing lossless text.replace, and ordinary folds such as É→é keep the fast path. The regression test enters Kİİ (U+212A U+0130 U+0130) in A1, runs edit.replace with what: "k", and asserts the cell text is unchanged — it fails on the old code (Text("x") vs Text("Kİİ")) and passes with the fix; cargo test -p gridcraft-engine cmd::edit::tests stays green (8/8).

Note for reviewers: open PR #980 reworks Find & Replace on character indices and would subsume this fix; if that lands first this branch can be closed. Until then this is the minimal data-loss fix that applies cleanly to current main.

Partially addresses #1307 (finding 5, case-insensitive replace corruption). Targeted tests green locally; cargo xtask ci left to the project runners.

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.

2 participants