下面列出了java.text.CharacterIterator#first ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
/**
* 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;
}
/**
* 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);
}
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('"');
}
/**
* 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();
}
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);
}
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);
}
/**
* 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();
}
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);
}
/**
* 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();
}
/**
* 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;
}
/**
* 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;
}
/**
* 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();
}
/**
* 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();
}
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('"');
}
/**
* 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();
}
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);
}
/**
* 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();
}
IncrementalBuffer(CharacterIterator chars)
{
this.atEnd = false;
this.chars = chars;
this.current = chars != null ? chars.first() : CharacterIterator.DONE;
}
/**
* 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();
}