StringBuilder (int length, @NotNull String str, @Nullable String additionalChars, boolean escapeSlash, boolean escapeUnicode, @NotNull StringBuilder buffer) { char prev = 0; for (int idx = 0; idx < length; idx++) { char ch = str.charAt(idx); switch (ch) { case '\b': buffer.append("\\b"); break; case '\t': buffer.append("\\t"); break; case '\n': buffer.append("\\n"); break; case '\f': buffer.append("\\f"); break; case '\r': buffer.append("\\r"); break; default: if (escapeSlash && ch == '\\') { buffer.append("\\\\"); } else if (additionalChars != null && additionalChars.indexOf(ch) > -1 && (escapeSlash || prev != '\\')) { buffer.append("\\").append(ch); } else if (escapeUnicode && !isPrintableUnicode(ch)) { CharSequence hexCode = toUpperCase(Integer.toHexString(ch)); buffer.append("\\u"); int paddingCount = 4 - hexCode.length(); while (paddingCount-- > 0) { buffer.append(0); } buffer.append(hexCode); } else { buffer.append(ch); } } prev = ch; } return buffer; }