java.text.CharacterIterator

Here are the examples of the java api class java.text.CharacterIterator taken from open source projects.

1. JSONWriter#string()

Project: zorka
File: JSONWriter.java
protected void string(Object obj) {
    add('"');
    CharacterIterator it = new StringCharacterIterator(obj.toString());
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if (c == '"')
            add("\\\"");
        else if (c == '\\')
            add("\\\\");
        else if (c == '/')
            add("\\/");
        else if (c == '\b')
            add("\\b");
        else if (c == '\f')
            add("\\f");
        else if (c == '\n')
            add("\\n");
        else if (c == '\r')
            add("\\r");
        else if (c == '\t')
            add("\\t");
        else if (Character.isISOControl(c)) {
            unicode(c);
        } else {
            add(c);
        }
    }
    add('"');
}

2. JsonWriter#string()

Project: urlrewritefilter
File: JsonWriter.java
private void string(Object obj) {
    add('"');
    CharacterIterator it = new StringCharacterIterator(obj.toString());
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if (c == '"')
            add("\\\"");
        else if (c == '\\')
            add("\\\\");
        else if (c == '/')
            add("\\/");
        else if (c == '\b')
            add("\\b");
        else if (c == '\f')
            add("\\f");
        else if (c == '\n')
            add("\\n");
        else if (c == '\r')
            add("\\r");
        else if (c == '\t')
            add("\\t");
        else if (Character.isISOControl(c)) {
            unicode(c);
        } else {
            add(c);
        }
    }
    add('"');
}

3. JsonValidator#literal()

Project: ToolsFinal
File: JsonValidator.java
private boolean literal(String text) {
    CharacterIterator ci = new StringCharacterIterator(text);
    char t = ci.first();
    if (c != t) {
        return false;
    }
    int start = col;
    boolean ret = true;
    for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) {
        if (t != nextCharacter()) {
            ret = false;
            break;
        }
    }
    nextCharacter();
    if (!ret) {
        ;
    }
    return ret;
}

4. RuleBasedBreakIterator#isBoundary()

Project: openjdk
File: RuleBasedBreakIterator.java
/**
     * Returns true if the specified position is a boundary position.  As a side
     * effect, leaves the iterator pointing to the first boundary position at
     * or after "offset".
     * @param offset the offset to check.
     * @return True if "offset" is a boundary position.
     */
@Override
public boolean isBoundary(int offset) {
    CharacterIterator text = getText();
    checkOffset(offset, text);
    if (offset == text.getBeginIndex()) {
        return true;
    } else // to check whether this is a boundary, we can use following() on the
    // position before the specified one and return true if the position we
    // get back is the one the user specified
    {
        return following(offset - 1) == offset;
    }
}

5. DictionaryBasedBreakIterator#previous()

Project: openjdk
File: DictionaryBasedBreakIterator.java
/**
     * Advances the iterator one step backwards.
     * @return The position of the last boundary position before the
     * current iteration position
     */
@Override
public int previous() {
    CharacterIterator text = getText();
    // covered by them, just move one step backward in the cache
    if (cachedBreakPositions != null && positionInCache > 0) {
        --positionInCache;
        text.setIndex(cachedBreakPositions[positionInCache]);
        return cachedBreakPositions[positionInCache];
    } else // otherwise, dump the cache and use the inherited previous() method to move
    // backward.  This may fill up the cache with new break positions, in which
    // case we have to mark our position in the cache
    {
        cachedBreakPositions = null;
        int result = super.previous();
        if (cachedBreakPositions != null) {
            positionInCache = cachedBreakPositions.length - 2;
        }
        return result;
    }
}

6. QueryUtil#hasWildcardCharacters()

Project: modeshape
File: QueryUtil.java
protected static boolean hasWildcardCharacters(String expression) {
    CharacterIterator iter = new StringCharacterIterator(expression);
    boolean skipNext = false;
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipNext) {
            skipNext = false;
            continue;
        }
        if (c == '*' || c == '?' || c == '%' || c == '_')
            return true;
        if (c == '\\')
            skipNext = true;
    }
    return false;
}

7. QuoteEncoder#encode()

Project: modeshape
File: QuoteEncoder.java
@Override
public String encode(String text) {
    final StringBuilder result = new StringBuilder();
    final CharacterIterator iter = new StringCharacterIterator(text);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (ESCAPE_CHARACTERS.get(c)) {
            result.append(ESCAPE_CHARACTER);
            if (c == '\n') {
                result.append('n');
            } else if (c == '\t') {
                result.append('t');
            } else if (c == '\r') {
                result.append('r');
            } else if (c == '\f') {
                result.append('f');
            }
        } else {
            result.append(c);
        }
    }
    return result.toString();
}

8. Jsr283Encoder#decode()

Project: modeshape
File: Jsr283Encoder.java
@Override
public String decode(String jcrNodeName) {
    if (jcrNodeName == null)
        return null;
    StringBuilder sb = new StringBuilder();
    CharacterIterator iter = new StringCharacterIterator(jcrNodeName);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        char mapped = c;
        if (c == '?') {
            mapped = '*';
        } else if (c == '?') {
            mapped = '/';
        } else if (c == '?') {
            mapped = ':';
        } else if (c == '?') {
            mapped = '[';
        } else if (c == '?') {
            mapped = ']';
        } else if (c == '?') {
            mapped = '|';
        }
        sb.append(mapped);
    }
    return sb.toString();
}

9. Jsr283Encoder#encode()

Project: modeshape
File: Jsr283Encoder.java
@Override
public String encode(String publicName) {
    if (publicName == null)
        return null;
    StringBuilder sb = new StringBuilder();
    CharacterIterator iter = new StringCharacterIterator(publicName);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        char mapped = c;
        if (c == '*') {
            mapped = '?';
        } else if (c == '/') {
            mapped = '?';
        } else if (c == ':') {
            mapped = '?';
        } else if (c == '[') {
            mapped = '?';
        } else if (c == ']') {
            mapped = '?';
        } else if (c == '|') {
            mapped = '?';
        }
        sb.append(mapped);
    }
    return sb.toString();
}

10. CompareStringQuery#hasWildcardCharacters()

Project: modeshape
File: CompareStringQuery.java
protected static boolean hasWildcardCharacters(String expression) {
    CharacterIterator iter = new StringCharacterIterator(expression);
    boolean skipNext = false;
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipNext) {
            skipNext = false;
            continue;
        }
        if (c == '*' || c == '?' || c == '%' || c == '_')
            return true;
        if (c == '\\')
            skipNext = true;
    }
    return false;
}

11. N3JenaWriterCommon#checkPrefixPart()

Project: jena
File: N3JenaWriterCommon.java
/* http://www.w3.org/TeamSubmission/turtle/#sec-grammar-grammar
     * [27]    qname           ::=     prefixName? ':' name?
     * [30]    nameStartChar   ::=     [A-Z] | "_" | [a-z] | [#x00C0-#x00D6] | [#x00D8-#x00F6] | [#x00F8-#x02FF] | [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
     * [31]    nameChar        ::=     nameStartChar | '-' | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040]
     * [32]    name            ::=     nameStartChar nameChar*
     * [33]    prefixName      ::=     ( nameStartChar - '_' ) nameChar*
     */
protected static boolean checkPrefixPart(String s) {
    if (s.length() == 0)
        return true;
    CharacterIterator cIter = new StringCharacterIterator(s);
    char ch = cIter.first();
    if (!checkNameStartChar(ch))
        return false;
    if (// Can't start with _ (bnodes labels handled separately) 
    ch == '_')
        return false;
    return checkNameTail(cIter);
}

12. TurtleValidate#checkValidPrefixPart()

Project: jena
File: TurtleValidate.java
/* http://www.w3.org/TeamSubmission/turtle/#sec-grammar-grammar
     * [27]    qname           ::=     prefixName? ':' name?
     * [30]    nameStartChar   ::=     [A-Z] | "_" | [a-z] | [#x00C0-#x00D6] | [#x00D8-#x00F6] | [#x00F8-#x02FF] | [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
     * [31]    nameChar        ::=     nameStartChar | '-' | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040]
     * [32]    name            ::=     nameStartChar nameChar*
     * [33]    prefixName      ::=     ( nameStartChar - '_' ) nameChar*
     */
protected static boolean checkValidPrefixPart(String s) {
    if (s.length() == 0)
        return true;
    CharacterIterator cIter = new StringCharacterIterator(s);
    char ch = cIter.first();
    if (!checkNameStartChar(ch))
        return false;
    if (// Can't start with _ (bnodes labels handled separately) 
    ch == '_')
        return false;
    return checkNameTail(cIter);
}

13. RuleBasedBreakIterator#isBoundary()

Project: jdk7u-jdk
File: RuleBasedBreakIterator.java
/**
     * Returns true if the specfied position is a boundary position.  As a side
     * effect, leaves the iterator pointing to the first boundary position at
     * or after "offset".
     * @param offset the offset to check.
     * @return True if "offset" is a boundary position.
     */
