# Parses and returns a Nit string from a JSON String
fun parse_json_string: Serializable do
var src = src
var ln = src.length
var p = pos
p += 1
if p > ln then return make_parse_error("Malformed JSON String")
var c = src[p]
var ret = parse_str_buf
var chunk_st = p
while c != '"' do
if c != '\\' then
p += 1
if p >= ln then return make_parse_error("Malformed JSON string")
c = src[p]
continue
end
ret.append_substring_impl(src, chunk_st, p - chunk_st)
p += 1
if p >= ln then return make_parse_error("Malformed Escape sequence in JSON string")
c = src[p]
if c == 'r' then
ret.add '\r'
p += 1
else if c == 'n' then
ret.add '\n'
p += 1
else if c == 't' then
ret.add '\t'
p += 1
else if c == 'u' then
var cp = 0
p += 1
for i in [0 .. 4[ do
cp <<= 4
if p >= ln then make_parse_error("Malformed \uXXXX Escape sequence in JSON string")
c = src[p]
if c >= '0' and c <= '9' then
cp += c.code_point - '0'.code_point
else if c >= 'a' and c <= 'f' then
cp += c.code_point - 'a'.code_point + 10
else if c >= 'A' and c <= 'F' then
cp += c.code_point - 'A'.code_point + 10
else
make_parse_error("Malformed \uXXXX Escape sequence in JSON string")
end
p += 1
end
c = cp.code_point
if cp >= 0xD800 and cp <= 0xDBFF then
if p >= ln then make_parse_error("Malformed \uXXXX Escape sequence in JSON string")
c = src[p]
if c != '\\' then make_parse_error("Malformed \uXXXX Escape sequence in JSON string")
p += 1
c = src[p]
if c != 'u' then make_parse_error("Malformed \uXXXX Escape sequence in JSON string")
var locp = 0
p += 1
for i in [0 .. 4[ do
locp <<= 4
if p > ln then make_parse_error("Malformed \uXXXX Escape sequence in JSON string")
c = src[p]
if c >= '0' and c <= '9' then
locp += c.code_point - '0'.code_point
else if c >= 'a' and c <= 'f' then
locp += c.code_point - 'a'.code_point + 10
else if c >= 'A' and c <= 'F' then
locp += c.code_point - 'A'.code_point + 10
else
make_parse_error("Malformed \uXXXX Escape sequence in JSON string")
end
p += 1
end
c = (((locp & 0x3FF) | ((cp & 0x3FF) << 10)) + 0x10000).code_point
end
ret.add c
else if c == 'b' then
ret.add 8.code_point
p += 1
else if c == 'f' then
ret.add '\f'
p += 1
else
p += 1
ret.add c
end
chunk_st = p
c = src[p]
end
pos = p + 1
if ret.is_empty then return src.substring(chunk_st, p - chunk_st)
ret.append_substring_impl(src, chunk_st, p - chunk_st)
var rets = ret.to_s
ret.clear
return rets
end
lib/json/static.nit:352,2--447,4