RFR: 8366224: Introduce DecimalDigits.appendPair for efficient two-digit formatting and refactor DateTimeHelper
Mikhail Yankelevich
myankelevich at openjdk.org
Wed Aug 27 11:47:45 UTC 2025
On Sat, 23 Aug 2025 04:06:13 GMT, Shaojin Wen <swen at openjdk.org> wrote:
> This PR introduces a new efficient API for appending two-digit integers to StringBuilders and refactors DateTimeHelper to leverage this new functionality.
>
> Changes include:
>
> 1. New `appendPair` method for efficient two-digit integer formatting (00-99):
> - Added `AbstractStringBuilder.appendPair(int i)` with core implementation
> - Added `JavaLangAccess.appendPair(StringBuilder, int)` for internal access
> - Added `System.JavaLangAccessImpl.appendPair(StringBuilder, int)` bridge
> - Added `DecimalDigits.appendPair(StringBuilder, int)` public static utility method
> - Enhanced Javadoc documentation for all new methods
>
> 2. Refactored `DateTimeHelper` to use the new `DecimalDigits.appendPair`:
> - Updated `DateTimeHelper.formatTo` methods for `LocalDate` and `LocalTime`
> - Replaced manual formatting logic with the new efficient two-digit appending
> - Improved code clarity and consistency in date/time formatting
>
> These changes improve code clarity and performance when formatting two-digit numbers, particularly in date/time formatting scenarios.
src/java.base/share/classes/java/lang/AbstractStringBuilder.java line 928:
> 926: DecimalDigits.uncheckedPutPairLatin1(value, count, i);
> 927: } else {
> 928: DecimalDigits.uncheckedPutPairUTF16(value, count, i);
I believe this is not covered by current tests, so could you please add the test if it's easy to reach?
src/java.base/share/classes/jdk/internal/util/DecimalDigits.java line 455:
> 453: * If the value is between 0 and 9, it is formatted with a leading zero
> 454: * (e.g., 5 becomes "05"). If the value is outside the range 0-99,
> 455: * the behavior is unspecified.
Just out of interest, why leave the behaviour as unspecified instead of validating and throwing an exception?
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/26911#discussion_r2303663156
PR Review Comment: https://git.openjdk.org/jdk/pull/26911#discussion_r2303685649
More information about the security-dev
mailing list