public boolean isBoundary(int offset) {
    CharacterIterator text = getText();
    checkOffset(offset, text);
    if (offset == text.getBeginIndex()) {
        return true;
    } else // to check whether this is a boundary, we can use following() on the
    // position before the specified one and return true if the position we
    // get back is the one the user specified
    {
        return following(offset - 1) == offset;
    }
}

14. DictionaryBasedBreakIterator#previous()

Project: jdk7u-jdk
File: DictionaryBasedBreakIterator.java
/**
     * Advances the iterator one step backwards.
     * @return The position of the last boundary position before the
     * current iteration position
     */
public int previous() {
    CharacterIterator text = getText();
    // covered by them, just move one step backward in the cache
    if (cachedBreakPositions != null && positionInCache > 0) {
        --positionInCache;
        text.setIndex(cachedBreakPositions[positionInCache]);
        return cachedBreakPositions[positionInCache];
    } else // otherwise, dump the cache and use the inherited previous() method to move
    // backward.  This may fill up the cache with new break positions, in which
    // case we have to mark our position in the cache
    {
        cachedBreakPositions = null;
        int result = super.previous();
        if (cachedBreakPositions != null) {
            positionInCache = cachedBreakPositions.length - 2;
        }
        return result;
    }
}

15. Strings#isUpperCased()

Project: intellij-community
File: Strings.java
public static boolean isUpperCased(@NotNull String text, @NotNull TextRange range) {
    if (range.getLength() == 0)
        return false;
    CharacterIterator it = new StringCharacterIterator(text, range.getStartOffset(), range.getEndOffset(), range.getStartOffset());
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if (!Character.isUpperCase(c)) {
            return false;
        }
    }
    return true;
}

16. RestTitle#getUnderline()

Project: intellij-community
File: RestTitle.java
@Nullable
public String getUnderline() {
    final String text = getNode().getText().trim();
    if (text.length() < 2)
        return null;
    final char adorn = text.charAt(text.length() - 2);
    final CharacterIterator it = new StringCharacterIterator(text);
    int start = 0;
    for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) {
        if (ch != adorn) {
            start = it.getIndex() + 1;
            break;
        }
    }
    return text.substring(start, text.length());
}

17. StubBuildingVisitor#parseClassSignature()

Project: intellij-community
File: StubBuildingVisitor.java
private ClassInfo parseClassSignature(String signature) throws ClsFormatException {
    ClassInfo result = new ClassInfo();
    CharacterIterator iterator = new StringCharacterIterator(signature);
    result.typeParameters = SignatureParsing.parseTypeParametersDeclaration(iterator, myMapping);
    result.superName = SignatureParsing.parseTopLevelClassRefSignature(iterator, myMapping);
    while (iterator.current() != CharacterIterator.DONE) {
        String name = SignatureParsing.parseTopLevelClassRefSignature(iterator, myMapping);
        if (name == null)
            throw new ClsFormatException();
        if (result.interfaceNames == null)
            result.interfaceNames = ContainerUtil.newSmartList();
        result.interfaceNames.add(name);
    }
    return result;
}

18. FOPGVTGlyphVector#performDefaultLayout()

Project: fop
File: FOPGVTGlyphVector.java
public void performDefaultLayout() {
    Font f = font.getFont();
    MinOptMax letterSpaceIPD = MinOptMax.ZERO;
    MinOptMax[] letterSpaceAdjustments = new MinOptMax[text.getEndIndex() - text.getBeginIndex()];
    boolean retainControls = false;
    GlyphMapping mapping = GlyphMapping.doGlyphMapping(text, text.getBeginIndex(), text.getEndIndex(), f, letterSpaceIPD, letterSpaceAdjustments, '\0', '\0', false, text.getBidiLevel(), true, true, retainControls);
    CharacterIterator glyphAsCharIter = mapping.mapping != null ? new StringCharacterIterator(mapping.mapping) : text.getIterator();
    this.glyphs = buildGlyphs(f, glyphAsCharIter);
    this.associations = mapping.associations;
    this.gposAdjustments = mapping.gposAdjustments;
    this.positions = buildGlyphPositions(glyphAsCharIter, mapping.gposAdjustments, letterSpaceAdjustments);
    this.glyphVisibilities = new boolean[this.glyphs.length];
    Arrays.fill(glyphVisibilities, true);
    this.glyphTransforms = new AffineTransform[this.glyphs.length];
}

19. RuleBasedBreakIterator#last()

Project: openjdk
File: RuleBasedBreakIterator.java
/**
     * Sets the current iteration position to the end of the text.
     * (i.e., the CharacterIterator's ending offset).
     * @return The text's past-the-end offset.
     */
@Override
public int last() {
    CharacterIterator t = getText();
    // I'm not sure why, but t.last() returns the offset of the last character,
    // rather than the past-the-end offset
    t.setIndex(t.getEndIndex());
    return t.getIndex();
}

20. RuleBasedBreakIterator#first()

Project: openjdk
File: RuleBasedBreakIterator.java
//=======================================================================
// BreakIterator overrides
//=======================================================================
/**
     * Sets the current iteration position to the beginning of the text.
     * (i.e., the CharacterIterator's starting offset).
     * @return The offset of the beginning of the text.
     */
@Override
public int first() {
    CharacterIterator t = getText();
    t.first();
    return t.getIndex();
}

21. RuleBasedBreakIterator#last()

Project: jdk7u-jdk
File: RuleBasedBreakIterator.java
/**
     * Sets the current iteration position to the end of the text.
     * (i.e., the CharacterIterator's ending offset).
     * @return The text's past-the-end offset.
     */
public int last() {
    CharacterIterator t = getText();
    // I'm not sure why, but t.last() returns the offset of the last character,
    // rather than the past-the-end offset
    t.setIndex(t.getEndIndex());
    return t.getIndex();
}

22. RuleBasedBreakIterator#first()

Project: jdk7u-jdk
File: RuleBasedBreakIterator.java
//=======================================================================
// BreakIterator overrides
//=======================================================================
/**
     * Sets the current iteration position to the beginning of the text.
     * (i.e., the CharacterIterator's starting offset).
     * @return The offset of the beginning of the text.
     */
public int first() {
    CharacterIterator t = getText();
    t.first();
    return t.getIndex();
}

23. StubBuildingVisitor#parseMethodSignature()

Project: intellij-community
File: StubBuildingVisitor.java
private MethodInfo parseMethodSignature(String signature, String[] exceptions) throws ClsFormatException {
    MethodInfo result = new MethodInfo();
    CharacterIterator iterator = new StringCharacterIterator(signature);
    result.typeParameters = SignatureParsing.parseTypeParametersDeclaration(iterator, myMapping);
    if (iterator.current() != '(')
        throw new ClsFormatException();
    iterator.next();
    if (iterator.current() == ')') {
        result.argTypes = ContainerUtil.emptyList();
    } else {
        result.argTypes = ContainerUtil.newSmartList();
        while (iterator.current() != ')' && iterator.current() != CharacterIterator.DONE) {
            result.argTypes.add(SignatureParsing.parseTypeString(iterator, myMapping));
        }
        if (iterator.current() != ')')
            throw new ClsFormatException();
    }
    iterator.next();
    result.returnType = SignatureParsing.parseTypeString(iterator, myMapping);
    result.throwTypes = null;
    while (iterator.current() == '^') {
        iterator.next();
        if (result.throwTypes == null)
            result.throwTypes = ContainerUtil.newSmartList();
        result.throwTypes.add(SignatureParsing.parseTypeString(iterator, myMapping));
    }
    if (exceptions != null && (result.throwTypes == null || exceptions.length > result.throwTypes.size())) {
        // a signature may be inconsistent with exception list - in this case, the more complete list takes precedence
        result.throwTypes = ContainerUtil.map(exceptions, new Function<String, String>() {

            @Override
            public String fun(String name) {
                return myMapping.fun(name);
            }
        });
    }
    return result;
}

24. RuleBasedBreakIterator#handlePrevious()

Project: openjdk
File: RuleBasedBreakIterator.java
/**
     * This method backs the iterator back up to a "safe position" in the text.
     * This is a position that we know, without any context, must be a break position.
     * The various calling methods then iterate forward from this safe position to
     * the appropriate position to return.  (For more information, see the description
     * of buildBackwardsStateTable() in RuleBasedBreakIterator.Builder.)
     */
protected int handlePrevious() {
    CharacterIterator text = getText();
    int state = START_STATE;
    int category = 0;
    int lastCategory = 0;
    int c = getCurrent();
    // loop until we reach the beginning of the text or transition to state 0
    while (c != CharacterIterator.DONE && state != STOP_STATE) {
        // save the last character's category and look up the current
        // character's category
        lastCategory = category;
        category = lookupCategory(c);
        // state transition in the backwards state table
        if (category != IGNORE) {
            state = lookupBackwardState(state, category);
        }
        // then advance one character backwards
        c = getPrevious();
    }
    // the break position.)
    if (c != CharacterIterator.DONE) {
        if (lastCategory != IGNORE) {
            getNext();
            getNext();
        } else {
            getNext();
        }
    }
    return text.getIndex();
}

25. RuleBasedBreakIterator#handleNext()

