Java Code Examples for java.text.CharacterIterator#getBeginIndex()

The following examples show how to use java.text.CharacterIterator#getBeginIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WhitespaceBasedBreakIterator.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Calculate break positions eagerly parallel to reading text.
 */
public void setText(CharacterIterator ci) {
    int begin = ci.getBeginIndex();
    text = new char[ci.getEndIndex() - begin];
    int[] breaks0 = new int[text.length + 1];
    int brIx = 0;
    breaks0[brIx++] = begin;

    int charIx = 0;
    boolean inWs = false;
    for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) {
        text[charIx] = c;
        boolean ws = Character.isWhitespace(c);
        if (inWs && !ws) {
            breaks0[brIx++] = charIx + begin;
        }
        inWs = ws;
        charIx++;
    }
    if (text.length > 0) {
        breaks0[brIx++] = text.length + begin;
    }
    System.arraycopy(breaks0, 0, breaks = new int[brIx], 0, brIx);
}
 
Example 2
Source File: WhitespaceBasedBreakIterator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Calculate break positions eagerly parallel to reading text.
 */
public void setText(CharacterIterator ci) {
    int begin = ci.getBeginIndex();
    text = new char[ci.getEndIndex() - begin];
    int[] breaks0 = new int[text.length + 1];
    int brIx = 0;
    breaks0[brIx++] = begin;

    int charIx = 0;
    boolean inWs = false;
    for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) {
        text[charIx] = c;
        boolean ws = Character.isWhitespace(c);
        if (inWs && !ws) {
            breaks0[brIx++] = charIx + begin;
        }
        inWs = ws;
        charIx++;
    }
    if (text.length > 0) {
        breaks0[brIx++] = text.length + begin;
    }
    System.arraycopy(breaks0, 0, breaks = new int[brIx], 0, brIx);
}
 
Example 3
Source File: MessageFormat.java    From fitnotifications with Apache License 2.0 6 votes vote down vote up
public static int append(Appendable result, CharacterIterator iterator) {
    try {
        int start = iterator.getBeginIndex();
        int limit = iterator.getEndIndex();
        int length = limit - start;
        if (start < limit) {
            result.append(iterator.first());
            while (++start < limit) {
                result.append(iterator.next());
            }
        }
        return length;
    } catch(IOException e) {
        throw new ICUUncheckedIOException(e);
    }
}
 
Example 4
Source File: RuleBasedBreakIterator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
    }

    // 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
    else {
        return following(offset - 1) == offset;
    }
}
 
Example 5
Source File: WhitespaceBasedBreakIterator.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Calculate break positions eagerly parallel to reading text.
 */
public void setText(CharacterIterator ci) {
    int begin = ci.getBeginIndex();
    text = new char[ci.getEndIndex() - begin];
    int[] breaks0 = new int[text.length + 1];
    int brIx = 0;
    breaks0[brIx++] = begin;

    int charIx = 0;
    boolean inWs = false;
    for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) {
        text[charIx] = c;
        boolean ws = Character.isWhitespace(c);
        if (inWs && !ws) {
            breaks0[brIx++] = charIx + begin;
        }
        inWs = ws;
        charIx++;
    }
    if (text.length > 0) {
        breaks0[brIx++] = text.length + begin;
    }
    System.arraycopy(breaks0, 0, breaks = new int[brIx], 0, brIx);
}
 
Example 6
Source File: LegendItem.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a string containing the characters from the given iterator.
 *
 * @param iterator  the iterator (<code>null</code> not permitted).
 *
 * @return A string.
 */
private String characterIteratorToString(CharacterIterator iterator) {
    int endIndex = iterator.getEndIndex();
    int beginIndex = iterator.getBeginIndex();
    int count = endIndex - beginIndex;
    if (count <= 0) {
        return "";
    }
    char[] chars = new char[count];
    int i = 0;
    char c = iterator.first();
    while (c != CharacterIterator.DONE) {
        chars[i] = c;
        i++;
        c = iterator.next();
    }
    return new String(chars);
}
 
Example 7
Source File: WhitespaceBasedBreakIterator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Calculate break positions eagerly parallel to reading text.
 */
