Skip to content

Editorial: Fix off-by-1 error in PartitionPattern#392

Merged
leobalter merged 1 commit intotc39:masterfrom
longlho:master
Jan 8, 2020
Merged

Editorial: Fix off-by-1 error in PartitionPattern#392
leobalter merged 1 commit intotc39:masterfrom
longlho:master

Conversation

@longlho
Copy link
Collaborator

@longlho longlho commented Nov 27, 2019

Right now after the loop finishes, the remainder is
pattern.substring(nextIndex + 1, len) (bc it's exclusive) but should
be pattern.substring(nextIndex, len) instead since nextIndex is
already set to endIndex + 1 in the loop.

Take AA{0}BB as an example, after the loop finishes:

  • beginIndex = -1
  • endIndex = 4
  • nextIndex = 5

so pattern.substring(5, 7) yields BB (correct) vs the old behavior
would just yield B

Reference impl: https://github.com/formatjs/formatjs/blob/master/packages/intl-utils/src/polyfill-utils.ts#L220
Reference test: https://github.com/formatjs/formatjs/blob/master/packages/intl-utils/tests/polyfill-utils.ts

@Ms2ger
Copy link
Contributor

Ms2ger commented Nov 27, 2019

Nice, thanks for catching that. It looks like PartitionNumberPattern was correct, and PartitionDateTimePattern was wrong, and I copied the wrong one in #366. The bug appears to date back to #100 from 2016.

@longlho
Copy link
Collaborator Author

longlho commented Nov 27, 2019

Gotcha, thanks for the context!

Copy link

@spectranaut spectranaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Right now after the loop finishes, the remainder is
`pattern.substring(nextIndex + 1, len)` (bc it's exclusive) but should
be `pattern.substring(nextIndex, len)` instead since `nextIndex` is
already set to `endIndex + 1` in the loop.

Take `AA{0}BB` as an example, after the loop finishes:
- beginIndex = -1
- endIndex = 4
- nextIndex = 5

so `pattern.substring(5, 7)` yields `BB` (correct) vs the old behavior
would just yield `B`
@sffc sffc added the has consensus Has consensus from TC39-TG2 label Jan 8, 2020
@sffc
Copy link
Contributor

sffc commented Jan 8, 2020

Has consensus according to December ECMA-402.

What further action is required on this Editorial PR?

@leobalter leobalter changed the title Normative: Fix off-by-1 error in PartitionPattern Editorial: Fix off-by-1 error in PartitionPattern Jan 8, 2020
@leobalter
Copy link
Member

I'm renaming the PR to reflect it's fixing a typo mostly.

@leobalter leobalter merged commit f3545f1 into tc39:master Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

has consensus Has consensus from TC39-TG2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants