On Sun, 8 Mar 2026 02:04:14 GMT, Tatsunori Uchino <[email protected]> wrote:

>> src/java.base/share/classes/java/lang/CharSequence.java line 271:
>> 
>>> 269:         // All we have to do here is to count the number of surrogate 
>>> pairs.
>>> 270:         // The first code unit of a surrogate pair is in [0, 
>>> lastIndex).
>>> 271:         for (int i = 0; i < lastIndex;) {
>> 
>> The loop would be more readable and demonstrabily correct if the i++ was in 
>> the usual place in `for` and not auto incremented in the middle of a 
>> conditional.
>
> I don't think so. Your suggestion doesn't simplify the code:
> 
> 
> boolean precededByHigh = false;
> for (int i = 0; i < lastIndex; i++) {
>     char c = charAt(i);
>     if (precededByHigh) {
>         if (Character.isLowSurrogate(c)) {
>             n--;
>         }
>         precededByHigh = false;
>     } else {
>         precededByHigh = Character.isHighSurrogate(c);
>     }
> }

vs:


for (int i = 0; i < lastIndex) {
    if (Character.isHighSurrogate(charAt(i++))) {
        if (i >= lastIndex) break;
        if (Character.isLowSurrogate(charAt(i))) {
            n--;
            i++;
        }
    }
}


- No `else`.
- No state variables.

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

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

Reply via email to