Project: openjdk
File: RuleBasedBreakIterator.java
//=======================================================================
// implementation
//=======================================================================
/**
     * This method is the actual implementation of the next() method.  All iteration
     * vectors through here.  This method initializes the state machine to state 1
     * and advances through the text character by character until we reach the end
     * of the text or the state machine transitions to state 0.  We update our return
     * value every time the state machine passes through a possible end state.
     */
protected int handleNext() {
    // if we're already at the end of the text, return DONE.
    CharacterIterator text = getText();
    if (text.getIndex() == text.getEndIndex()) {
        return BreakIterator.DONE;
    }
    // no matter what, we always advance at least one character forward
    int result = getNextIndex();
    int lookaheadResult = 0;
    // begin in state 1
    int state = START_STATE;
    int category;
    int c = getCurrent();
    // loop until we reach the end of the text or transition to state 0
    while (c != CharacterIterator.DONE && state != STOP_STATE) {
        // look up the current character's character category (which tells us
        // which column in the state table to look at)
        category = lookupCategory(c);
        // transition in the state table
        if (category != IGNORE) {
            state = lookupState(state, category);
        }
        // to the last saved lookup-state position
        if (lookaheadStates[state]) {
            if (endStates[state]) {
                result = lookaheadResult;
            } else {
                lookaheadResult = getNextIndex();
            }
        } else // otherwise, if the state we've just transitioned to is an accepting
        // state, update the break position to be the current iteration position
        {
            if (endStates[state]) {
                result = getNextIndex();
            }
        }
        c = getNext();
    }
    // position, that always matches the lookahead criteria)
    if (c == CharacterIterator.DONE && lookaheadResult == text.getEndIndex()) {
        result = lookaheadResult;
    }
    text.setIndex(result);
    return result;
}

26. RuleBasedBreakIterator#preceding()

Project: openjdk
File: RuleBasedBreakIterator.java
/**
     * Sets the iterator to refer to the last boundary position before the
     * specified position.
     * @offset The position to begin searching for a break from.
     * @return The position of the last boundary before the starting position.
     */
@Override
public int preceding(int offset) {
    // if we start by updating the current iteration position to the
    // position specified by the caller, we can just use previous()
    // to carry out this operation
    CharacterIterator text = getText();
    checkOffset(offset, text);
    text.setIndex(offset);
    return previous();
}

27. RuleBasedBreakIterator#following()

Project: openjdk
File: RuleBasedBreakIterator.java
/**
     * Sets the iterator to refer to the first boundary position following
     * the specified position.
     * @offset The position from which to begin searching for a break position.
     * @return The position of the first break after the current position.
     */
@Override
public int following(int offset) {
    CharacterIterator text = getText();
    checkOffset(offset, text);
    // Set our internal iteration position (temporarily)
    // to the position passed in.  If this is the _beginning_ position,
    // then we can just use next() to get our return value
    text.setIndex(offset);
    if (offset == text.getBeginIndex()) {
        cachedLastKnownBreak = handleNext();
        return cachedLastKnownBreak;
    }
    // otherwise, we have to sync up first.  Use handlePrevious() to back
    // us up to a known break position before the specified position (if
    // we can determine that the specified position is a break position,
    // we don't back up at all).  This may or may not be the last break
    // position at or before our starting position.  Advance forward
    // from here until we've passed the starting position.  The position
    // we stop on will be the first break position after the specified one.
    int result = cachedLastKnownBreak;
    if (result >= offset || result <= BreakIterator.DONE) {
        result = handlePrevious();
    } else {
        //it might be better to check if handlePrevious() give us closer
        //safe value but handlePrevious() is slow too
        //So, this has to be done carefully
        text.setIndex(result);
    }
    while (result != BreakIterator.DONE && result <= offset) {
        result = handleNext();
    }
    cachedLastKnownBreak = result;
    return result;
}

28. RuleBasedBreakIterator#previous()

Project: openjdk
File: RuleBasedBreakIterator.java
/**
     * Advances the iterator backwards, to the last boundary preceding this one.
     * @return The position of the last boundary position preceding this one.
     */
@Override
public int previous() {
    // if we're already sitting at the beginning of the text, return DONE
    CharacterIterator text = getText();
    if (current() == text.getBeginIndex()) {
        return BreakIterator.DONE;
    }
    // set things up.  handlePrevious() will back us up to some valid
    // break position before the current position (we back our internal
    // iterator up one step to prevent handlePrevious() from returning
    // the current position), but not necessarily the last one before
    // where we started
    int start = current();
    int lastResult = cachedLastKnownBreak;
    if (lastResult >= start || lastResult <= BreakIterator.DONE) {
        getPrevious();
        lastResult = handlePrevious();
    } else {
        //it might be better to check if handlePrevious() give us closer
        //safe value but handlePrevious() is slow too
        //So, this has to be done carefully
        text.setIndex(lastResult);
    }
    int result = lastResult;
    // point is our return value
    while (result != BreakIterator.DONE && result < start) {
        lastResult = result;
        result = handleNext();
    }
    // set the current iteration position to be the last break position
    // before where we started, and then return that value
    text.setIndex(lastResult);
    cachedLastKnownBreak = lastResult;
    return lastResult;
}

29. RuleBasedBreakIterator#handlePrevious()

Project: jdk7u-jdk
File: RuleBasedBreakIterator.java
/**
     * This method backs the iterator back up to a "safe position" in the text.
     * This is a position that we know, without any context, must be a break position.
     * The various calling methods then iterate forward from this safe position to
     * the appropriate position to return.  (For more information, see the description
     * of buildBackwardsStateTable() in RuleBasedBreakIterator.Builder.)
     */
protected int handlePrevious() {
    CharacterIterator text = getText();
    int state = START_STATE;
    int category = 0;
    int lastCategory = 0;
    int c = getCurrent();
    // loop until we reach the beginning of the text or transition to state 0
    while (c != CharacterIterator.DONE && state != STOP_STATE) {
        // save the last character's category and look up the current
        // character's category
        lastCategory = category;
        category = lookupCategory(c);
        // state transition in the backwards state table
        if (category != IGNORE) {
            state = lookupBackwardState(state, category);
        }
        // then advance one character backwards
        c = getPrevious();
    }
    // the break position.)
    if (c != CharacterIterator.DONE) {
        if (lastCategory != IGNORE) {
            getNext();
            getNext();
        } else {
            getNext();
        }
    }
    return text.getIndex();
}

30. RuleBasedBreakIterator#handleNext()

Project: jdk7u-jdk
File: RuleBasedBreakIterator.java
//=======================================================================
// implementation
//=======================================================================
/**
     * This method is the actual implementation of the next() method.  All iteration
     * vectors through here.  This method initializes the state machine to state 1
     * and advances through the text character by character until we reach the end
     * of the text or the state machine transitions to state 0.  We update our return
     * value every time the state machine passes through a possible end state.
     */
protected int handleNext() {
    // if we're already at the end of the text, return DONE.
    CharacterIterator text = getText();
    if (text.getIndex() == text.getEndIndex()) {
        return BreakIterator.DONE;
    }
    // no matter what, we always advance at least one character forward
    int result = getNextIndex();
    int lookaheadResult = 0;
    // begin in state 1
    int state = START_STATE;
    int category;
    int c = getCurrent();
    // loop until we reach the end of the text or transition to state 0
    while (c != CharacterIterator.DONE && state != STOP_STATE) {
        // look up the current character's character category (which tells us
        // which column in the state table to look at)
        category = lookupCategory(c);
        // transition in the state table
        if (category != IGNORE) {
            state = lookupState(state, category);
        }
        // to the last saved lookup-state position
        if (lookaheadStates[state]) {
            if (endStates[state]) {
                result = lookaheadResult;
            } else {
                lookaheadResult = getNextIndex();
            }
        } else // otherwise, if the state we've just transitioned to is an accepting
        // state, update the break position to be the current iteration position
        {
            if (endStates[state]) {
                result = getNextIndex();
            }
        }
        c = getNext();
    }
    // position, that always matches the lookahead criteria)
    if (c == CharacterIterator.DONE && lookaheadResult == text.getEndIndex()) {
        result = lookaheadResult;
    }
    text.setIndex(result);
    return result;
}

31. RuleBasedBreakIterator#preceding()

Project: jdk7u-jdk
File: RuleBasedBreakIterator.java
/**
     * Sets the iterator to refer to the last boundary position before the
     * specified position.
     * @offset The position to begin searching for a break from.
     * @return The position of the last boundary before the starting position.
     */
public int preceding(int offset) {
    // if we start by updating the current iteration position to the
    // position specified by the caller, we can just use previous()
    // to carry out this operation
    CharacterIterator text = getText();
    checkOffset(offset, text);
    text.setIndex(offset);
    return previous();
}

32. RuleBasedBreakIterator#following()

Project: jdk7u-jdk
File: RuleBasedBreakIterator.java
/**
     * Sets the iterator to refer to the first boundary position following
     * the specified position.
     * @offset The position from which to begin searching for a break position.
     * @return The position of the first break after the current position.
     */
