Search in sources :

Example 1 with CharacterIterator

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;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator)

Example 2 with CharacterIterator

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();
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator)

Example 3 with CharacterIterator

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;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator)

Example 4 with CharacterIterator

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;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator)

Example 5 with CharacterIterator

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;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator)

Aggregations

CharacterIterator (java.text.CharacterIterator)32 StringCharacterIterator (java.text.StringCharacterIterator)27 Nullable (org.jetbrains.annotations.Nullable)3 ClsFormatException (com.intellij.util.cls.ClsFormatException)2 ArrayList (java.util.ArrayList)2 Logger (com.intellij.openapi.diagnostic.Logger)1 Pair (com.intellij.openapi.util.Pair)1 Pair.pair (com.intellij.openapi.util.Pair.pair)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 CommonClassNames (com.intellij.psi.CommonClassNames)1 PsiNameHelper (com.intellij.psi.PsiNameHelper)1 ModifierFlags (com.intellij.psi.impl.cache.ModifierFlags)1 TypeInfo (com.intellij.psi.impl.cache.TypeInfo)1 com.intellij.psi.impl.java.stubs (com.intellij.psi.impl.java.stubs)1 com.intellij.psi.impl.java.stubs.impl (com.intellij.psi.impl.java.stubs.impl)1 PsiFileStub (com.intellij.psi.stubs.PsiFileStub)1 StubElement (com.intellij.psi.stubs.StubElement)1 ArrayUtil (com.intellij.util.ArrayUtil)1