use of java.text.CharacterIterator 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.CharacterIterator 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.CharacterIterator 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.CharacterIterator in project japid42 by branaway.
the class DirUtil method isClassname.
// public static boolean hasTags(String root) {
// String dirName = DirUtil.TAGSDIR;
// return containsTemplateFiles(root, dirName);
// }
// public static boolean hasJavaTags(String root) {
// String dirName = DirUtil.JAVATAGS;
// return containsTemplateFiles(root, dirName);
// }
//
// public static boolean hasLayouts(String root) {
// String dirName = DirUtil.LAYOUTDIR;
// return containsTemplateFiles(root, dirName);
// }
//
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.CharacterIterator in project intellij-community by JetBrains.
the class Strings method isCapitalized.
public static boolean isCapitalized(@NotNull String text, @NotNull TextRange range) {
if (range.getLength() == 0)
return false;
CharacterIterator it = new StringCharacterIterator(text, range.getStartOffset() + 1, range.getEndOffset(), range.getStartOffset() + 1);
boolean lowCase = true;
for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
lowCase = Character.isLowerCase(c);
}
return Character.isUpperCase(text.charAt(range.getStartOffset())) && lowCase;
}
Aggregations