Search in sources :

Example 36 with StringCharacterIterator

use of java.text.StringCharacterIterator in project lucene-solr by apache.

the class SplittingBreakIterator method getText.

@Override
public CharacterIterator getText() {
    StringCharacterIterator charIter = new StringCharacterIterator(text);
    // API doesn't say what the state should be but it should probably be at the current index.
    charIter.setIndex(current());
    return charIter;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator)

Example 37 with StringCharacterIterator

use of java.text.StringCharacterIterator in project compiler by boalang.

the class JsonFormat method escapeText.

/**
     * Implements JSON string escaping as specified <a href="http://www.ietf.org/rfc/rfc4627.txt">here</a>.
     * <ul>
     *  <li>The following characters are escaped by prefixing them with a '\' : \b,\f,\n,\r,\t,\,"</li> 
     *  <li>Other control characters in the range 0x0000-0x001F are escaped using the \\uXXXX notation</li>
     *  <li>UTF-16 surrogate pairs are encoded using the \\uXXXX\\uXXXX notation</li>
     *  <li>any other character is printed as-is</li>
     * </ul>
     */
static String escapeText(String input) {
    StringBuilder builder = new StringBuilder(input.length());
    CharacterIterator iter = new StringCharacterIterator(input);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch(c) {
            case '\b':
                builder.append("\\b");
                break;
            case '\f':
                builder.append("\\f");
                break;
            case '\n':
                builder.append("\\n");
                break;
            case '\r':
                builder.append("\\r");
                break;
            case '\t':
                builder.append("\\t");
                break;
            case '\\':
                builder.append("\\\\");
                break;
            case '"':
                builder.append("\\\"");
                break;
            default:
                // Check for other control characters
                if (c >= 0x0000 && c <= 0x001F) {
                    appendEscapedUnicode(builder, c);
                } else if (Character.isHighSurrogate(c)) {
                    // Encode the surrogate pair using 2 six-character sequence (\\uXXXX\\uXXXX)
                    appendEscapedUnicode(builder, c);
                    c = iter.next();
                    if (c == CharacterIterator.DONE)
                        throw new IllegalArgumentException("invalid unicode string: unexpected high surrogate pair value without corresponding low value.");
                    appendEscapedUnicode(builder, c);
                } else {
                    // Anything else can be printed as-is
                    builder.append(c);
                }
                break;
        }
    }
    return builder.toString();
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator)

Example 38 with StringCharacterIterator

use of java.text.StringCharacterIterator in project sling by apache.

the class CustomLogFormat method parse.

// ---------- Parsing the format pattern -----------------------------------
private Parameter[] parse(String pattern) {
    List<Parameter> parameterList = new ArrayList<Parameter>();
    StringBuilder buf = new StringBuilder();
    CharacterIterator sr = new StringCharacterIterator(pattern);
    for (int c = sr.first(); c != CharacterIterator.DONE; c = sr.next()) {
        if (c == '%') {
            int c1 = sr.next();
            if (c1 != '%') {
                if (buf.length() > 0) {
                    Parameter text = new PlainTextParameter(buf.toString());
                    parameterList.add(text);
                    buf.setLength(0);
                }
                Parameter param = this.parseFormatString(sr, c1);
                if (param != null) {
                    parameterList.add(param);
                }
                continue;
            }
        }
        buf.append((char) c);
    }
    // append any remaining plain text
    if (buf.length() > 0) {
        Parameter text = new PlainTextParameter(buf.toString());
        parameterList.add(text);
        buf.setLength(0);
    }
    return parameterList.toArray(new Parameter[parameterList.size()]);
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator) ArrayList(java.util.ArrayList)

Example 39 with StringCharacterIterator

use of java.text.StringCharacterIterator in project leopard by tanhaichao.

the class XmlUtils method escape.

public static String escape(String input) {
    if (input == null) {
        return null;
    }
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(input);
    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 {
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator)

Example 40 with StringCharacterIterator

use of java.text.StringCharacterIterator in project Lucee by lucee.

the class UUCoder method decode.

/**
 * decodes back a String to a byte array
 * @param b
 * @return decoded byte array
 */
public static byte[] decode(String str) throws CoderException {
    byte[] out = new byte[str.length()];
    int len = 0;
    int offset = 0;
    // int current = 0;
    byte b = 0;
    boolean stop = false;
    StringCharacterIterator it = new StringCharacterIterator(str);
    do {
        b = _dec(it.current());
        it.next();
        if (b > 45)
            throw new CoderException("can't decode string [" + str + "]");
        if (b < 45)
            stop = true;
        len += b;
        for (; b > 0; b -= 3) {
            decodeChars(it, out, offset);
            offset += 3;
        }
        it.next();
    } while (!stop);
    byte[] rtn = new byte[len];
    for (int i = 0; i < len; i++) rtn[i] = out[i];
    return rtn;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator)

Aggregations

StringCharacterIterator (java.text.StringCharacterIterator)41 CharacterIterator (java.text.CharacterIterator)20 Nullable (org.jetbrains.annotations.Nullable)3 ClsFormatException (com.intellij.util.cls.ClsFormatException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)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