RFR: 8364007: Add no-argument codePointCount method to CharSequence and String [v3]

Chen Liang liach at openjdk.org
Mon Jan 26 19:39:51 UTC 2026


On Sat, 26 Jul 2025 10:10:40 GMT, Tatsunori Uchino <duke at openjdk.org> wrote:

>> Adds `codePointCount()` overloads to `String`, `Character`, `(Abstract)StringBuilder`, and `StringBuffer` to make it possible to conveniently retrieve the length of a string as code points without extra boundary checks.
>> 
>> 
>> if (superTremendouslyLongExpressionYieldingAString().codePointCount() > limit) {
>>     throw new Exception("exceeding length");
>> }
>> 
>> 
>> Is a CSR required to this change?
>
> Tatsunori Uchino has updated the pull request incrementally with four additional commits since the last revision:
> 
>  - Update `@bug` in correct file
>  - Add default implementation on codePointCount in CharSequence
>  - Update `@bug` entries in test class doc comments
>  - Discard changes on code whose form is not `str.codePointCount(0, str.length())`

src/java.base/share/classes/java/lang/AbstractStringBuilder.java line 539:

> 537:      * @return the number of Unicode code points in this String
> 538:      * @since  26
> 539:      */

Suggestion:

    /**
     * @since 27
     */

src/java.base/share/classes/java/lang/CharSequence.java line 262:

> 260:      *
> 261:      * @return the number of Unicode code points in this sequence
> 262:      * @since 26

Suggestion:

     * {@return the number of Unicode code points in this character sequence}
     * Unpaired surrogates count as one code point each.
     *
     * @since 27

src/java.base/share/classes/java/lang/Character.java line 9965:

> 9963:      * @since  26
> 9964:      */
> 9965:     public static int codePointCount(CharSequence seq) {

Let's remove this method, given we have a method on CharSequence already.

src/java.base/share/classes/java/lang/Character.java line 10011:

> 10009:      * @throws NullPointerException if {@code a} is null.
> 10010:      * @since  26
> 10011:      */

Suggestion:

    /**
     * {@return the number of Unicode code points in the {@code char} array}
     * Unpaired surrogates count as one code point each.
     *
     * @param a the {@code char} array
     * @throws NullPointerException if {@code a} is null
     * @since 27
     */

src/java.base/share/classes/java/lang/String.java line 1723:

> 1721:      *
> 1722:      * @return the number of Unicode code points in this String
> 1723:      * @since  26

Suggestion:

     * {@return the number of Unicode code points in this String}
     * Unpaired surrogates count as one code point each.
     *
     * @since 27

src/java.base/share/classes/java/lang/StringBuffer.java line 274:

> 272: 
> 273:     /**
> 274:      * @since     26

Suggestion:

     * @since 27

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/26461#discussion_r2728958484
PR Review Comment: https://git.openjdk.org/jdk/pull/26461#discussion_r2728956859
PR Review Comment: https://git.openjdk.org/jdk/pull/26461#discussion_r2728961159
PR Review Comment: https://git.openjdk.org/jdk/pull/26461#discussion_r2728967673
PR Review Comment: https://git.openjdk.org/jdk/pull/26461#discussion_r2728954676
PR Review Comment: https://git.openjdk.org/jdk/pull/26461#discussion_r2728962198


More information about the core-libs-dev mailing list