public int following(int offset) {
    CharacterIterator text = getText();
    checkOffset(offset, text);
    // Set our internal iteration position (temporarily)
    // to the position passed in.  If this is the _beginning_ position,
    // then we can just use next() to get our return value
    text.setIndex(offset);
    if (offset == text.getBeginIndex()) {
        cachedLastKnownBreak = handleNext();
        return cachedLastKnownBreak;
    }
    // otherwise, we have to sync up first.  Use handlePrevious() to back
    // us up to a known break position before the specified position (if
    // we can determine that the specified position is a break position,
    // we don't back up at all).  This may or may not be the last break
    // position at or before our starting position.  Advance forward
    // from here until we've passed the starting position.  The position
    // we stop on will be the first break position after the specified one.
    int result = cachedLastKnownBreak;
    if (result >= offset || result <= BreakIterator.DONE) {
        result = handlePrevious();
    } else {
        //it might be better to check if handlePrevious() give us closer
        //safe value but handlePrevious() is slow too
        //So, this has to be done carefully
        text.setIndex(result);
    }
    while (result != BreakIterator.DONE && result <= offset) {
        result = handleNext();
    }
    cachedLastKnownBreak = result;
    return result;
}

33. RuleBasedBreakIterator#previous()

Project: jdk7u-jdk
File: RuleBasedBreakIterator.java
/**
     * Advances the iterator backwards, to the last boundary preceding this one.
     * @return The position of the last boundary position preceding this one.
     */
public int previous() {
    // if we're already sitting at the beginning of the text, return DONE
    CharacterIterator text = getText();
    if (current() == text.getBeginIndex()) {
        return BreakIterator.DONE;
    }
    // set things up.  handlePrevious() will back us up to some valid
    // break position before the current position (we back our internal
    // iterator up one step to prevent handlePrevious() from returning
    // the current position), but not necessarily the last one before
    // where we started
    int start = current();
    int lastResult = cachedLastKnownBreak;
    if (lastResult >= start || lastResult <= BreakIterator.DONE) {
        getPrevious();
        lastResult = handlePrevious();
    } else {
        //it might be better to check if handlePrevious() give us closer
        //safe value but handlePrevious() is slow too
        //So, this has to be done carefully
        text.setIndex(lastResult);
    }
    int result = lastResult;
    // point is our return value
    while (result != BreakIterator.DONE && result < start) {
        lastResult = result;
        result = handleNext();
    }
    // set the current iteration position to be the last break position
    // before where we started, and then return that value
    text.setIndex(lastResult);
    cachedLastKnownBreak = lastResult;
    return lastResult;
}

34. TestNGAntTask#fromURI()

Project: testng
File: TestNGAntTask.java
private String fromURI(String uri) {
    URL url = null;
    try {
        url = new URL(uri);
    } catch (MalformedURLException murle) {
    }
    if ((null == url) || !("file".equals(url.getProtocol()))) {
        throw new IllegalArgumentException("Can only handle valid file: URIs");
    }
    StringBuffer buf = new StringBuffer(url.getHost());
    if (buf.length() > 0) {
        buf.insert(0, File.separatorChar).insert(0, File.separatorChar);
    }
    String file = url.getFile();
    int queryPos = file.indexOf('?');
    buf.append((queryPos < 0) ? file : file.substring(0, queryPos));
    uri = buf.toString().replace('/', File.separatorChar);
    if ((File.pathSeparatorChar == ';') && uri.startsWith("\\") && (uri.length() > 2) && Character.isLetter(uri.charAt(1)) && (uri.lastIndexOf(':') > -1)) {
        uri = uri.substring(1);
    }
    StringBuffer sb = new StringBuffer();
    CharacterIterator iter = new StringCharacterIterator(uri);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '%') {
            char c1 = iter.next();
            if (c1 != CharacterIterator.DONE) {
                int i1 = Character.digit(c1, 16);
                char c2 = iter.next();
                if (c2 != CharacterIterator.DONE) {
                    int i2 = Character.digit(c2, 16);
                    sb.append((char) ((i1 << 4) + i2));
                }
            }
        } else {
            sb.append(c);
        }
    }
    return sb.toString();
}

35. CustomLogFormat#parse()

Project: sling
File: CustomLogFormat.java
// ---------- Parsing the format pattern -----------------------------------
private Parameter[] parse(String pattern) {
    List<Parameter> parameterList = new ArrayList<Parameter>();
    StringBuilder buf = new StringBuilder();
    CharacterIterator sr = new StringCharacterIterator(pattern);
    for (int c = sr.first(); c != CharacterIterator.DONE; c = sr.next()) {
        if (c == '%') {
            int c1 = sr.next();
            if (c1 != '%') {
                if (buf.length() > 0) {
                    Parameter text = new PlainTextParameter(buf.toString());
                    parameterList.add(text);
                    buf.setLength(0);
                }
                Parameter param = this.parseFormatString(sr, c1);
                if (param != null) {
                    parameterList.add(param);
                }
                continue;
            }
        }
        buf.append((char) c);
    }
    // append any remaining plain text
    if (buf.length() > 0) {
        Parameter text = new PlainTextParameter(buf.toString());
        parameterList.add(text);
        buf.setLength(0);
    }
    return parameterList.toArray(new Parameter[parameterList.size()]);
}

36. DictionaryBasedBreakIterator#handleNext()

Project: openjdk
File: DictionaryBasedBreakIterator.java
/**
     * This is the implementation function for next().
     */
@Override
protected int handleNext() {
    CharacterIterator text = getText();
    // and possibly regenerate the cache
    if (cachedBreakPositions == null || positionInCache == cachedBreakPositions.length - 1) {
        // start by using the inherited handleNext() to find a tentative return
        // value.   dictionaryCharCount tells us how many dictionary characters
        // we passed over on our way to the tentative return value
        int startPos = text.getIndex();
        dictionaryCharCount = 0;
        int result = super.handleNext();
        // for the new range
        if (dictionaryCharCount > 1 && result - startPos > 1) {
            divideUpDictionaryRange(startPos, result);
        } else // otherwise, the value we got back from the inherited fuction
        // is our return value, and we can dump the cache
        {
            cachedBreakPositions = null;
            return result;
        }
    }
    // and return it
    if (cachedBreakPositions != null) {
        ++positionInCache;
        text.setIndex(cachedBreakPositions[positionInCache]);
        return cachedBreakPositions[positionInCache];
    }
    // SHOULD NEVER GET HERE!
    return -9999;
}

37. DictionaryBasedBreakIterator#following()

Project: openjdk
File: DictionaryBasedBreakIterator.java
/**
     * Sets the current iteration position to the first boundary position after
     * the specified position.
     * @param offset The position to begin searching forward from
     * @return The position of the first boundary after "offset"
     */
@Override
public int following(int offset) {
    CharacterIterator text = getText();
    checkOffset(offset, text);
    // class that may refresh the cache.
    if (cachedBreakPositions == null || offset < cachedBreakPositions[0] || offset >= cachedBreakPositions[cachedBreakPositions.length - 1]) {
        cachedBreakPositions = null;
        return super.following(offset);
    } else // on the other hand, if "offset" is within the range covered by the
    // cache, then just search the cache for the first break position
    // after "offset"
    {
        positionInCache = 0;
        while (positionInCache < cachedBreakPositions.length && offset >= cachedBreakPositions[positionInCache]) {
            ++positionInCache;
        }
        text.setIndex(cachedBreakPositions[positionInCache]);
        return text.getIndex();
    }
}

38. DictionaryBasedBreakIterator#preceding()

Project: openjdk
File: DictionaryBasedBreakIterator.java
/**
     * Sets the current iteration position to the last boundary position
     * before the specified position.
     * @param offset The position to begin searching from
     * @return The position of the last boundary before "offset"
     */
@Override
public int preceding(int offset) {
    CharacterIterator text = getText();
    checkOffset(offset, text);
    // refresh the cache)
    if (cachedBreakPositions == null || offset <= cachedBreakPositions[0] || offset > cachedBreakPositions[cachedBreakPositions.length - 1]) {
        cachedBreakPositions = null;
        return super.preceding(offset);
    } else // on the other hand, if "offset" is within the range covered by the cache,
    // then all we have to do is search the cache for the last break position
    // before "offset"
    {
        positionInCache = 0;
        while (positionInCache < cachedBreakPositions.length && offset > cachedBreakPositions[positionInCache]) {
            ++positionInCache;
        }
        --positionInCache;
        text.setIndex(cachedBreakPositions[positionInCache]);
        return text.getIndex();
    }
}

39. BsonDataOutput#writeChars()

Project: modeshape
File: BsonDataOutput.java
/**
     * Writes every character in the string <code>s</code>, to the output stream, in order, two bytes per character. If
     * <code>s</code> is <code>null</code>, a <code>NullPointerException</code> is thrown. If <code>s.length</code> is zero, then
     * no characters are written. Otherwise, the character <code>s[0]</code> is written first, then <code>s[1]</code>, and so on;
     * the last character written is <code>s[s.length-1]</code>. For each character, two bytes are actually written, low-order
     * byte first, in exactly the manner of the <code>writeChar</code> method.
     * 
     * @param str the string value to be written.
     */
