fix(normalizer): make NormalizedString::prepend work when normalized is empty - #2339
Open
natedemoss wants to merge 1 commit into
Open
natedemoss wants to merge 1 commit into
natedemoss wants to merge 1 commit into
Conversation
…is empty `prepend` only ran a transformation when `normalized` still had a first character to anchor on, so on an empty normalized string (e.g. right after `clear()`) it silently did nothing. `append` had the same bug and was fixed in huggingface#1717, but the `prepend` half of huggingface#1636 was left behind. Mirror that fix: when there is no first character, splice the whole string in as new characters over the empty normalized range. The `Prepend` normalizer already guards on `!normalized.is_empty()`, so this only changes direct `NormalizedString::prepend` calls (including the Python `NormalizedString.prepend` binding). Assisted-by: Claude Opus 5
There was a problem hiding this comment.
Pull request overview
Fixes a NormalizedString::prepend edge case where calling prepend() after clear() was a silent no-op because the implementation required an “anchor” first character in normalized. This brings prepend in line with the existing append behavior on empty normalized, restoring correct behavior for both Rust and downstream bindings (e.g., Python).
Changes:
- Update
NormalizedString::prependto insert characters into an empty normalized range whennormalizedis empty. - Add a regression test
test_prepend_after_clearmirroring the existingtest_append_after_clearcoverage.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Collaborator
|
ty! See #2119 if you want as we are moving towards v1 |
Author
|
Ok |
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.
Problem
NormalizedString::prependis a silent no-op whennormalizedis empty:Same through the Python binding:
This is the unfixed half of #1636. That issue reported it for both
appendandprepend("This is also a problem withprepend"); #1717 fixedappendand addedtest_append_after_clear, but leftprependalone.prependanchors on the first character ofnormalized- it needs it both as the target range fortransform_rangeand as the trailing element it re-emits:On an empty
normalizedthere is no anchor, the body is skipped, and there is noelse.clear()is exactly the operation that emptiesnormalizedwhile leavingoriginalintact.Fix
Mirror the merged
appendfix: with no anchor character to replace, emit every char ofsas an insertion over the empty normalized range.Insertions with
idx < 1get alignment(0, 0), a zero-width span at the start of the original - correct, since none of the text originates fromoriginal.originalandlen_original()are untouched.Blast radius is narrow:
normalizers::prepend::Prepend::normalizealready guards on!normalized.is_empty(), so thePrependnormalizer is unaffected. Only directNormalizedString::prependon an empty normalized string changes, and its prior behavior was to drop the input silently. No public API or serialization change, no new dependency.Tests
test_prepend_after_clear, placed next to the existingtest_append_after_clearand asserting the same properties. Fails onmain(left: "" right: "World "), passes with the fix.Full crate suite green: 256 tests, 0 failures (202 lib unit + 33 integration + 21 doc).
cargo fmt -- --checkandcargo clippy --all-targets --all-features -- -D warningsboth clean.