Core: AsCString new flag: ASCSTRING_JSON

git-svn-id: svn://ultimatepp.org/upp/trunk@5176 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-07-11 09:01:18 +00:00
parent a175d10e24
commit cbaf5dd1ce
5 changed files with 804 additions and 773 deletions

File diff suppressed because it is too large Load diff

View file

@ -111,10 +111,12 @@ String AsJSON(const Value& v, const String& sep, bool pretty)
if(IsNumber(v))
return Format("%.16g", (double)v);
if(IsString(v))
return AsCString((String)v);
return AsCString((String)v, INT_MAX, NULL, ASCSTRING_OCTALHI|ASCSTRING_JSON);
if(IsNull(v))
return "null";
#ifdef SVO_VALUE
NEVER_("Non-JSON value in AsJSON: " + v.GetTypeName());
#endif
return "null";
}

View file

@ -132,6 +132,7 @@ inline bool CParser::Char3(char c1, char c2, char c3)
enum {
ASCSTRING_SMART = 0x01,
ASCSTRING_OCTALHI = 0x02,
ASCSTRING_JSON = 0x04,
};
String AsCString(const char *s, const char *end, int linemax = INT_MAX, const char *linepfx = NULL,

View file

@ -451,6 +451,11 @@ inline void NextCStringLine(String& t, const char *linepfx, int& pl)
pl = t.GetLength();
}
inline int HexDigit(int c)
{
return "0123456789ABCDEF"[c & 15];
}
String AsCString(const char *s, const char *lim, int linemax, const char *linepfx, dword flags)
{
String t;
@ -472,14 +477,27 @@ String AsCString(const char *s, const char *lim, int linemax, const char *linepf
case '\\': t.Cat("\\\\"); break;
case '\n': t.Cat("\\n"); wasspace = true; break;
default:
if(byte(*s) < 32 || (byte)*s >= 0x7f && (flags & ASCSTRING_OCTALHI) || (byte)*s == 0xff) {
char h[4];
int q = (byte)*s;
h[0] = '\\';
h[1] = (3 & (q >> 6)) + '0';
h[2] = (7 & (q >> 3)) + '0';
h[3] = (7 & q) + '0';
t.Cat(h, 4);
if(byte(*s) < 32 || (byte)*s >= 0x7f && (flags & ASCSTRING_OCTALHI) || (byte)*s == 0xff || (byte)*s == 0x7f) {
if(flags & ASCSTRING_JSON) {
char h[6];
int q = (byte)*s;
h[0] = '\\';
h[1] = 'u';
h[2] = '0';
h[3] = '0';
h[4] = HexDigit(q >> 4);
h[5] = HexDigit(q);
t.Cat(h, 6);
}
else {
char h[4];
int q = (byte)*s;
h[0] = '\\';
h[1] = (3 & (q >> 6)) + '0';
h[2] = (7 & (q >> 3)) + '0';
h[3] = (7 & q) + '0';
t.Cat(h, 4);
}
}
else {
t.Cat(*s);

View file

@ -454,8 +454,14 @@ ending `"`\`"`\n`" and [@3 linepfx] is inserted and literal continues
on the new line.&]
[s7;%% [%-*@3 linepfx]-|Pointer to zero`-terminated text to be inserted
at the beginning of the line when the line length is exceeded.&]
[s7;%% [%-*@3 flags]-|true activates smarter breaking into lines `-
breaks at spaces are preferred.&]
[s7;%% [%-*@3 flags]-|a combination of flags:&]
[s0;~~~1408;%% -|ASCSTRING`_SMART-|breaks string into lines when too
long&]
[s0;~~~1408;%% -|ASCSTRING`_OCTALHI-|escapes characters >128&]
[s0;~~~1408;%% -|ASCSTRING`_JSON-|uses JSON notation for escapes (`\u0001
-| instead of `\001)&]
[s0;~~~1408;%% &]
[s0;%% &]
[s7;%% [*/ Return value]-|C`-like literal.&]
[s3;%% &]
[s4; &]
@ -479,4 +485,4 @@ sCString]([%-*@3 s].[^topic`:`/`/Core`/src`/String`$en`-us`#String0`:`:Begin`(`)
egin](),[%- _][%-*@3 s].[^topic`:`/`/Core`/src`/String`$en`-us`#String0`:`:End`(`)const^ E
nd](),[%- _][%-*@3 linemax],[%- _][%-*@3 linepfx],[%- _][%-*@3 flags]).&]
[s3;%% &]
[s0; ]
[s0; ]]