fix: burrows_wheeler_transform round-trip corruption and unicode panic - #1064
fix: burrows_wheeler_transform round-trip corruption and unicode panic#1064SEPURI-SAI-KRISHNA wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1064 +/- ##
=======================================
Coverage 95.89% 95.89%
=======================================
Files 396 396
Lines 30440 30473 +33
=======================================
+ Hits 29190 29222 +32
- Misses 1250 1251 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
Still relevant and ready for review, all 7 checks are green and Codecov reports every modified line covered. Recapping the impact, since this one is a silent-correctness bug rather than a panic: @imp2002 would you have a moment to take a look? |
Pull Request Template
Description
burrows_wheeler_transformandinv_burrows_wheeler_transformsort by two differentorderings, so the transform is not reversible for any input that mixes upper and lower
case. No error is raised — the inverse simply returns a different string:
Why it happens
Inverting a BWT works only when the forward rotation sort and the inverse's character sort
are the same total order. They were not:
sort_by_key(|a| a.to_lowercase()), i.e.case-insensitively;
sort_by_key(|a| a.1), i.e. by code point.For
"Hello"the two disagree immediately:Dropping
.to_lowercase()makes the forward sort plain lexicographic. Rust comparesStringbytewise and UTF-8 byte order matches code-point order, so the forward sort now agrees with
the inverse's
charsort exactly. The existing expectations are unaffected because everytest string was single-case (
"CARROT","THEALGORITHMS") or punctuation, whereto_lowercase()is a no-op on the ordering.Rotations were also built by slicing at byte offsets
input[i..].to_owned() + &input[..i]indexes a&strby an arbitraryi, which panics assoon as the index lands inside a multi-byte character:
Rotations are now built from a
Vec<char>, so every index is a character position.inv_burrows_wheeler_transformgot the matching treatment: it usedinput.len()(a bytecount) to drive a loop that indexed by character, and called
chars().nth(i)inside thatloop, which is a quadratic scan. Both are gone.
The stability of
sort_by_keyin the inverse is what keeps equal characters in their originalrelative order, which the reconstruction walk depends on; that is now stated in a comment so
it does not get "optimised" into
sort_unstable_by_keylater.Tests
Added
mixed_case(7 strings that all round-tripped to garbage before),unicode(6 stringsthat all panicked before),
single_characterandrepeated_characters. The existing testsare unchanged and still pass. All run in well under 300ms.
Note:
src/compression/burrows_wheeler_transform.rsis a separate implementation and doesnot have this bug — it sorts consistently in both directions. It is untouched here.
Type of change
Checklist:
cargo clippy --all -- -D warningsjust before my last commit and fixed any issue that was found.cargo fmtjust before my last commit.cargo testjust before my last commit and all tests passed.mod.rsfile within its own folder, and in any parent folder(s).DIRECTORY.mdwith the correct link.COUNTRIBUTING.mdand my code follows its guidelines.