RFR: 8364007: Add no-argument codePointCount method to CharSequence and String [v14]
Chen Liang
liach at openjdk.org
Thu Jan 29 22:31:04 UTC 2026
On Thu, 29 Jan 2026 10:47:55 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 with a new target base due to a merge or a rebase. The pull request now contains 20 commits:
>
> - Merge remote-tracking branch 'origin/master' into codepoint-count
> - Fix comments
> - Don't use removed `Character::codePointCount` overload
> - Update year in copyright
> - Fix double empty lines
> - Remove `Character.codePointCount()`
> - Replace "unpaired surrogates" with "isolated surrogate code units"
>
> https://www.unicode.org/versions/Unicode17.0.0/core-spec/chapter-3/#G1654
> https://www.unicode.org/charts/PDF/UDC00.pdf
> - Remove `Character.codePointCount` overload
> - Rename parameter names from `a` to `seq`
>
> `chars` is too confusing with `char`
> - Improve JavaDoc
>
> Co-authored-by: Chen Liang <liach at openjdk.org>
> - ... and 10 more: https://git.openjdk.org/jdk/compare/681e4ec8...198b3188
Please fix this identified thread safety issue.
Submitted your patch to our CI for testing. I built the Javadoc locally, the since-only tag works like this so they are fine:
<img width="1837" height="578" alt="image" src="https://github.com/user-attachments/assets/2b2f8926-0a4d-4540-9fce-f3c91b26979f" />
src/java.base/share/classes/java/lang/AbstractStringBuilder.java line 542:
> 540: return count;
> 541: }
> 542: return StringUTF16.codePointCount(value, 0, count);
Suggestion:
return StringUTF16.codePointCountSB(value, 0, count);
In buggy user program that use StringBuilder from more than one threads, we can have `value.length < count`, so we must perform this call checked.
I think we need a new entry for this method in `test/jdk/java/lang/StringBuilder/StressSBTest.java` too.
-------------
Changes requested by liach (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/26461#pullrequestreview-3725332918
PR Review Comment: https://git.openjdk.org/jdk/pull/26461#discussion_r2743790652
More information about the core-libs-dev
mailing list