jdk/src/share/classes/java/lang/CharSequence.java
changeset 18141 acb3c688a1b8
parent 17210 8a90d05f28d8
child 18776 c17100862d86
equal deleted inserted replaced
17954:885a02686acb 18141:acb3c688a1b8
   177         class CodePointIterator implements PrimitiveIterator.OfInt {
   177         class CodePointIterator implements PrimitiveIterator.OfInt {
   178             int cur = 0;
   178             int cur = 0;
   179 
   179 
   180             @Override
   180             @Override
   181             public void forEachRemaining(IntConsumer block) {
   181             public void forEachRemaining(IntConsumer block) {
   182                 while (cur < length()) {
   182                 final int length = length();
   183                     int cp = Character.codePointAt(CharSequence.this, cur);
   183                 int i = cur;
   184                     cur += Character.charCount(cp);
   184                 try {
   185                     block.accept(cp);
   185                     while (i < length) {
       
   186                         char c1 = charAt(i++);
       
   187                         if (!Character.isHighSurrogate(c1) || i >= length) {
       
   188                             block.accept(c1);
       
   189                         } else {
       
   190                             char c2 = charAt(i);
       
   191                             if (Character.isLowSurrogate(c2)) {
       
   192                                 i++;
       
   193                                 block.accept(Character.toCodePoint(c1, c2));
       
   194                             } else {
       
   195                                 block.accept(c1);
       
   196                             }
       
   197                         }
       
   198                     }
       
   199                 } finally {
       
   200                     cur = i;
   186                 }
   201                 }
   187             }
   202             }
   188 
   203 
   189             public boolean hasNext() {
   204             public boolean hasNext() {
   190                 return cur < length();
   205                 return cur < length();
   191             }
   206             }
   192 
   207 
   193             public int nextInt() {
   208             public int nextInt() {
   194                 if (!hasNext()) {
   209                 final int length = length();
       
   210 
       
   211                 if (cur >= length) {
   195                     throw new NoSuchElementException();
   212                     throw new NoSuchElementException();
   196                 }
   213                 }
   197                 int cp = Character.codePointAt(CharSequence.this, cur);
   214                 char c1 = charAt(cur++);
   198                 cur += Character.charCount(cp);
   215                 if (Character.isHighSurrogate(c1) && cur < length) {
   199                 return cp;
   216                     char c2 = charAt(cur);
       
   217                     if (Character.isLowSurrogate(c2)) {
       
   218                         cur++;
       
   219                         return Character.toCodePoint(c1, c2);
       
   220                     }
       
   221                 }
       
   222                 return c1;
   200             }
   223             }
   201         }
   224         }
   202 
   225 
   203         return StreamSupport.intStream(() ->
   226         return StreamSupport.intStream(() ->
   204                 Spliterators.spliteratorUnknownSize(
   227                 Spliterators.spliteratorUnknownSize(