Fix str_title_case skipping later lowercase-exception words after a leading occurrence#371
Open
patchwright wants to merge 1 commit into
Open
Conversation
The lowercase-exception pass used `break` when an exception word matched
at the start of the string, which abandoned every *later* occurrence of
that word instead of just the leading one. A small word appearing once at
the start and again later was therefore never lowercased:
str_title_case('the cat in the hat') -> 'The Cat in The Hat'
str_title_case('a man and a plan') -> 'A Man and A Plan'
Use `continue` so only the leading occurrence is skipped. Adds a
regression test covering a lowercase-exception word that recurs after the
start (verified to fail on the previous code and pass with the fix).
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
In
str_title_case, the lowercase-exception pass bails out of the wholeloop the first time a match is found at the start of the string:
findallyields every position of the exception word. The intent of theis_startguard is to leave the leading word capitalized — butbreakabandons all later occurrences too, so a small word that appears once at the
start and again later never gets lowercased:
Fix
Use
continueinstead ofbreak, so only the leading occurrence is skippedand later ones are still processed. One-word change.
Tests
Added
test_str_title_case__lower__repeated_after_startcovering alowercase-exception word that recurs after the start (
the cat in the hat,its uppercase variant, and
a man and a plan). Verified it fails on theprevious code and passes with the fix. The full local suite (347 tests)
passes, and
ruff check/ruff format --checkare clean.