@Override
public void writeChars(String str) {
    CharacterIterator iter = new StringCharacterIterator(str);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        writeChar(c);
    }
}

40. BsonDataOutput#writeBytes()

Project: modeshape
File: BsonDataOutput.java
/**
     * Writes a string to the output stream. For every character in the string <code>s</code>, taken in order, one byte is written
     * to the output stream. If <code>s</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.
     * <p>
     * If <code>s.length</code> is zero, then no bytes are written. Otherwise, the character <code>s[0]</code> is written first,
     * then <code>s[1]</code>, and so on; the last character written is <code>s[s.length-1]</code>. For each character, one byte
     * is written, the low-order byte, in exactly the manner of the <code>writeByte</code> method . The high-order eight bits of
     * each character in the string are ignored.
     * 
     * @param str the string value to be written.
     * @deprecated The semantics of {@code writeBytes(String s)} are considered dangerous. Please use {@link #writeUTF(String s)},
     *             {@link #writeChars(String s)} or another write method instead.
     */
@Deprecated
@Override
public void writeBytes(String str) {
    CharacterIterator iter = new StringCharacterIterator(str);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        writeByte(c);
    }
}

41. CompactJsonWriter#write()

Project: modeshape
File: CompactJsonWriter.java
protected void write(String value, Writer writer) throws IOException {
    // We need to escape certain characters found within the string ...
    writer.append('"');
    CharacterIterator iter = new StringCharacterIterator(value);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch(c) {
            case '"':
            case '\'':
                // Escape the single- or double-quote characters ..
                writer.append('\\').append(c);
                break;
            case '/':
                // writer.append('\\');
                writer.append(c);
                break;
            case '\\':
                writer.append('\\').append(c);
                break;
            case '\b':
                writer.append("\\b");
                break;
            case '\f':
                writer.append("\\f");
                break;
            case '\n':
                writer.append("\\n");
                break;
            case '\r':
                writer.append("\\r");
                break;
            case '\t':
                writer.append("\\t");
                break;
            default:
                if (c < ' ') {
                    String t = "000" + Integer.toHexString(c);
                    writer.append("\\u").append(t.substring(t.length() - 4));
                } else {
                    writer.append(c);
                }
                break;
        }
    }
    writer.append('"');
}

42. XmlCharacters#isValidNcName()

Project: modeshape
File: XmlCharacters.java
/**
     * Determine if the supplied name is a valid XML NCName.
     * 
     * @param name the string being checked
     * @return true if the supplied name is indeed a valid XML NCName, or false otherwise
     */
public static boolean isValidNcName(String name) {
    if (name == null || name.length() == 0)
        return false;
    CharacterIterator iter = new StringCharacterIterator(name);
    char c = iter.first();
    if (!isValidNcNameStart(c))
        return false;
    while (c != CharacterIterator.DONE) {
        if (!isValidNcName(c))
            return false;
        c = iter.next();
    }
    return true;
}

43. XmlCharacters#isValidName()

Project: modeshape
File: XmlCharacters.java
/**
     * Determine if the supplied name is a valid XML Name.
     * 
     * @param name the string being checked
     * @return true if the supplied name is indeed a valid XML Name, or false otherwise
     */
public static boolean isValidName(String name) {
    if (name == null || name.length() == 0)
        return false;
    CharacterIterator iter = new StringCharacterIterator(name);
    char c = iter.first();
    if (!isValidNameStart(c))
        return false;
    while (c != CharacterIterator.DONE) {
        if (!isValidName(c))
            return false;
        c = iter.next();
    }
    return true;
}

44. StringUtil#containsAnyOf()

Project: modeshape
File: StringUtil.java
/**
     * Return whether the supplied string contains any of the supplied characters.
     * 
     * @param str the string to be examined; may not be null
     * @param chars the characters to be found within the supplied string; may be zero-length
     * @return true if the supplied string contains at least one of the supplied characters, or false otherwise
     */
public static boolean containsAnyOf(String str, char... chars) {
    CharacterIterator iter = new StringCharacterIterator(str);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        for (char match : chars) {
            if (c == match)
                return true;
        }
    }
    return false;
}

45. XmlValueEncoder#decode()

Project: modeshape
File: XmlValueEncoder.java
/**
     * {@inheritDoc}
     * 
     * @see org.modeshape.common.text.TextDecoder#decode(java.lang.String)
     */
@Override
public String decode(String encodedText) {
    if (encodedText == null)
        return null;
    StringBuilder sb = new StringBuilder();
    CharacterIterator iter = new StringCharacterIterator(encodedText);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '&') {
            int index = iter.getIndex();
            do {
                // CHECKSTYLE IGNORE check FOR NEXT 1 LINES
                c = iter.next();
            } while (c != CharacterIterator.DONE && c != ';');
            // We found a closing semicolon
            if (c == ';') {
                String s = encodedText.substring(index + 1, iter.getIndex());
                if (SPECIAL_ENTITIES.containsKey(s)) {
                    sb.append(SPECIAL_ENTITIES.get(s));
                    continue;
                }
                if (s.length() > 0 && s.charAt(0) == '#') {
                    try {
                        sb.append((char) Short.parseShort(s.substring(1, s.length())));
                        continue;
                    } catch (NumberFormatException nfe) {
                    }
                }
            }
            // Malformed encoding, restore state and pass poorly encoded data back
            // CHECKSTYLE IGNORE check FOR NEXT 1 LINES
            c = '&';
            iter.setIndex(index);
        }
        sb.append(c);
    }
    return sb.toString();
}

46. XmlValueEncoder#encode()

Project: modeshape
File: XmlValueEncoder.java
/**
     * {@inheritDoc}
     * 
     * @see org.modeshape.common.text.TextEncoder#encode(java.lang.String)
     */
