fix(string): split the digit that follows an acronym in Words/SnakeCase/KebabCase/CamelCase/PascalCase - #1010
Open
yu2971512385-ui wants to merge 1 commit into
Conversation
…tter
Words() consumed three bytes when the "([A-Z])([A-Z])([a-z])" branch of
the word-boundary scan matched, so the scan resumed past the lowercase
letter and never checked the boundary it forms with the next byte. A
digit right after an acronym therefore stayed glued to it:
lo.Words("IPv6Address") // [I Pv6 Address]
lo.SnakeCase("IPv6Address") // i_pv6_address
while the same letter/digit boundary splits everywhere else, including
the Int8Value case already covered by the tests, and lodash - which the
case helpers mirror - returns [I Pv 6 Address] / i_pv_6_address.
The trailing lowercase letter only marks where the acronym ends; it
opens the next word, so leave it for the following iteration instead of
consuming it.
This branch has not been deployed
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.
Describe your changes
A digit that directly follows an acronym is not treated as its own word, so it stays glued to the preceding letters:
Every other letter/digit boundary splits, including the
Int8Valuecase the test table already pins ([Int 8 Value],int_8_value), so the same boundary is handled two different ways depending on what precedes it. lodash — which the case helpers mirror, as confirmed in #948 — returns["I", "Pv", "6", "Address"]andi_pv_6_address.Root cause
In
splitWordBoundaries, the branch for the([A-Z])([A-Z])([a-z])alternative appends all three bytes and advancesi += 3:For
IPv6Addressthat consumesI,P,vin one step, so the scan resumes at6and thev/6pair is never tested against the letter→digit rule. The lowercase letter is only the marker for where the acronym ends — it belongs to the next word, so consuming it here also throws away its left edge for the following boundary check.Fix
Emit
canddand advance by 2, leaving the lowercase letter to start the next iteration. One-line change, no new allocations, no change to the scan's linear cost.Behaviour after the change
IPv6Addressi_pv6_addressi_pv_6_addressi_pv_6_addressIPv4Addressi_pv4_addressi_pv_4_addressi_pv_4_addressABc1a_bc1a_bc_1a_bc_1HTMLv5Dochtm_lv5_dochtm_lv_5_dochtm_lv_5_docInt8Valueint_8_valueint_8_value(unchanged)int_8_valueXMLHttpRequestxml_http_requestxml_http_request(unchanged)xml_http_requestIOStream,HTTPCode,PINEAPPLE,LogRouterS3BucketNameI also diffed
Words/CamelCase/PascalCase/KebabCase/SnakeCaseagainst lodash over a 79-string corpus (acronyms, digits, separators, unicode, apostrophes): the threeIPv6Addressdivergences disappear and no new divergence appears — 89 differences before, 86 after, all remaining ones pre-existing and unrelated (unicode folding, apostrophes).Tests
TestWords:IPv6Address,ABc1,ABCdEfTestAllCase:IPv6Address,HTMLv5Docacross all four case helpersWithout the source change 5 subtests fail; with it the full
go test ./...suite passes.Checklist before requesting a review