151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/*
26f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it
651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as
751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation.  Oracle designates this
851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided
951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code.
1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT
1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that
1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code).
1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version
1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation,
1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any
2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions.
2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.lang;
2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
286f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kongimport java.util.NoSuchElementException;
296f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kongimport java.util.PrimitiveIterator;
306f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kongimport java.util.Spliterator;
316f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kongimport java.util.Spliterators;
326f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kongimport java.util.function.IntConsumer;
336f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kongimport java.util.stream.IntStream;
346f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kongimport java.util.stream.StreamSupport;
3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/**
3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A <tt>CharSequence</tt> is a readable sequence of <code>char</code> values. This
3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface provides uniform, read-only access to many different kinds of
3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>char</code> sequences.
4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A <code>char</code> value represents a character in the <i>Basic
4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Multilingual Plane (BMP)</i> or a surrogate. Refer to <a
4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * href="Character.html#unicode">Unicode Character Representation</a> for details.
4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> This interface does not refine the general contracts of the {@link
4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.Object#equals(java.lang.Object) equals} and {@link
4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.Object#hashCode() hashCode} methods.  The result of comparing two
4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects that implement <tt>CharSequence</tt> is therefore, in general,
4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * undefined.  Each object may be implemented by a different class, and there
4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is no guarantee that each class will be capable of testing its instances
5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for equality with those of the other.  It is therefore inappropriate to use
5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arbitrary <tt>CharSequence</tt> instances as elements in a set or as keys in
5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a map. </p>
5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Mike McCloskey
5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4
5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @spec JSR-51
5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic interface CharSequence {
6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the length of this character sequence.  The length is the number
6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of 16-bit <code>char</code>s in the sequence.</p>
6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the number of <code>char</code>s in this sequence
6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    int length();
6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the <code>char</code> value at the specified index.  An index ranges from zero
7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to <tt>length() - 1</tt>.  The first <code>char</code> value of the sequence is at
7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * index zero, the next at index one, and so on, as for array
7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * indexing. </p>
7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If the <code>char</code> value specified by the index is a
7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <a href="{@docRoot}/java/lang/Character.html#unicode">surrogate</a>, the surrogate
7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value is returned.
7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param   index   the index of the <code>char</code> value to be returned
8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the specified <code>char</code> value
8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws  IndexOutOfBoundsException
8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          if the <tt>index</tt> argument is negative or not less than
8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          <tt>length()</tt>
8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    char charAt(int index);
8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a new <code>CharSequence</code> that is a subsequence of this sequence.
9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The subsequence starts with the <code>char</code> value at the specified index and
9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ends with the <code>char</code> value at index <tt>end - 1</tt>.  The length
9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (in <code>char</code>s) of the
9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returned sequence is <tt>end - start</tt>, so if <tt>start == end</tt>
9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * then an empty sequence is returned. </p>
9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param   start   the start index, inclusive
9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param   end     the end index, exclusive
9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the specified subsequence
10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws  IndexOutOfBoundsException
10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          if <tt>start</tt> or <tt>end</tt> are negative,
10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          if <tt>end</tt> is greater than <tt>length()</tt>,
10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          or if <tt>start</tt> is greater than <tt>end</tt>
10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    CharSequence subSequence(int start, int end);
10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string containing the characters in this sequence in the same
11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * order as this sequence.  The length of the string will be the length of
11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this sequence. </p>
11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  a string consisting of exactly this sequence of characters
11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public String toString();
11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
1186f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong    /**
1196f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * Returns a stream of {@code int} zero-extending the {@code char} values
1206f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * from this sequence.  Any char which maps to a <a
1216f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * href="{@docRoot}/java/lang/Character.html#unicode">surrogate code
1226f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * point</a> is passed through uninterpreted.
1236f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     *
1246f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * <p>If the sequence is mutated while the stream is being read, the
1256f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * result is undefined.
1266f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     *
1276f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * @return an IntStream of char values from this sequence
1286f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * @since 1.8
1296f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     */
1306f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong    public default IntStream chars() {
1316f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong        class CharIterator implements PrimitiveIterator.OfInt {
1326f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            int cur = 0;
1336f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong
1346f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            public boolean hasNext() {
1356f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                return cur < length();
1366f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            }
1376f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong
1386f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            public int nextInt() {
1396f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                if (hasNext()) {
1406f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                    return charAt(cur++);
1416f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                } else {
1426f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                    throw new NoSuchElementException();
1436f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                }
1446f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            }
1456f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong
1466f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            @Override
1476f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            public void forEachRemaining(IntConsumer block) {
1486f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                for (; cur < length(); cur++) {
1496f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                    block.accept(charAt(cur));
1506f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                }
1516f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            }
1526f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong        }
1536f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong
1546f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong        return StreamSupport.intStream(() ->
1556f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                Spliterators.spliterator(
1566f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        new CharIterator(),
1576f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        length(),
1586f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        Spliterator.ORDERED),
1596f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED,
1606f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                false);
1616f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong    }
1626f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong
1636f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong    /**
1646f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * Returns a stream of code point values from this sequence.  Any surrogate
1656f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * pairs encountered in the sequence are combined as if by {@linkplain
1666f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * Character#toCodePoint Character.toCodePoint} and the result is passed
1676f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * to the stream. Any other code units, including ordinary BMP characters,
1686f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * unpaired surrogates, and undefined code units, are zero-extended to
1696f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * {@code int} values which are then passed to the stream.
1706f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     *
1716f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * <p>If the sequence is mutated while the stream is being read, the result
1726f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * is undefined.
1736f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     *
1746f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * @return an IntStream of Unicode code points from this sequence
1756f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     * @since 1.8
1766f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong     */
1776f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong    public default IntStream codePoints() {
1786f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong        class CodePointIterator implements PrimitiveIterator.OfInt {
1796f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            int cur = 0;
1806f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong
1816f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            @Override
1826f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            public void forEachRemaining(IntConsumer block) {
1836f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                final int length = length();
1846f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                int i = cur;
1856f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                try {
1866f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                    while (i < length) {
1876f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        char c1 = charAt(i++);
1886f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        if (!Character.isHighSurrogate(c1) || i >= length) {
1896f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                            block.accept(c1);
1906f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        } else {
1916f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                            char c2 = charAt(i);
1926f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                            if (Character.isLowSurrogate(c2)) {
1936f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                                i++;
1946f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                                block.accept(Character.toCodePoint(c1, c2));
1956f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                            } else {
1966f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                                block.accept(c1);
1976f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                            }
1986f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        }
1996f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                    }
2006f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                } finally {
2016f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                    cur = i;
2026f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                }
2036f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            }
2046f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong
2056f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            public boolean hasNext() {
2066f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                return cur < length();
2076f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            }
2086f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong
2096f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            public int nextInt() {
2106f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                final int length = length();
2116f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong
2126f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                if (cur >= length) {
2136f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                    throw new NoSuchElementException();
2146f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                }
2156f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                char c1 = charAt(cur++);
2166f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                if (Character.isHighSurrogate(c1) && cur < length) {
2176f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                    char c2 = charAt(cur);
2186f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                    if (Character.isLowSurrogate(c2)) {
2196f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        cur++;
2206f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        return Character.toCodePoint(c1, c2);
2216f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                    }
2226f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                }
2236f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                return c1;
2246f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong            }
2256f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong        }
2266f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong
2276f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong        return StreamSupport.intStream(() ->
2286f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                Spliterators.spliteratorUnknownSize(
2296f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        new CodePointIterator(),
2306f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                        Spliterator.ORDERED),
2316f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                Spliterator.ORDERED,
2326f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong                false);
2336f07fc6c80a7e85fdfffbc2ac4e15b53cdd04a3eYi Kong    }
23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski}
235