@Override
public String encode(String text) {
    if (text == null)
        return null;
    StringBuilder sb = new StringBuilder();
    CharacterIterator iter = new StringCharacterIterator(text);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch(c) {
            case '&':
                sb.append("&");
                break;
            case '"':
                sb.append(""");
                break;
            case '<':
                sb.append("<");
                break;
            case '>':
                sb.append(">");
                break;
            case '\'':
                sb.append("'");
                break;
            default:
                sb.append(c);
        }
    }
    return sb.toString();
}

47. XmlNameEncoder#encode()

Project: modeshape
File: XmlNameEncoder.java
@Override
public String encode(String text) {
    if (text == null)
        return null;
    if (text.length() == 0)
        return text;
    StringBuilder sb = new StringBuilder();
    String hex = null;
    CharacterIterator iter = new StringCharacterIterator(text);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '_') {
            // Read the next character (if there is one) ...
            char next = iter.next();
            if (next == CharacterIterator.DONE) {
                sb.append(c);
                break;
            }
            // If the next character is not 'x', then these are just regular characters ...
            if (next != 'x') {
                sb.append(c).append(next);
                continue;
            }
            // The next character is 'x', so write out the '_' character in encoded form ...
            sb.append("_x005f_");
            // And then write out the next character ...
            sb.append(next);
        } else if (iter.getIndex() == 0 && XML_NAME_START_ALLOWED_CHARACTERS.get(c)) {
            // The fist only allows a subset from the total list of characters
            sb.append(c);
        } else if (iter.getIndex() > 0 && XML_NAME_ALLOWED_CHARACTERS.get(c)) {
            // Legal characters for an XML Name ...
            sb.append(c);
        } else {
            // All other characters must be escaped with '_xHHHH_' where 'HHHH' is the hex string for the code point
            hex = Integer.toHexString(c);
            // The hex string excludes the leading '0's, so check the character values so we know how many to prepend
            if (c >= '' && c <= '') {
                sb.append("_x000").append(hex);
            } else if (c >= '' && c <= 'ÿ') {
                sb.append("_x00").append(hex);
            } else if (c >= '?' && c <= '?') {
                sb.append("_x0").append(hex);
            } else {
                sb.append("_x").append(hex);
            }
            sb.append('_');
        }
    }
    return sb.toString();
}

48. XmlNameEncoder#decode()

Project: modeshape
File: XmlNameEncoder.java
/**
     * {@inheritDoc}
     * 
     * @see org.modeshape.common.text.TextDecoder#decode(java.lang.String)
     */
@Override
public String decode(String encodedText) {
    if (encodedText == null)
        return null;
    if (encodedText.length() < 7) {
        // Not big enough to have an encoded sequence
        return encodedText;
    }
    StringBuilder sb = new StringBuilder();
    char[] digits = new char[4];
    CharacterIterator iter = new StringCharacterIterator(encodedText);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '_') {
            // Read the next character, if there is one ...
            char next = iter.next();
            if (next == CharacterIterator.DONE) {
                sb.append(c);
                break;
            }
            // If the next character is not 'x', then these are just regular characters ...
            if (next != 'x') {
                sb.append(c).append(next);
                continue;
            }
            // Read the next 4 characters (digits) and another '_' character ...
            digits[0] = iter.next();
            if (digits[0] == CharacterIterator.DONE) {
                sb.append(c).append(next);
                break;
            }
            digits[1] = iter.next();
            if (digits[1] == CharacterIterator.DONE) {
                sb.append(c).append(next).append(digits, 0, 1);
                break;
            }
            digits[2] = iter.next();
            if (digits[2] == CharacterIterator.DONE) {
                sb.append(c).append(next).append(digits, 0, 2);
                break;
            }
            digits[3] = iter.next();
            if (digits[3] == CharacterIterator.DONE) {
                sb.append(c).append(next).append(digits, 0, 3);
                break;
            }
            char underscore = iter.next();
            if (underscore != '_') {
                // includes DONE
                sb.append(c).append(next).append(digits, 0, 4);
                if (underscore == CharacterIterator.DONE)
                    break;
                sb.append(underscore);
                continue;
            }
            // Now parse into the resulting character
            try {
                sb.appendCodePoint(Integer.parseInt(new String(digits), 16));
            } catch (NumberFormatException e) {
                sb.append(c).append(next).append(digits).append(underscore);
            }
        } else {
            // Just append other characters ...
            sb.append(c);
        }
    }
    return sb.toString();
}

49. UrlEncoder#decode()

Project: modeshape
File: UrlEncoder.java
@Override
public String decode(String encodedText) {
    if (encodedText == null)
        return null;
    if (encodedText.length() == 0)
        return encodedText;
    final StringBuilder result = new StringBuilder();
    final CharacterIterator iter = new StringCharacterIterator(encodedText);
    byte[] escapedCharBytes = new byte[4];
    int byteIdx = 0;
    int bytesPerChar = -1;
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == ESCAPE_CHARACTER) {
            boolean foundEscapedCharacter = false;
            // Found the first character in a potential escape sequence, so grab the next two characters ...
            char hexChar1 = iter.next();
            char hexChar2 = hexChar1 != CharacterIterator.DONE ? iter.next() : CharacterIterator.DONE;
            if (hexChar2 != CharacterIterator.DONE) {
                // We found two more characters, but ensure they form a valid hexadecimal number ...
                int hexNum1 = Character.digit(hexChar1, 16);
                int hexNum2 = Character.digit(hexChar2, 16);
                if (hexNum1 > -1 && hexNum2 > -1) {
                    foundEscapedCharacter = true;
                    //character by reading the number of leading 1 bits from the 1st high order byte
                    if (bytesPerChar == -1) {
                        bytesPerChar = BYTES_PER_CHAR[hexNum1];
                    }
                    //record the next byte into the array
                    escapedCharBytes[byteIdx++] = (byte) (hexNum1 * 16 + hexNum2);
                    if (byteIdx == bytesPerChar) {
                        //we've filled the buffer of bytes
                        try {
                            result.append(new String(escapedCharBytes, 0, bytesPerChar, "UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            throw new IllegalStateException(e);
                        }
                        byteIdx = 0;
                        bytesPerChar = -1;
                    }
                }
            }
            if (!foundEscapedCharacter) {
                result.append(c);
                if (hexChar1 != CharacterIterator.DONE)
                    result.append(hexChar1);
                if (hexChar2 != CharacterIterator.DONE)
                    result.append(hexChar2);
            }
        } else {
            result.append(c);
        }
    }
    return result.toString();
}

50. UrlEncoder#encode()

Project: modeshape
File: UrlEncoder.java
protected String encode(String text, BitSet safeChars) {
    final StringBuilder result = new StringBuilder();
    final CharacterIterator iter = new StringCharacterIterator(text);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (safeChars.get(c)) {
            // Safe character, so just pass through ...
            result.append(c);
        } else {
            try {
                // The character is not a safe character, and must be escaped in UTF-8 form (see http://tools.ietf.org/html/rfc3629)
                byte[] utf8Bytes = Character.toString(c).getBytes("UTF-8");
                for (byte utf8Byte : utf8Bytes) {
                    result.append(ESCAPE_CHARACTER);
                    int high = (utf8Byte & 0xf0) >> 4;
                    int low = utf8Byte & 0x0f;
                    result.append(Integer.toHexString(high));
                    result.append(Integer.toHexString(low));
                }
            } catch (UnsupportedEncodingException e) {
                throw new IllegalStateException(e);
            }
        }
    }
    return result.toString();
}

51. QuoteEncoder#decode()

Project: modeshape
File: QuoteEncoder.java
@Override
public String decode(String encodedText) {
    if (encodedText == null)
        return null;
    if (encodedText.length() == 0)
        return "";
    final StringBuilder result = new StringBuilder();
    final CharacterIterator iter = new StringCharacterIterator(encodedText);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == ESCAPE_CHARACTER) {
            char nextChar = iter.next();
            if (nextChar == 'n') {
                result.append('\n');
            } else if (nextChar == 't') {
                result.append('\t');
            } else if (nextChar == 'r') {
                result.append('\r');
            } else if (nextChar == 'f') {
                result.append('\f');
            } else {
                result.append(nextChar);
            }
        } else {
            result.append(c);
        }
    }
    return result.toString();
}

52. Jsr283Encoder#containsEncodeableCharacters()

Project: modeshape
File: Jsr283Encoder.java
public static boolean containsEncodeableCharacters(String str) {
    CharacterIterator iter = new StringCharacterIterator(str);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '*' || c == '/' || c == ':' || c == '[' || c == ']' || c == '|')
            return true;
    }
    return false;
}

53. RelikeQuery#getCompareType()

Project: modeshape
File: RelikeQuery.java
/**
     * Determine the compare type given the expression. This method returns:
     * <ul>
     * <li>CompareType.EQ if expression does not contains '_' or '%';</li>
     * <li>CompareType.ENDS_WITH if expression has only one '%' at start and does not contains '_';</li>
     * <li>CompareType.STARTS_WITH if expression has only one '%' at end and does not contains '_';</li>
     * <li>CompareType.REGEXP otherwise</li>
     * </ul>
     * 
     * @param expression the expression for which the {@link CompareType} is to be found
     * @return the compare type; never null;
     */
private static CompareType getCompareType(String expression) {
    CompareType result = CompareType.EQ;
    CharacterIterator iter = new StringCharacterIterator(expression);
    final int fistIndex = 0;
    final int lastIndex = expression.length() - 1;
    boolean skipNext = false;
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipNext) {
            skipNext = false;
            continue;
        }
        if (c == '_') {
            return CompareType.REGEXP;
        }
        if (c == '%') {
            if (result != CompareType.EQ) {
                // pattern like '%abcdfe%' -> only regexp can handle this;
                return CompareType.REGEXP;
            }
            int index = iter.getIndex();
            if (index == fistIndex) {
                result = CompareType.ENDS_WITH;
            } else if (index == lastIndex) {
                result = CompareType.STARTS_WITH;
            } else {
                return CompareType.REGEXP;
            }
        }
        if (c == '\\')
            skipNext = true;
    }
    return result;
}

54. FixedWidthReader#readLine()

Project: metamodel
File: FixedWidthReader.java
public String[] readLine(String line) throws IOException {
    final List<String> values = new ArrayList<String>();
    if (line == null) {
        return null;
    }
    StringBuilder nextValue = new StringBuilder();
    int valueIndex = 0;
    final CharacterIterator it = new StringCharacterIterator(line);
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        nextValue.append(c);
        final int valueWidth;
        if (constantWidth) {
            valueWidth = _fixedValueWidth;
        } else {
            if (valueIndex >= _valueWidths.length) {
                if (_failOnInconsistentLineWidth) {
                    String[] result = values.toArray(new String[values.size()]);
                    throw new InconsistentValueWidthException(result, line, _rowNumber + 1);
                } else {
                    // silently ignore the inconsistency
                    break;
                }
            }
            valueWidth = _valueWidths[valueIndex];
        }
        if (nextValue.length() == valueWidth) {
            // write the value
            values.add(nextValue.toString().trim());
            nextValue = new StringBuilder();
            valueIndex++;
        }
    }
    if (nextValue.length() > 0) {
        values.add(nextValue.toString().trim());
    }
    String[] result = values.toArray(new String[values.size()]);
    if (!_failOnInconsistentLineWidth && !constantWidth) {
        if (result.length != _valueWidths.length) {
            String[] correctedResult = new String[_valueWidths.length];
            for (int i = 0; i < result.length && i < _valueWidths.length; i++) {
                correctedResult[i] = result[i];
            }
            result = correctedResult;
        }
    }
    if (_failOnInconsistentLineWidth) {
        _rowNumber++;
        if (constantWidth) {
            if (line.length() % _fixedValueWidth != 0) {
                throw new InconsistentValueWidthException(result, line, _rowNumber);
            }
        } else {
            if (result.length != values.size()) {
                throw new InconsistentValueWidthException(result, line, _rowNumber);
            }
            if (line.length() != expectedLineLength) {
                throw new InconsistentValueWidthException(result, line, _rowNumber);
            }
        }
    }
    return result;
}

55. N3JenaWriterCommon#checkNamePart()

Project: jena
File: N3JenaWriterCommon.java
protected static boolean checkNamePart(String s) {
    if (s.length() == 0)
        return true;
    CharacterIterator cIter = new StringCharacterIterator(s);
    char ch = cIter.first();
    if (!checkNameStartChar(ch))
        return false;
    return checkNameTail(cIter);
}

56. TurtleValidate#checkValidNamePart()

Project: jena
File: TurtleValidate.java
protected static boolean checkValidNamePart(String s) {
    if (s.length() == 0)
        return true;
    CharacterIterator cIter = new StringCharacterIterator(s);
    char ch = cIter.first();
    if (!checkNameStartChar(ch))
        return false;
    return checkNameTail(cIter);
}

57. DictionaryBasedBreakIterator#handleNext()

Project: jdk7u-jdk
File: DictionaryBasedBreakIterator.java
/**
     * This is the implementation function for next().
     */
protected int handleNext() {
    CharacterIterator text = getText();
    // and possibly regenerate the cache
    if (cachedBreakPositions == null || positionInCache == cachedBreakPositions.length - 1) {
        // start by using the inherited handleNext() to find a tentative return
        // value.   dictionaryCharCount tells us how many dictionary characters
        // we passed over on our way to the tentative return value
        int startPos = text.getIndex();
        dictionaryCharCount = 0;
        int result = super.handleNext();
        // for the new range
        if (dictionaryCharCount > 1 && result - startPos > 1) {
            divideUpDictionaryRange(startPos, result);
        } else // otherwise, the value we got back from the inherited fuction
        // is our return value, and we can dump the cache
        {
            cachedBreakPositions = null;
            return result;
        }
    }
    // and return it
    if (cachedBreakPositions != null) {
        ++positionInCache;
        text.setIndex(cachedBreakPositions[positionInCache]);
        return cachedBreakPositions[positionInCache];
    }
    // SHOULD NEVER GET HERE!
    return -9999;
}

58. DictionaryBasedBreakIterator#following()

Project: jdk7u-jdk
File: DictionaryBasedBreakIterator.java
/**
     * Sets the current iteration position to the first boundary position after
     * the specified position.
     * @param offset The position to begin searching forward from
     * @return The position of the first boundary after "offset"
     */
public int following(int offset) {
    CharacterIterator text = getText();
    checkOffset(offset, text);
    // class that may refresh the cache.
    if (cachedBreakPositions == null || offset < cachedBreakPositions[0] || offset >= cachedBreakPositions[cachedBreakPositions.length - 1]) {
        cachedBreakPositions = null;
        return super.following(offset);
    } else // on the other hand, if "offset" is within the range covered by the
    // cache, then just search the cache for the first break position
    // after "offset"
    {
        positionInCache = 0;
        while (positionInCache < cachedBreakPositions.length && offset >= cachedBreakPositions[positionInCache]) {
            ++positionInCache;
        }
        text.setIndex(cachedBreakPositions[positionInCache]);
        return text.getIndex();
    }
}

59. DictionaryBasedBreakIterator#preceding()

Project: jdk7u-jdk
File: DictionaryBasedBreakIterator.java
/**
     * Sets the current iteration position to the last boundary position
     * before the specified position.
     * @param offset The position to begin searching from
     * @return The position of the last boundary before "offset"
     */
public int preceding(int offset) {
    CharacterIterator text = getText();
    checkOffset(offset, text);
    // refresh the cache)
    if (cachedBreakPositions == null || offset <= cachedBreakPositions[0] || offset > cachedBreakPositions[cachedBreakPositions.length - 1]) {
        cachedBreakPositions = null;
        return super.preceding(offset);
    } else // on the other hand, if "offset" is within the range covered by the cache,
    // then all we have to do is search the cache for the last break position
    // before "offset"
    {
        positionInCache = 0;
        while (positionInCache < cachedBreakPositions.length && offset > cachedBreakPositions[positionInCache]) {
            ++positionInCache;
        }
        --positionInCache;
        text.setIndex(cachedBreakPositions[positionInCache]);
        return text.getIndex();
    }
}

60. Strings#isCapitalized()

Project: intellij-community
File: Strings.java
public static boolean isCapitalized(@NotNull String text, @NotNull TextRange range) {
    if (range.getLength() == 0)
        return false;
    CharacterIterator it = new StringCharacterIterator(text, range.getStartOffset() + 1, range.getEndOffset(), range.getStartOffset() + 1);
    boolean lowCase = true;
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        lowCase = Character.isLowerCase(c);
    }
    return Character.isUpperCase(text.charAt(range.getStartOffset())) && lowCase;
}

61. RestTitle#getName()

Project: intellij-community
File: RestTitle.java
@Nullable
public String getName() {
    final String text = getNode().getText().trim();
    if (text.length() < 2)
        return null;
    final char adorn = text.charAt(text.length() - 2);
    final CharacterIterator it = new StringCharacterIterator(text);
    int finish = 0;
    for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) {
        if (ch != adorn) {
            finish = it.getIndex();
            break;
        }
    }
    int start = 0;
    if (text.charAt(0) == adorn) {
        for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
            if (ch != adorn) {
                start = it.getIndex() + 1;
                break;
            }
        }
    }
    if (finish <= 0 || start < 0)
        return null;
    return text.substring(start, finish).trim();
}

62. ProtobufJsonFormat#escapeText()

Project: incubator-tajo
File: ProtobufJsonFormat.java
/**
   * Implements JSON string escaping as specified <a href="http://www.ietf.org/rfc/rfc4627.txt">here</a>.
   * <ul>
   *  <li>The following characters are escaped by prefixing them with a '\' : \b,\f,\n,\r,\t,\,"</li>
   *  <li>Other control characters in the range 0x0000-0x001F are escaped using the \\uXXXX notation</li>
   *  <li>UTF-16 surrogate pairs are encoded using the \\uXXXX\\uXXXX notation</li>
   *  <li>any other character is printed as-is</li>
   * </ul>
   */
static String escapeText(String input) {
    StringBuilder builder = new StringBuilder(input.length());
    CharacterIterator iter = new StringCharacterIterator(input);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch(c) {
            case '\b':
                builder.append("\\b");
                break;
            case '\f':
                builder.append("\\f");
                break;
            case '\n':
                builder.append("\\n");
                break;
            case '\r':
                builder.append("\\r");
                break;
            case '\t':
                builder.append("\\t");
                break;
            case '\\':
                builder.append("\\\\");
                break;
            case '"':
                builder.append("\\\"");
                break;
            default:
                // Check for other control characters
                if (c >= 0x0000 && c <= 0x001F) {
                    appendEscapedUnicode(builder, c);
                } else if (Character.isHighSurrogate(c)) {
                    // Encode the surrogate pair using 2 six-character sequence (\\uXXXX\\uXXXX)
                    appendEscapedUnicode(builder, c);
                    c = iter.next();
                    if (c == CharacterIterator.DONE)
                        throw new IllegalArgumentException("invalid unicode string: unexpected high surrogate pair value without corresponding low value.");
                    appendEscapedUnicode(builder, c);
                } else {
                    // Anything else can be printed as-is
                    builder.append(c);
                }
                break;
        }
    }
    return builder.toString();
}

63. Text#utf8Length()

Project: hadoop-common
File: Text.java
/**
   * For the given string, returns the number of UTF-8 bytes
   * required to encode the string.
   * @param string text to encode
   * @return number of UTF-8 bytes required to encode
   */
public static int utf8Length(String string) {
    CharacterIterator iter = new StringCharacterIterator(string);
    char ch = iter.first();
    int size = 0;
    while (ch != CharacterIterator.DONE) {
        if ((ch >= 0xD800) && (ch < 0xDC00)) {
            // surrogate pair?
            char trail = iter.next();
            if ((trail > 0xDBFF) && (trail < 0xE000)) {
                // valid pair
                size += 4;
            } else {
                // invalid pair
                size += 3;
                // rewind one
                iter.previous();
            }
        } else if (ch < 0x80) {
            size++;
        } else if (ch < 0x800) {
            size += 2;
        } else {
            // ch < 0x10000, that is, the largest char value
            size += 3;
        }
        ch = iter.next();
    }
    return size;
}

64. Text#utf8Length()

Project: hadoop-20
File: Text.java
/**
   * For the given string, returns the number of UTF-8 bytes
   * required to encode the string.
   * @param string text to encode
   * @return number of UTF-8 bytes required to encode
   */
public static int utf8Length(String string) {
    CharacterIterator iter = new StringCharacterIterator(string);
    char ch = iter.first();
    int size = 0;
    while (ch != CharacterIterator.DONE) {
        if ((ch >= 0xD800) && (ch < 0xDC00)) {
            // surrogate pair?
            char trail = iter.next();
            if ((trail > 0xDBFF) && (trail < 0xE000)) {
                // valid pair
                size += 4;
            } else {
                // invalid pair
                size += 3;
                // rewind one
                iter.previous();
            }
        } else if (ch < 0x80) {
            size++;
        } else if (ch < 0x800) {
            size += 2;
        } else {
            // ch < 0x10000, that is, the largest char value
            size += 3;
        }
        ch = iter.next();
    }
    return size;
}

65. BasicGlyphVectorTestCase#createGlyphVector()

Project: fop
File: BasicGlyphVectorTestCase.java
@Before
public void createGlyphVector() {
    FontMetrics metrics = mockFontMetrics();
    Font font = mockFont(metrics);
    FOPGVTFont gvtFont = mockGVTFont(font);
    CharacterIterator it = new StringCharacterIterator("ABC");
    glyphVector = new FOPGVTGlyphVector(gvtFont, it, null);
    glyphVector.performDefaultLayout();
}

66. GenerateBidiTestData#parseTestSpec()

Project: fop
File: GenerateBidiTestData.java
private static int[] parseTestSpec(String line, int[] levels) {
    CharacterIterator ci = new StringCharacterIterator(line);
    List cl = new ArrayList();
    while (!atEnd(ci) && !maybeReadNext(ci, ';')) {
        skipSpace(ci);
        int bc;
        if ((bc = maybeReadBidiClass(ci)) >= 0) {
            cl.add(Integer.valueOf(bc));
        } else {
            break;
        }
    }
    skipSpace(ci);
    String s;
    int bs = 0;
    if ((s = maybeReadHexDigits(ci, -1)) != null) {
        bs = Integer.parseInt(s, 16);
    } else {
        badTestSpec("missing bit set", ci);
    }
    skipSpace(ci);
    if (!atEnd(ci)) {
        badTestSpec("extraneous content prior to end of line", ci);
    }
    return createTestArray(cl, bs, levels);
}

67. GenerateBidiTestData#parseReorderSpec()

Project: fop
File: GenerateBidiTestData.java
private static int[] parseReorderSpec(String line, int[] levels) {
    CharacterIterator ci = new StringCharacterIterator(line);
    List rl = new ArrayList();
    skipSpace(ci);
    if (!maybeReadToken(ci, PFX_REORDER)) {
        badReorderSpec("missing prefix \"" + PFX_REORDER + "\"", ci);
    }
    boolean more = true;
    while (more) {
        skipSpace(ci);
        Integer l;
        if ((l = maybeReadInteger(ci)) != null) {
            rl.add(l);
        } else {
            more = false;
        }
    }
    skipSpace(ci);
    if (!atEnd(ci)) {
        badReorderSpec("extraneous content prior to end of line", ci);
    }
    return createReorderArray(rl, levels);
}

68. GenerateBidiTestData#parseLevelSpec()

Project: fop
File: GenerateBidiTestData.java
private static int[] parseLevelSpec(String line) {
    CharacterIterator ci = new StringCharacterIterator(line);
    List ll = new ArrayList();
    skipSpace(ci);
    if (!maybeReadToken(ci, PFX_LEVELS)) {
        badLevelSpec("missing prefix \"" + PFX_LEVELS + "\"", ci);
    }
    boolean more = true;
    while (more) {
        Integer l;
        skipSpace(ci);
        if ((l = maybeReadInteger(ci)) != null) {
            ll.add(l);
        } else if (maybeReadToken(ci, "x")) {
            ll.add(Integer.valueOf(-1));
        } else {
            more = false;
        }
    }
    skipSpace(ci);
    if (!atEnd(ci)) {
        badLevelSpec("extraneous content prior to end of line", ci);
    }
    if (ll.size() == 0) {
        badLevelSpec("must have at least one level value", ci);
    }
    return createLevelsArray(ll);
}

69. GenerateBidiTestData#parseCharacterRanges()

Project: fop
File: GenerateBidiTestData.java
private static List parseCharacterRanges(String charRanges) {
    List ranges = new ArrayList();
    CharacterIterator ci = new StringCharacterIterator(charRanges);
    // read initial list delimiter
    skipSpace(ci);
    if (!readStartOfList(ci)) {
        badRangeSpec("missing initial list delimiter", charRanges);
    }
    // read negation token if present
    boolean negated = false;
    skipSpace(ci);
    if (maybeReadNext(ci, '^')) {
        negated = true;
    }
    // read item
    int[] r;
    skipSpace(ci);
    if ((r = maybeReadItem(ci)) != null) {
        ranges.add(r);
        if (verbose) {
            if ((++numTypeRanges % 10) == 0) {
                System.out.print("#");
            }
        }
    } else {
        badRangeSpec("must contain at least one item", charRanges);
    }
    // read more items if present
    boolean more = true;
    while (more) {
        // read separator if present
        String s;
        skipSpace(ci);
        if ((s = maybeReadSeparator(ci)) != null) {
            if ((s.length() != 0) && !s.equals("||")) {
                badRangeSpec("invalid item separator \"" + s + "\"", charRanges);
            }
        }
        // read item
        skipSpace(ci);
        if ((r = maybeReadItem(ci)) != null) {
            ranges.add(r);
            if (verbose) {
                if ((++numTypeRanges % 10) == 0) {
                    System.out.print("#");
                }
            }
        } else {
            more = false;
        }
    }
    // read terminating list delimiter
    skipSpace(ci);
    if (!readEndOfList(ci)) {
        badRangeSpec("missing terminating list delimiter", charRanges);
    }
    if (!atEnd(ci)) {
        badRangeSpec("extraneous content prior to end of line", ci);
    }
    if (negated) {
        ranges = complementRanges(ranges);
    }
    return removeSurrogates(ranges);
}

70. Text#utf8Length()

Project: drill
File: Text.java
/**
   * For the given string, returns the number of UTF-8 bytes required to encode the string.
   *
   * @param string
   *          text to encode
   * @return number of UTF-8 bytes required to encode
   */
public static int utf8Length(String string) {
    CharacterIterator iter = new StringCharacterIterator(string);
    char ch = iter.first();
    int size = 0;
    while (ch != CharacterIterator.DONE) {
        if ((ch >= 0xD800) && (ch < 0xDC00)) {
            // surrogate pair?
            char trail = iter.next();
            if ((trail > 0xDBFF) && (trail < 0xE000)) {
                // valid pair
                size += 4;
            } else {
                // invalid pair
                size += 3;
                // rewind one
                iter.previous();
            }
        } else if (ch < 0x80) {
            size++;
        } else if (ch < 0x800) {
            size += 2;
        } else {
            // ch < 0x10000, that is, the largest char value
            size += 3;
        }
        ch = iter.next();
    }
    return size;
}

71. ForkedJsonFormat#escapeText()

Project: atlasdb
File: ForkedJsonFormat.java
/**
     * Implements JSON string escaping as specified <a href="http://www.ietf.org/rfc/rfc4627.txt">here</a>.
     * <ul>
     *  <li>The following characters are escaped by prefixing them with a '\' : \b,\f,\n,\r,\t,\,"</li>
     *  <li>Other control characters in the range 0x0000-0x001F are escaped using the \\uXXXX notation</li>
     *  <li>UTF-16 surrogate pairs are encoded using the \\uXXXX\\uXXXX notation</li>
     *  <li>any other character is printed as-is</li>
     * </ul>
     */
static String escapeText(String input) {
    StringBuilder builder = new StringBuilder(input.length());
    CharacterIterator iter = new StringCharacterIterator(input);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch(c) {
            case '\b':
                builder.append("\\b");
                break;
            case '\f':
                builder.append("\\f");
                break;
            case '\n':
                builder.append("\\n");
                break;
            case '\r':
                builder.append("\\r");
                break;
            case '\t':
                builder.append("\\t");
                break;
            case '\\':
                builder.append("\\\\");
                break;
            case '"':
                builder.append("\\\"");
                break;
            default:
                // Check for other control characters
                if (c >= 0x0000 && c <= 0x001F) {
                    appendEscapedUnicode(builder, c);
                } else if (Character.isHighSurrogate(c)) {
                    // Encode the surrogate pair using 2 six-character sequence (\\uXXXX\\uXXXX)
                    appendEscapedUnicode(builder, c);
                    c = iter.next();
                    if (c == CharacterIterator.DONE)
                        throw new IllegalArgumentException("invalid unicode string: unexpected high surrogate pair value without corresponding low value.");
                    appendEscapedUnicode(builder, c);
                } else {
                    // Anything else can be printed as-is
                    builder.append(c);
                }
                break;
        }
    }
    return builder.toString();
}

72. Text#utf8Length()

Project: arrow
File: Text.java
/**
   * For the given string, returns the number of UTF-8 bytes required to encode the string.
   *
   * @param string
   *          text to encode
   * @return number of UTF-8 bytes required to encode
   */
public static int utf8Length(String string) {
    CharacterIterator iter = new StringCharacterIterator(string);
    char ch = iter.first();
    int size = 0;
    while (ch != CharacterIterator.DONE) {
        if ((ch >= 0xD800) && (ch < 0xDC00)) {
            // surrogate pair?
            char trail = iter.next();
            if ((trail > 0xDBFF) && (trail < 0xE000)) {
                // valid pair
                size += 4;
            } else {
                // invalid pair
                size += 3;
                // rewind one
                iter.previous();
            }
        } else if (ch < 0x80) {
            size++;
        } else if (ch < 0x800) {
            size += 2;
        } else {
            // ch < 0x10000, that is, the largest char value
            size += 3;
        }
        ch = iter.next();
    }
    return size;
}