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

The following examples show how to use java.text.CharacterIterator#first() . 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: JSONWriter.java    From alipay-sdk-java-all with Apache License 2.0 6 votes vote down vote up
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('"');
}
 
Example 2
Source File: WhitespaceBasedBreakIterator.java    From hottub 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: WhitespaceBasedBreakIterator.java    From jdk8u-dev-jdk 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 4
Source File: Locator.java    From L2jOrg with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Decodes an Uri with % characters.
 *
 * @param uri String with the uri possibly containing % characters.
 * @return The decoded Uri
 */
private static String decodeUri(String uri) {
    if (uri.indexOf('%') == -1) {
        return uri;
    }
    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);
        }
    }
    String path = sb.toString();
    return path;
}
 
Example 5
Source File: RuleBasedBreakIterator.java    From jdk-1.7-annotated with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 6
Source File: RuleBasedBreakIterator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 7
Source File: StandardGlyphVector.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public StandardGlyphVector(Font font, CharacterIterator iter, FontRenderContext frc) {
    int offset = iter.getBeginIndex();
    char[] text = new char [iter.getEndIndex() - offset];
    for(char c = iter.first();
        c != CharacterIterator.DONE;
        c = iter.next()) {
        text[iter.getIndex() - offset] = c;
    }
    init(font, text, 0, text.length, frc, UNINITIALIZED_FLAGS);
}
 
Example 8
Source File: RuleBasedBreakIterator.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 9
Source File: JSONWriter.java    From pay with Apache License 2.0 5 votes vote down vote up
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('"');
}
 
Example 10
Source File: RuleBasedBreakIterator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 11
Source File: RuleBasedBreakIterator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 12
Source File: Text.java    From Canova with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
                iter.previous(); // rewind one
            }
        } 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;
}
 
Example 13
Source File: Text.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
        iter.previous(); // rewind one
      }
    } 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;
}
 
Example 14
Source File: GeneralUtils.java    From ade with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a string based on the input string, but with all characters
 * with ordinal values < 32 or >= 128 replaced with ' '.
 * 
 * @param src - The string to clean
 * 
 * @return The original string if it does not contain any characters
 *     outside the allowed range, or a new string with any characters
 *     outside the allowed range converted to ' '
 */
public static String cleanString(String src) {
    if (src == null) {
        return null;
    }

    boolean foundBad = false;
    final CharacterIterator it = new StringCharacterIterator(src);
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if (c < ASCII_PRINTABLE_LOW || c >= ASCII_PRINTABLE_HI) {
            foundBad = true;
            break;
        }
    }

    if (!foundBad) {
        return src;
    }

    final StringBuilder res = new StringBuilder();
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if (c < ASCII_PRINTABLE_LOW || c >= ASCII_PRINTABLE_HI) {
            res.append(ASCII_SPACE);
        } else {
            res.append(c);
        }
    }

    return res.toString();
}
 
Example 15
Source File: StandardGlyphVector.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public StandardGlyphVector(Font font, CharacterIterator iter, FontRenderContext frc) {
    int offset = iter.getBeginIndex();
    char[] text = new char [iter.getEndIndex() - offset];
    for(char c = iter.first();
        c != CharacterIterator.DONE;
        c = iter.next()) {
        text[iter.getIndex() - offset] = c;
    }
    init(font, text, 0, text.length, frc, UNINITIALIZED_FLAGS);
}
 
Example 16
Source File: RuleBasedBreakIterator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 17
Source File: StandardGlyphVector.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public StandardGlyphVector(Font font, CharacterIterator iter, FontRenderContext frc) {
    int offset = iter.getBeginIndex();
    char[] text = new char [iter.getEndIndex() - offset];
    for(char c = iter.first();
        c != CharacterIterator.DONE;
        c = iter.next()) {
        text[iter.getIndex() - offset] = c;
    }
    init(font, text, 0, text.length, frc, UNINITIALIZED_FLAGS);
}
 
Example 18
Source File: StandardGlyphVector.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public StandardGlyphVector(Font font, CharacterIterator iter, FontRenderContext frc) {
    int offset = iter.getBeginIndex();
    char[] text = new char [iter.getEndIndex() - offset];
    for(char c = iter.first();
        c != CharacterIterator.DONE;
        c = iter.next()) {
        text[iter.getIndex() - offset] = c;
    }
    init(font, text, 0, text.length, frc, UNINITIALIZED_FLAGS);
}
 
Example 19
Source File: ParserThread.java    From bigtable-sql with Apache License 2.0 4 votes vote down vote up
IncrementalBuffer(CharacterIterator chars)
{
	this.atEnd = false;
	this.chars = chars;
	this.current = chars != null ? chars.first() : CharacterIterator.DONE;
}
 
Example 20
Source File: BirtResources.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Escapes a string to make it usable in JavaScript.
 * @param s input string
 * @return escaped string, without quotes
 */
public static String makeJavaScriptString( String s )
{
	StringBuffer output = new StringBuffer( s.length( ) );
	CharacterIterator it = new StringCharacterIterator(s);		
	for (char c = it.first(); c != CharacterIterator.DONE; c = it.next())
	{
		switch ( c )
		{
			// backspace
			case 0x08:
				output.append( BACKSLASH + "b" );
				break;
			// tab
			case 0x09:
				output.append( BACKSLASH + "t" );
				break;
			// newline
			case 0x0A:
				output.append( BACKSLASH + "n" );
				break;
			// form feed
			case 0x0C:
				output.append( BACKSLASH + "f" );
				break;
			// carriage return
			case 0x0D:
				output.append( BACKSLASH + "r" );
				break;
			// single quote
			case 0x27:
			// double quote
			case 0x22:
			// slash
			case 0x2F:
			// backslash
			case 0x5C:
				output.append( BACKSLASH + c );
				break;
			// string ranges
			default:
				output.append( c );
		}			
	}
	return output.toString();
}