text
stringlengths 0
14.1k
|
---|
if (ctx->options & JSON_PARSE_IGNORE_ENCODING) |
check_utf8 = false; |
while (src < end) { |
if (!string_examine_chars[(uint8_t)*src]) { |
src++; |
} else if (*src == '""') { |
/* string end */ |
*hasesc_p = hasesc; |
*str_end_p = src; |
*nlines_p = lines; |
return true; |
} else if (*src == '\\') { |
hasesc = true; |
src++; |
if (src < end && (*src == '\\' || *src == '""')) |
src++; |
} else if (*src & 0x80) { |
n = utf8_validate_seq(src, end); |
if (n) { |
src += n; |
} else if (check_utf8) { |
goto badutf; |
} else { |
src++; |
} |
} else if (*src == '\n') { |
lines++; |
src++; |
} else { |
goto badutf; |
} |
} |
return err_false(ctx, ""Unexpected end of string""); |
badutf: |
return err_false(ctx, ""Invalid UTF8 sequence""); |
} |
/* string boundaries are known, copy and unescape */ |
static char *process_escapes(struct JsonContext *ctx, |
const char *src, const char *end, |
char *dst, char *dstend) |
{ |
const char *esc; |
/* process escapes */ |
while (src < end) { |
esc = memchr(src, '\\', end - src); |
if (!esc) { |
dst = plain_copy(dst, src, end); |
break; |
} |
dst = plain_copy(dst, src, esc); |
src = esc + 1; |
switch (*src++) { |
case '""': *dst++ = '""'; break; |
case '\\': *dst++ = '\\'; break; |
case '/': *dst++ = '/'; break; |
case 'b': *dst++ = '\b'; break; |
case 'f': *dst++ = '\f'; break; |
case 'n': *dst++ = '\n'; break; |
case 'r': *dst++ = '\r'; break; |
case 't': *dst++ = '\t'; break; |
case 'u': |
if (!parse_uescape(ctx, &dst, dstend, &src, end)) |
return NULL; |
break; |
default: |
return err_null(ctx, ""Invalid escape code""); |
} |
} |
return dst; |
} |
/* 2-phase string processing */ |
static bool parse_string(struct JsonContext *ctx, const char **src_p, const char *end) |
{ |
const char *start, *strend = NULL; |
bool hasesc = false; |
char *dst, *dstend; |
size_t len; |
struct JsonValue *jv; |
int64_t lines = 0; |
/* find string boundaries, validate */ |
start = *src_p; |
if (!scan_string(ctx, start, end, &strend, &hasesc, &lines)) |
return false; |
/* create value struct */ |
len = strend - start; |
jv = mk_value(ctx, JSON_STRING, len + 1, true); |
if (!jv) |
return false; |
dst = get_cstring(jv); |
dstend = dst + len; |
/* copy & process escapes */ |