Search in sources :

Example 31 with CharacterIterator

use of java.text.CharacterIterator 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 32 with CharacterIterator

use of java.text.CharacterIterator 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 33 with CharacterIterator

use of java.text.CharacterIterator in project rabbitmq-java-client by rabbitmq.

the class JSONWriter method string.

private void string(Object obj) {
    add('"');
    CharacterIterator it = new StringCharacterIterator(obj.toString());
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if (c == '"')
            add("\\\"");
        else if (c == '\\')
            add("\\\\");
        else if (c == '/')
            add("\\/");
        else if (c == '\b')
            add("\\b");
        else if (c == '\f')
            add("\\f");
        else if (c == '\n')
            add("\\n");
        else if (c == '\r')
            add("\\r");
        else if (c == '\t')
            add("\\t");
        else if (Character.isISOControl(c)) {
            unicode(c);
        } else {
            add(c);
        }
    }
    add('"');
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator)

Aggregations

CharacterIterator (java.text.CharacterIterator)33 StringCharacterIterator (java.text.StringCharacterIterator)28 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