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 swagger-core by swagger-api.
the class PathUtils method parsePath.
public static String parsePath(String uri, Map<String, String> patterns) {
if (uri == null) {
return null;
} else if (StringUtils.isBlank(uri)) {
return String.valueOf(SLASH);
}
CharacterIterator ci = new StringCharacterIterator(uri);
StringBuilder pathBuffer = new StringBuilder();
char c = ci.first();
if (c == CharacterIterator.DONE) {
return String.valueOf(SLASH);
}
do {
if (c == OPEN) {
String regexBuffer = cutParameter(ci, patterns);
if (regexBuffer == null) {
LOGGER.warn("Operation path \"" + uri + "\" contains syntax error.");
return null;
}
pathBuffer.append(regexBuffer);
} else {
int length = pathBuffer.length();
if (!(c == SLASH && (length != 0 && pathBuffer.charAt(length - 1) == SLASH))) {
pathBuffer.append(c);
}
}
} while ((c = ci.next()) != CharacterIterator.DONE);
return pathBuffer.toString();
}
use of java.text.StringCharacterIterator in project Japid by branaway.
the class DirUtil method isClassname.
public static boolean isClassname(String classname) {
if (classname == null || classname.length() == 0)
return false;
CharacterIterator iter = new StringCharacterIterator(classname);
// Check first character (there should at least be one character for each part) ...
char c = iter.first();
if (c == CharacterIterator.DONE)
return false;
if (!Character.isJavaIdentifierStart(c) && !Character.isIdentifierIgnorable(c))
return false;
c = iter.next();
// Check the remaining characters, if there are any ...
while (c != CharacterIterator.DONE) {
if (!Character.isJavaIdentifierPart(c) && !Character.isIdentifierIgnorable(c))
return false;
c = iter.next();
}
return true;
}
use of java.text.StringCharacterIterator in project bazel by bazelbuild.
the class ASCIIPropertyListParser method parseQuotedString.
/**
* Parses a string according to the format specified for ASCII property lists.
* Such strings can contain escape sequences which are unescaped in this method.
*
* @param s The escaped string according to the ASCII property list format, without leading and trailing quotation marks.
* @return The unescaped string in UTF-8 or ASCII format, depending on the contained characters.
* @throws Exception If the string could not be properly parsed.
*/
public static synchronized String parseQuotedString(String s) throws UnsupportedEncodingException, CharacterCodingException {
StringBuilder parsed = new StringBuilder();
StringCharacterIterator iterator = new StringCharacterIterator(s);
char c = iterator.current();
while (iterator.getIndex() < iterator.getEndIndex()) {
switch(c) {
case '\\':
{
//An escaped sequence is following
parsed.append(parseEscapedSequence(iterator));
break;
}
default:
{
parsed.append(c);
break;
}
}
c = iterator.next();
}
return parsed.toString();
}
Aggregations