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();
}
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()]);
}
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('"');
}
Aggregations