static String escape()

in src/main/java/org/apache/sling/engine/impl/log/CustomLogFormat.java [470:526]


        static String escape(String value) {
            // nothing to do for empty values
            if (value == null || value.length() == 0) {
                return value;
            }

            // find the first non-printable
            int i = 0;
            while (i < value.length() && isPrint(value.charAt(i))) {
                i++;
            }

            // if none has been found, just return the value
            if (i >= value.length()) {
                return value;
            }

            // otherwise copy the printable first part in a string buffer
            // and start encoding
            StringBuilder buf = new StringBuilder(value.substring(0, i));
            while (i < value.length()) {
                char c = value.charAt(i);
                if (isPrint(c)) {
                    buf.append(c);
                } else if (c == '\n') { // LF
                    buf.append("\\n");
                } else if (c == '\r') { // CR
                    buf.append("\\r");
                } else if (c == '\t') { // HTAB
                    buf.append("\\t");
                } else if (c == '\f') { // VTAB
                    buf.append("\\f");
                } else if (c == '\b') { // BSP
                    buf.append("\\b");
                } else if (c == '"') { // "
                    buf.append("\\\"");
                } else if (c == '\\') { // \
                    buf.append("\\\\");
                } else { // encode
                    buf.append("\\u");
                    if (c < 0x10) {
                        buf.append('0'); // leading zero
                    }
                    if (c < 0x100) {
                        buf.append('0'); // leading zero
                    }
                    if (c < 0x1000) {
                        buf.append('0'); // leading zero
                    }
                    buf.append(Integer.toHexString(c));
                }
                i++;
            }

            // return the encoded string value
            return buf.toString();
        }