Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8327640: Allow NumberFormat strict parsing #18339

Conversation

justin-curtis-lu
Copy link
Member

@justin-curtis-lu justin-curtis-lu commented Mar 18, 2024

Please review this PR and associated CSR which introduces strict parsing for NumberFormat.

The concrete subclasses that will utilize this leniency value are DecimalFormat and CompactNumberFormat. Strict leniency allows for parsing to be used as an input validation technique for localized formatted numbers. The default leniency mode will remain lenient, and can only be set to strict through an intentional setLenient(boolean) method call. Leniency operates differently based off the values returned by isGroupingUsed(), getGroupingSize(), and isParseIntegerOnly().

Below is an example of the change, the CSR can be viewed for further detail.

DecimalFormat fmt = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
fmt.parse("1,,,0,,,00,.23Zabced45");  // returns 1000.23
fmt.setLenient(false);
fmt.parse("1,,,0,,,00,.23Zabced45"); // Now throws a ParseException
fmt.setGroupingSize(2);
fmt.parse("12,34,567"); // throws ParseException
fmt.setParseIntegerOnly(true)
fmt.parse("12,34.56"); // throws ParseException
fmt.parse("12,34"); // successfully returns the Number 1234

The associated tests are all localized, and the data is converted properly during runtime.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change requires CSR request JDK-8327703 to be approved

Issues

  • JDK-8327640: Allow NumberFormat strict parsing (Enhancement - P4)
  • JDK-8327703: Allow NumberFormat strict parsing (CSR)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/18339/head:pull/18339
$ git checkout pull/18339

Update a local copy of the PR:
$ git checkout pull/18339
$ git pull https://git.openjdk.org/jdk.git pull/18339/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 18339

View PR using the GUI difftool:
$ git pr show -t 18339

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/18339.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 18, 2024

👋 Welcome back jlu! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Mar 18, 2024

@justin-curtis-lu This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8327640: Allow NumberFormat strict parsing

Reviewed-by: naoto

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 14 new commits pushed to the master branch:

  • 397d948: 8329605: hs errfile generic events - move memory protections and nmethod flushes to separate sections
  • f706949: 8329781: Serial: Remove serialFullGC.inline.hpp
  • 0f78d01: 8329658: Serial: Refactor ContiguousSpace::_next_compaction_space
  • b8f675f: 8329771: G1: Refactor G1BlockOffsetTable::verify
  • 3e9c381: 8329488: Move OopStorage code from safepoint cleanup and remove safepoint cleanup code
  • 77a217d: 8330095: RISC-V: Remove obsolete vandn_vi instruction
  • 717a07b: 8322140: javax/swing/JTable/JTableScrollPrintTest.java does not print the rows and columns of the table in Nimbus and Aqua LookAndFeel
  • aebfd53: 8329660: G1: Improve TestGCLogMessages to be more precise
  • 006a516: 8329962: Remove CardTable::invalidate
  • c7fcd62: 8330006: Serial: Extract out ContiguousSpace::block_start_const
  • ... and 4 more: https://git.openjdk.org/jdk/compare/ece7d4349a13f75c654e2ca0f4d1b66d3af5cf10...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added csr Pull request needs approved CSR before integration rfr Pull request is ready for review labels Mar 18, 2024
@openjdk
Copy link

openjdk bot commented Mar 18, 2024

@justin-curtis-lu The following labels will be automatically applied to this pull request:

  • core-libs
  • i18n

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added core-libs core-libs-dev@openjdk.org i18n i18n-dev@openjdk.org labels Mar 18, 2024
@mlbridge
Copy link

mlbridge bot commented Mar 18, 2024

Replace setLenient() with setStrict() to avoid messy inversion of boolean.
Add a leniency section to NumberFormat.
Overhaul of NumberFormat class specification to be much more organized/readable.
gotPositive = text.regionMatches(position,positiveSuffix,0,
positiveSuffix.length());
boolean containsPosSuffix =
text.regionMatches(position, positiveSuffix,0, positiveSuffix.length());
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
text.regionMatches(position, positiveSuffix,0, positiveSuffix.length());
text.regionMatches(position, positiveSuffix, 0, positiveSuffix.length());

gotNegative = text.regionMatches(position,negativeSuffix,0,
negativeSuffix.length());
boolean containsNegSuffix =
text.regionMatches(position, negativeSuffix,0, negativeSuffix.length());
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
text.regionMatches(position, negativeSuffix,0, negativeSuffix.length());
text.regionMatches(position, negativeSuffix, 0, negativeSuffix.length());

boolean containsNegSuffix =
text.regionMatches(position, negativeSuffix,0, negativeSuffix.length());
boolean endsWithNegSuffix =
containsNegSuffix && text.length() == position + negativeSuffix.length();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
containsNegSuffix && text.length() == position + negativeSuffix.length();
containsNegSuffix && text.length() == position + negativeSuffix.length();

