in src/com/amazon/ion/impl/_Private_IonTextAppender.java [573:668]
private final void printCodePoints(CharSequence text, String[] escapes)
throws IOException
{
int len = text.length();
for (int i = 0; i < len; ++i)
{
// Find a span of non-escaped ASCII code points so we can write
// them as quickly as possible.
char c = 0;
int j;
for (j = i; j < len; ++j) {
c = text.charAt(j);
// The escapes array always includes U+80 through U+FF.
if (c >= 0x100 || escapes[c] != null)
{
// c is escaped and/or outside ASCII range.
if (j > i) {
appendAscii(text, i, j);
i = j;
}
break;
}
}
if (j == len) {
// we've reached the end of sequence; append it and break
appendAscii(text, i, j);
break;
}
// We've found a code point that's escaped and/or non-ASCII.
if (c < 0x80)
{
// An escaped ASCII character.
assert escapes[c] != null;
appendAscii(escapes[c]);
}
else if (c < 0x100)
{
// Non-ASCII LATIN-1; we will have an escape sequence but may
// not use it.
assert escapes[c] != null;
// Always escape the C1 control codes U+80 through U+9F.
if (escapeNonAscii || c <= 0x9F) {
appendAscii(escapes[c]);
} else {
appendUtf16(c);
}
}
else if (c < 0xD800 || c >= 0xE000)
{
// Not LATIN-1, but still in the BMP.
String s = Integer.toHexString(c);
if (escapeNonAscii) {
appendAscii(HEX_4_PREFIX);
appendAscii(ZERO_PADDING[4 - s.length()]);
appendAscii(s);
} else {
appendUtf16(c);
}
}
else if (isHighSurrogate(c))
{
// Outside the BMP! High surrogate must be followed by low.
char c2;
if (++i == len || !isLowSurrogate(c2 = text.charAt(i))) {
String message =
"text is invalid UTF-16. It contains an unmatched " +
"leading surrogate 0x" + Integer.toHexString(c) +
" at index " + (i-1);
throw new IllegalArgumentException(message);
}
if (escapeNonAscii) {
int cp = makeUnicodeScalar(c, c2);
String s = Integer.toHexString(cp);
appendAscii(HEX_8_PREFIX);
appendAscii(ZERO_PADDING[8 - s.length()]);
appendAscii(s);
} else {
appendUtf16Surrogate(c, c2);
}
}
else
{
// unmatched low surrogate
assert isLowSurrogate(c);
String message =
"text is invalid UTF-16. It contains an unmatched " +
"trailing surrogate 0x" + Integer.toHexString(c) +
" at index " + i;
throw new IllegalArgumentException(message);
}
}
}