std.json: Inline appendJSONChar

Since the previous commit, it was only called from one place.
This commit is contained in:
Vladimir Panteleev 2017-06-26 08:11:39 +00:00
parent 55aa34e440
commit e44666fc51

View file

@ -1117,7 +1117,25 @@ string toJSON(const ref JSONValue root, in bool pretty = false, in JSONOptions o
case '\r': json.put("\\r"); break; case '\r': json.put("\\r"); break;
case '\t': json.put("\\t"); break; case '\t': json.put("\\t"); break;
default: default:
appendJSONChar(json, c, options); {
import std.uni : isControl;
with (JSONOptions) if (isControl(c) ||
((options & escapeNonAsciiChars) >= escapeNonAsciiChars && c >= 0x80))
{
json.put("\\u");
foreach_reverse (i; 0 .. 4)
{
char ch = (c >>> (4 * i)) & 0x0f;
ch += ch < 10 ? '0' : 'A' - 10;
json.put(ch);
}
}
else
{
json.put(c);
}
}
} }
} }
@ -1284,27 +1302,6 @@ string toJSON(const ref JSONValue root, in bool pretty = false, in JSONOptions o
return json.data; return json.data;
} }
private void appendJSONChar(ref Appender!string dst, dchar c, JSONOptions opts) @safe
{
import std.uni : isControl;
with (JSONOptions) if (isControl(c) ||
((opts & escapeNonAsciiChars) >= escapeNonAsciiChars && c >= 0x80))
{
dst.put("\\u");
foreach_reverse (i; 0 .. 4)
{
char ch = (c >>> (4 * i)) & 0x0f;
ch += ch < 10 ? '0' : 'A' - 10;
dst.put(ch);
}
}
else
{
dst.put(c);
}
}
@safe unittest // bugzilla 12897 @safe unittest // bugzilla 12897
{ {
JSONValue jv0 = JSONValue("test测试"); JSONValue jv0 = JSONValue("test测试");