package com.fasterxml.jackson.dataformat.javaprop.io;
import java.util.Arrays;
public class JPropEscapes
{
private final static char[] HEX = "0123456789ABCDEF".toCharArray();
private final static int UNICODE_ESCAPE = -1;
private final static int[] sValueEscapes;
static {
final int[] table = new int[256];
for (int i = 0; i < 32; ++i) {
table[i] = UNICODE_ESCAPE;
table[128+i] = UNICODE_ESCAPE;
}
table[0x7F] = UNICODE_ESCAPE;
table['\t'] = 't';
table['\r'] = 'r';
table['\n'] = 'n';
table['\\'] = '\\';
sValueEscapes = table;
}
private final static int[] sKeyEscapes;
static {
final int[] table = Arrays.copyOf(sValueEscapes, 256);
table['#'] = '#';
table['!'] = '!';
table['='] = '=';
table[':'] = ':';
table[' '] = ' ';
sKeyEscapes = table;
}
public static void appendKey(StringBuilder sb, String key) {
final int end = key.length();
if (end == 0) {
return;
}
final int[] esc = sKeyEscapes;
int i = 0;
while (true) {
char c = key.charAt(i);
if ((c > 0xFF) || esc[c] != 0) {
break;
}
sb.append(c);
if (++i == end) {
return;
}
}
_appendWithEscapes(sb, key, esc, i);
}
public static StringBuilder appendValue(String value) {
final int end = value.length();
if (end == 0) {
return null;
}
final int[] esc = sValueEscapes;
int i = 0;
while (true) {
char c = value.charAt(i);
if ((c > 0xFF) || esc[c] != 0) {
break;
}
if (++i == end) {
return null;
}
}
StringBuilder sb = new StringBuilder(end + 5 + (end>>3));
for (int j = 0; j < i; ++j) {
sb.append(value.charAt(j));
}
_appendWithEscapes(sb, value, esc, i);
return sb;
}
private static void _appendWithEscapes(StringBuilder sb, String key,
int[] esc, int i)
{
final int end = key.length();
do {
char c = key.charAt(i);
int type = (c > 0xFF) ? UNICODE_ESCAPE : esc[c];
if (type == 0) {
sb.append(c);
continue;
}
if (type == UNICODE_ESCAPE) {
sb.append('\\');
sb.append('u');
sb.append(HEX[c >>> 12]);
sb.append(HEX[(c >> 8) & 0xF]);
sb.append(HEX[(c >> 4) & 0xF]);
sb.append(HEX[c & 0xF]);
} else {
sb.append('\\');
sb.append((char) type);
}
} while (++i < end);
}
}