use of java.text.StringCharacterIterator in project hadoop by apache.
the class Text method utf8Length.
/**
* 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;
}
use of java.text.StringCharacterIterator in project OpenNotebook by jaltekruse.
the class MathObjectAttribute method formatForXML.
public static String formatForXML(String aText) {
final StringBuilder result = new StringBuilder();
final StringCharacterIterator iterator = new StringCharacterIterator(aText);
char character = iterator.current();
while (character != CharacterIterator.DONE) {
if (character == '<') {
result.append("<");
} else if (character == '>') {
result.append(">");
} else if (character == '\"') {
result.append(""");
} else if (character == '\'') {
result.append("'");
} else if (character == '&') {
result.append("&");
} else {
//the char is not a special one
//add it to the result as is
result.append(character);
}
character = iterator.next();
}
return result.toString();
}
use of java.text.StringCharacterIterator in project OpenAM by OpenRock.
the class PolicyClientServlet method displayXML.
//This is a utility function used to hack up an HTML display of an XML
//string.
private String displayXML(String input) {
StringCharacterIterator iter = new StringCharacterIterator(input);
StringBuffer buf = new StringBuffer();
for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
if (c == '>') {
buf.append(">");
} else if (c == '<') {
buf.append("<");
} else if (c == '\n') {
buf.append("<BR>\n");
} else {
buf.append(c);
}
}
return buf.toString();
}
use of java.text.StringCharacterIterator in project Gradle-demo by Arisono.
the class JsonValidator method valid.
private boolean valid(String input) {
if ("".equals(input))
return true;
boolean ret = true;
it = new StringCharacterIterator(input);
c = it.first();
col = 1;
if (!value()) {
ret = error("value", 1);
} else {
skipWhiteSpace();
if (c != CharacterIterator.DONE) {
ret = error("end", col);
}
}
return ret;
}
use of java.text.StringCharacterIterator in project intellij-community by JetBrains.
the class RestTitle method getUnderline.
@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());
}
Aggregations