public void setText(CharacterIterator ci) {
    int begin = ci.getBeginIndex();
    text = new char[ci.getEndIndex() - begin];
    int[] breaks0 = new int[text.length + 1];
    int brIx = 0;
    breaks0[brIx++] = begin;

    int charIx = 0;
    boolean inWs = false;
    for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) {
        text[charIx] = c;
        boolean ws = Character.isWhitespace(c);
        if (inWs && !ws) {
            breaks0[brIx++] = charIx + begin;
        }
        inWs = ws;
        charIx++;
    }
    if (text.length > 0) {
        breaks0[brIx++] = text.length + begin;
    }
    System.arraycopy(breaks0, 0, breaks = new int[brIx], 0, brIx);
}
 
Example 8
Source File: LegendItem.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns a string containing the characters from the given iterator.
 *
 * @param iterator  the iterator (<code>null</code> not permitted).
 *
 * @return A string.
 */
private String characterIteratorToString(CharacterIterator iterator) {
    int endIndex = iterator.getEndIndex();
    int beginIndex = iterator.getBeginIndex();
    int count = endIndex - beginIndex;
    if (count <= 0) {
        return "";
    }
    char[] chars = new char[count];
    int i = 0;
    char c = iterator.first();
    while (c != CharacterIterator.DONE) {
        chars[i] = c;
        i++;
        c = iterator.next();
    }
    return new String(chars);
}
 
Example 9
Source File: LegendItem.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a string containing the characters from the given iterator.
 *
 * @param iterator  the iterator (<code>null</code> not permitted).
 *
 * @return A string.
 */
private String characterIteratorToString(CharacterIterator iterator) {
    int endIndex = iterator.getEndIndex();
    int beginIndex = iterator.getBeginIndex();
    int count = endIndex - beginIndex;
    if (count <= 0) {
        return "";
    }
    char[] chars = new char[count];
    int i = 0;
    char c = iterator.first();
    while (c != CharacterIterator.DONE) {
        chars[i] = c;
        i++;
        c = iterator.next();
    }
    return new String(chars);
}
 
Example 10
Source File: WhitespaceBasedBreakIterator.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
/**
 * Calculate break positions eagerly parallel to reading text.
 */
public void setText(CharacterIterator ci) {
    int begin = ci.getBeginIndex();
    text = new char[ci.getEndIndex() - begin];
    int[] breaks0 = new int[text.length + 1];
    int brIx = 0;
    breaks0[brIx++] = begin;

    int charIx = 0;
    boolean inWs = false;
    for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) {
        text[charIx] = c;
        boolean ws = Character.isWhitespace(c);
        if (inWs && !ws) {
            breaks0[brIx++] = charIx + begin;
        }
        inWs = ws;
        charIx++;
    }
    if (text.length > 0) {
        breaks0[brIx++] = text.length + begin;
    }
    System.arraycopy(breaks0, 0, breaks = new int[brIx], 0, brIx);
}
 
Example 11
Source File: RuleBasedBreakIterator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
    }

    // 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
    else {
        return following(offset - 1) == offset;
    }
}
 
Example 12
Source File: RuleBasedBreakIterator.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

    // 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
    else {
        return following(offset - 1) == offset;
    }
}
 
Example 13
Source File: RuleBasedBreakIterator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: RuleBasedBreakIterator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: RuleBasedBreakIterator.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;

    // iterate forward from the known break position until we pass our
    // starting point.  The last break position before the starting
    // 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;
}
 
Example 16
Source File: RuleBasedBreakIterator.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Throw IllegalArgumentException unless begin <= offset < end.
 */
protected static final void checkOffset(int offset, CharacterIterator text) {
    if (offset < text.getBeginIndex() || offset > text.getEndIndex()) {
        throw new IllegalArgumentException("offset out of bounds");
    }
}
 
Example 17
Source File: RuleBasedBreakIterator.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;

    // iterate forward from the known break position until we pass our
    // starting point.  The last break position before the starting
    // 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;
}
 
Example 18
Source File: RuleBasedBreakIterator.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Throw IllegalArgumentException unless begin <= offset < end.
 */
protected static final void checkOffset(int offset, CharacterIterator text) {
    if (offset < text.getBeginIndex() || offset > text.getEndIndex()) {
        throw new IllegalArgumentException("offset out of bounds");
    }
}
 