@@ -2438,7 +2479,7 @@ int subparseNumber(String text, int position,
boolean isExponent, boolean[] status) {
// process digits or Inf, find decimal position
status[STATUS_INFINITE] = false;
if (!isExponent && text.regionMatches(position,symbols.getInfinity(),0,
if (!isExponent && text.regionMatches(position, symbols.getInfinity(),0,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (!isExponent && text.regionMatches(position, symbols.getInfinity(),0,
if (!isExponent && text.regionMatches(position, symbols.getInfinity(), 0,

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, this and the other suggestions you provided should be fixed.

@openjdk openjdk bot removed the csr Pull request needs approved CSR before integration label Apr 4, 2024
@openjdk openjdk bot changed the title JDK-8327640: Allow NumberFormat strict parsing 8327640: Allow NumberFormat strict parsing Apr 10, 2024
Copy link
Member

@naotoj naotoj left a comment

Choose a reason for hiding this comment

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

Looks good overall. Left some minor comments.
As to the tests, good to see those corner cases, but they should have unit tests for the new methods, i.e, isStrict() and setStrict(). Also I think equality/serialization for those methods should be examined.

Comment on lines 41 to 43
import sun.util.locale.provider.LocaleProviderAdapter;
import sun.util.locale.provider.ResourceBundleBasedAdapter;

Copy link
Member

Choose a reason for hiding this comment

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

Internal packages typically follow public packages.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed. (IntelliJ is adamant on having it that way, and even after I previously reverted it, it seems like it slipped through again. Oddly, it never used to do this, need to look at the settings)

@@ -2575,7 +2648,24 @@ int subparseNumber(String text, int position,
}
}
return position;
}

// Checks to make sure grouping size is not violated. Used when strict.
Copy link
Member

Choose a reason for hiding this comment

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

If it is only supposed to be used in strict, might be helpful to put an assertion here.


// Calculates the index that violated the grouping size
// Calculation is determined whether it was an under or over violation
Copy link
Member

Choose a reason for hiding this comment

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

Not quite sure what under/over violation means here. Need more comments?

Comment on lines 41 to 43
import sun.util.locale.provider.LocaleProviderAdapter;
import sun.util.locale.provider.LocaleServiceProviderPool;

Copy link
Member

Choose a reason for hiding this comment

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

Same as above

* @since 23
*/
public boolean isStrict() {
throw new UnsupportedOperationException();
Copy link
Member

Choose a reason for hiding this comment

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

Some message might be helpful.

* @since 23
*/
public void setStrict(boolean strict) {
throw new UnsupportedOperationException();
Copy link
Member

Choose a reason for hiding this comment

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

Same here

@justin-curtis-lu
Copy link
Member Author

Thank you for the review @naotoj. All of your comments should be reflected in the two previous commits.

As for the test suggestions, I added to existing tests if possible, otherwise I created new test files to address your suggestions. The new DecimalFormat equality and serialization test can definitely have other qualities tested (beyond parseStrict), but perhaps that's best for a separate issue.

Copy link
Member

@naotoj naotoj left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the changes.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Apr 11, 2024
@justin-curtis-lu
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented Apr 16, 2024

Going to push as commit 941bee1.
Since your change was applied there have been 46 commits pushed to the master branch:

  • 2ede143: 8330279: Typo in java.text.Bidi class description
  • 90df3b7: 8329190: (ch) DatagramChannel.receive should throw ClosedChannelException when called on closed channel
  • f11a496: 8180450: secondary_super_cache does not scale well
  • bfff02e: 8330165: C2: make superword consistently use PhaseIdealLoop::register_new_node()
  • e073d5b: 8329665: fatal error: memory leak: allocating without ResourceMark
  • 6e77d91: 8330261: Clean up linking steps
  • 61fa4d4: 8330351: Remove the SHARED_LIBRARY and STATIC_LIBRARY macros
  • 56ff87a: 8330359: G1: Remove unused forward declaration in g1BlockOffsetTable.hpp
  • 8a5b86c: 8196106: Support nested infinite or recursive flat mapped streams
  • 58911cc: 8188784: javax/management/notification/BroadcasterSupportDeadlockTest.java - TEST FAILED: deadlock
  • ... and 36 more: https://git.openjdk.org/jdk/compare/ece7d4349a13f75c654e2ca0f4d1b66d3af5cf10...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Apr 16, 2024
@openjdk openjdk bot closed this Apr 16, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Apr 16, 2024
@openjdk
Copy link

openjdk bot commented Apr 16, 2024

@justin-curtis-lu Pushed as commit 941bee1.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org i18n i18n-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

3 participants