Search in sources :

Example 1 with StringCharacterIterator

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

Example 2 with StringCharacterIterator

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("&lt;");
        } else if (character == '>') {
            result.append("&gt;");
        } else if (character == '\"') {
            result.append("&quot;");
        } else if (character == '\'') {
            result.append("&#039;");
        } else if (character == '&') {
            result.append("&amp;");
        } else {
            //the char is not a special one
            //add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator)

Example 3 with StringCharacterIterator

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

Example 4 with StringCharacterIterator

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

Example 5 with StringCharacterIterator

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

Aggregations

StringCharacterIterator (java.text.StringCharacterIterator)35 CharacterIterator (java.text.CharacterIterator)19 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