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

Alan Bateman alanb at openjdk.org
Sat Jan 31 08:51:10 UTC 2026


On Sat, 31 Jan 2026 08:15: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 incrementally with one additional commit since the last revision:
> 
>   Add `codePointCount` for `CharBuffer`

src/java.base/share/classes/java/nio/X-Buffer.java.template line 2062:

> 2060:     /**
> 2061:      * {@return the number of Unicode code points in this character sequence}
> 2062:      * Isolated surrogate code units count as one code point each.

I agree the override needs to be specified but it will need to be specified to count the code points in the between the position (inclusive) and the limit (exclusive).

src/java.base/share/classes/java/nio/X-Buffer.java.template line 2070:

> 2068:         int lim = limit();
> 2069:         int count = l;
> 2070:         for (int i = position(); i < lim;) {

There will need to a robustness pass done on this override as the CharBuffer may be backed by off-heap memory. Look at the existing overides to see examples where it captures the limit and position once.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/26461#discussion_r2749219147
PR Review Comment: https://git.openjdk.org/jdk/pull/26461#discussion_r2749219908


More information about the core-libs-dev mailing list