Path: blob/master/src/java.desktop/share/classes/sun/font/CodePointIterator.java
41155 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright IBM Corp. 2003 - All Rights Reserved27*28* The original version of this source code and documentation is29* copyrighted and owned by IBM. These materials are provided30* under terms of a License Agreement between IBM and Sun.31* This technology is protected by multiple US and International32* patents. This notice and attribution to IBM may not be removed.33*/3435package sun.font;3637import java.text.CharacterIterator;3839public abstract class CodePointIterator {40public static final int DONE = -1;4142public abstract void setToStart();43public abstract void setToLimit();4445public abstract int next();46public abstract int prev();4748public abstract int charIndex();4950public static CodePointIterator create(char[] text) {51return new CharArrayCodePointIterator(text);52}5354public static CodePointIterator create(char[] text, int start, int limit) {55return new CharArrayCodePointIterator(text, start, limit);56}5758public static CodePointIterator create(CharSequence text) {59return new CharSequenceCodePointIterator(text);60}6162public static CodePointIterator create(CharacterIterator iter) {63return new CharacterIteratorCodePointIterator(iter);64}65}6667final class CharArrayCodePointIterator extends CodePointIterator {68private char[] text;69private int start;70private int limit;71private int index;7273public CharArrayCodePointIterator(char[] text) {74this.text = text;75this.limit = text.length;76}7778public CharArrayCodePointIterator(char[] text, int start, int limit) {79if (start < 0 || limit < start || limit > text.length) {80throw new IllegalArgumentException();81}8283this.text = text;84this.start = this.index = start;85this.limit = limit;86}8788public void setToStart() {89index = start;90}9192public void setToLimit() {93index = limit;94}9596public int next() {97if (index < limit) {98char cp1 = text[index++];99if (Character.isHighSurrogate(cp1) && index < limit) {100char cp2 = text[index];101if (Character.isLowSurrogate(cp2)) {102++index;103return Character.toCodePoint(cp1, cp2);104}105}106return cp1;107}108return DONE;109}110111public int prev() {112if (index > start) {113char cp2 = text[--index];114if (Character.isLowSurrogate(cp2) && index > start) {115char cp1 = text[index - 1];116if (Character.isHighSurrogate(cp1)) {117--index;118return Character.toCodePoint(cp1, cp2);119}120}121return cp2;122}123return DONE;124}125126public int charIndex() {127return index;128}129}130131final class CharSequenceCodePointIterator extends CodePointIterator {132private CharSequence text;133private int index;134135public CharSequenceCodePointIterator(CharSequence text) {136this.text = text;137}138139public void setToStart() {140index = 0;141}142143public void setToLimit() {144index = text.length();145}146147public int next() {148if (index < text.length()) {149char cp1 = text.charAt(index++);150if (Character.isHighSurrogate(cp1) && index < text.length()) {151char cp2 = text.charAt(index+1);152if (Character.isLowSurrogate(cp2)) {153++index;154return Character.toCodePoint(cp1, cp2);155}156}157return cp1;158}159return DONE;160}161162public int prev() {163if (index > 0) {164char cp2 = text.charAt(--index);165if (Character.isLowSurrogate(cp2) && index > 0) {166char cp1 = text.charAt(index - 1);167if (Character.isHighSurrogate(cp1)) {168--index;169return Character.toCodePoint(cp1, cp2);170}171}172return cp2;173}174return DONE;175}176177public int charIndex() {178return index;179}180}181182// note this has different iteration semantics than CharacterIterator183final class CharacterIteratorCodePointIterator extends CodePointIterator {184private CharacterIterator iter;185186public CharacterIteratorCodePointIterator(CharacterIterator iter) {187this.iter = iter;188}189190public void setToStart() {191iter.setIndex(iter.getBeginIndex());192}193194public void setToLimit() {195iter.setIndex(iter.getEndIndex());196}197198public int next() {199char cp1 = iter.current();200if (cp1 != CharacterIterator.DONE) {201char cp2 = iter.next();202if (Character.isHighSurrogate(cp1) && cp2 != CharacterIterator.DONE) {203if (Character.isLowSurrogate(cp2)) {204iter.next();205return Character.toCodePoint(cp1, cp2);206}207}208return cp1;209}210return DONE;211}212213public int prev() {214char cp2 = iter.previous();215if (cp2 != CharacterIterator.DONE) {216if (Character.isLowSurrogate(cp2)) {217char cp1 = iter.previous();218if (Character.isHighSurrogate(cp1)) {219return Character.toCodePoint(cp1, cp2);220}221iter.next();222}223return cp2;224}225return DONE;226}227228public int charIndex() {229return iter.getIndex();230}231}232233234