fix: burrows_wheeler_transform round-trip corruption and unicode panic - #1064
Open
SEPURI-SAI-KRISHNA wants to merge 1 commit into
Open
fix: burrows_wheeler_transform round-trip corruption and unicode panic#1064SEPURI-SAI-KRISHNA wants to merge 1 commit into
SEPURI-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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.