in subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/CharBuf.java [392:517]
private CharBuf doAddJsonEscapedString(char[] charArray, boolean disableUnicodeEscaping) {
char[] _buffer = buffer;
int _location = this.location;
final byte[] _encoded = encoded;
final byte[] _charTo = charTo;
/* We are making a bet that not all chars will be unicode. */
int ensureThisMuch = charArray.length * 6 + 2;
int sizeNeeded = (ensureThisMuch) + _location;
if (sizeNeeded > capacity) {
int growBy = Math.max((_buffer.length * 2), sizeNeeded);
_buffer = Chr.grow(buffer, growBy);
capacity = _buffer.length;
}
_buffer[_location] = '"';
_location++;
int index = 0;
while (true) {
char c = charArray[index];
if (shouldEscape(c, disableUnicodeEscaping)) {
/* We are covering our bet with a safety net.
otherwise we would have to have 5x buffer
allocated for control chars */
if (_location + 5 > _buffer.length) {
_buffer = Chr.grow(_buffer, 20);
}
switch (c) {
case '\"':
_buffer[_location] = '\\';
_location++;
_buffer[_location] = '"';
_location++;
break;
case '\\':
_buffer[_location] = '\\';
_location++;
_buffer[_location] = '\\';
_location++;
break;
//There is no requirement to escape solidus so we will not.
// case '/':
// _buffer[_location] = '\\';
// _location ++;
// _buffer[_location] = '/';
// _location ++;
// break;
case '\b':
_buffer[_location] = '\\';
_location++;
_buffer[_location] = 'b';
_location++;
break;
case '\f':
_buffer[_location] = '\\';
_location++;
_buffer[_location] = 'f';
_location++;
break;
case '\n':
_buffer[_location] = '\\';
_location++;
_buffer[_location] = 'n';
_location++;
break;
case '\r':
_buffer[_location] = '\\';
_location++;
_buffer[_location] = 'r';
_location++;
break;
case '\t':
_buffer[_location] = '\\';
_location++;
_buffer[_location] = 't';
_location++;
break;
default:
_buffer[_location] = '\\';
_location++;
_buffer[_location] = 'u';
_location++;
if (c <= 255) {
_buffer[_location] = '0';
_location++;
_buffer[_location] = '0';
_location++;
ByteScanner.encodeByteIntoTwoAsciiCharBytes(c, _encoded);
for (int b : _encoded) {
_buffer[_location] = (char) b;
_location++;
}
} else {
_charTo[1] = (byte) (c);
_charTo[0] = (byte) (c >>> 8);
for (int charByte : _charTo) {
ByteScanner.encodeByteIntoTwoAsciiCharBytes(charByte, _encoded);
for (int b : _encoded) {
_buffer[_location] = (char) b;
_location++;
}
}
}
}
} else {
_buffer[_location] = c;
_location++;
}
if (++index >= charArray.length) break;
}
_buffer[_location] = '"';
_location++;
buffer = _buffer;
location = _location;
return this;
}