Example 19
Source File: Font.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the logical bounds of the characters indexed in the
 * specified {@link CharacterIterator} in the
 * specified <code>FontRenderContext</code>.  The logical bounds
 * contains the origin, ascent, advance, and height, which includes
 * the leading.  The logical bounds does not always enclose all the
 * text.  For example, in some languages and in some fonts, accent
 * marks can be positioned above the ascent or below the descent.
 * To obtain a visual bounding box, which encloses all the text,
 * use the {@link TextLayout#getBounds() getBounds} method of
 * <code>TextLayout</code>.
 * <p>Note: The returned bounds is in baseline-relative coordinates
 * (see {@link java.awt.Font class notes}).
 * @param ci the specified <code>CharacterIterator</code>
 * @param beginIndex the initial offset in <code>ci</code>
 * @param limit the end offset in <code>ci</code>
 * @param frc the specified <code>FontRenderContext</code>
 * @return a <code>Rectangle2D</code> that is the bounding box of the
 * characters indexed in the specified <code>CharacterIterator</code>
 * in the specified <code>FontRenderContext</code>.
 * @see FontRenderContext
 * @see Font#createGlyphVector
 * @since 1.2
 * @throws IndexOutOfBoundsException if <code>beginIndex</code> is
 *         less than the start index of <code>ci</code>, or
 *         <code>limit</code> is greater than the end index of
 *         <code>ci</code>, or <code>beginIndex</code> is greater
 *         than <code>limit</code>
 */
public Rectangle2D getStringBounds(CharacterIterator ci,
                                int beginIndex, int limit,
                                   FontRenderContext frc) {
    int start = ci.getBeginIndex();
    int end = ci.getEndIndex();

    if (beginIndex < start) {
        throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
    }
    if (limit > end) {
        throw new IndexOutOfBoundsException("limit: " + limit);
    }
    if (beginIndex > limit) {
        throw new IndexOutOfBoundsException("range length: " +
                                            (limit - beginIndex));
    }

    char[]  arr = new char[limit - beginIndex];

    ci.setIndex(beginIndex);
    for(int idx = 0; idx < arr.length; idx++) {
        arr[idx] = ci.current();
        ci.next();
    }

    return getStringBounds(arr,0,arr.length,frc);
}
 
Example 20
Source File: Font.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the logical bounds of the characters indexed in the
 * specified {@link CharacterIterator} in the
 * specified <code>FontRenderContext</code>.  The logical bounds
 * contains the origin, ascent, advance, and height, which includes
 * the leading.  The logical bounds does not always enclose all the
 * text.  For example, in some languages and in some fonts, accent
 * marks can be positioned above the ascent or below the descent.
 * To obtain a visual bounding box, which encloses all the text,
 * use the {@link TextLayout#getBounds() getBounds} method of
 * <code>TextLayout</code>.
 * <p>Note: The returned bounds is in baseline-relative coordinates
 * (see {@link java.awt.Font class notes}).
 * @param ci the specified <code>CharacterIterator</code>
 * @param beginIndex the initial offset in <code>ci</code>
 * @param limit the end offset in <code>ci</code>
 * @param frc the specified <code>FontRenderContext</code>
 * @return a <code>Rectangle2D</code> that is the bounding box of the
 * characters indexed in the specified <code>CharacterIterator</code>
 * in the specified <code>FontRenderContext</code>.
 * @see FontRenderContext
 * @see Font#createGlyphVector
 * @since 1.2
 * @throws IndexOutOfBoundsException if <code>beginIndex</code> is
 *         less than the start index of <code>ci</code>, or
 *         <code>limit</code> is greater than the end index of
 *         <code>ci</code>, or <code>beginIndex</code> is greater
 *         than <code>limit</code>
 */
public Rectangle2D getStringBounds(CharacterIterator ci,
                                int beginIndex, int limit,
                                   FontRenderContext frc) {
    int start = ci.getBeginIndex();
    int end = ci.getEndIndex();

    if (beginIndex < start) {
        throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
    }
    if (limit > end) {
        throw new IndexOutOfBoundsException("limit: " + limit);
    }
    if (beginIndex > limit) {
        throw new IndexOutOfBoundsException("range length: " +
                                            (limit - beginIndex));
    }

    char[]  arr = new char[limit - beginIndex];

    ci.setIndex(beginIndex);
    for(int idx = 0; idx < arr.length; idx++) {
        arr[idx] = ci.current();
        ci.next();
    }

    return getStringBounds(arr,0,arr.length,frc);
}