0

I'm trying to be good, and be compatible with Unicode.

Unfortunately, Java does not make this very easy.

creating and using Strings with surrogate pairs

public static Iterable<Integer> getCodePoints(final String text) {
    return new Iterable<Integer>() {
        @Override public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {
                private int nextIndex = 0;

                @Override public boolean hasNext() {
                    return nextIndex < text.length();
                }

                @Override public Integer next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    int codePoint = text.codePointAt(nextIndex);
                    nextIndex += Character.charCount(codePoint);
                    return codePoint;
                }

                @Override public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}

Does Scala make getting the code points of a String any easier?

Community
  • 1
  • 1
Paul Draper
  • 71,663
  • 43
  • 186
  • 262

0 Answers0