static void AppendChar(StringBuilder sb, char ch)
{
switch (ch) {
case '\\':
sb.Append("\\\\");
break;
case '\0':
sb.Append("\\0");
break;
case '\a':
sb.Append("\\a");
break;
case '\b':
sb.Append("\\b");
break;
case '\f':
sb.Append("\\f");
break;
case '\n':
sb.Append("\\n");
break;
case '\r':
sb.Append("\\r");
break;
case '\t':
sb.Append("\\t");
break;
case '\v':
sb.Append("\\v");
break;
default:
if (char.IsControl(ch) || char.IsSurrogate(ch) ||
// print all uncommon white spaces as numbers
(char.IsWhiteSpace(ch) && ch != ' ')) {
sb.Append("\\u");
sb.Append(((int)ch).ToString("x4"));
} else {
sb.Append(ch);
}
break;
}
}