MCPcopy Create free account
hub / github.com/Bwar/CJsonObject / parse_string

Function parse_string

cJSON.c:233–336  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

231static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0,
232 0xF8, 0xFC };
233static const char *parse_string(cJSON *item, const char *str, const char **ep)
234{
235 const char *ptr = str + 1;
236 char *ptr2;
237 char *out;
238 int len = 0;
239 unsigned uc, uc2;
240 if (*str != '\"')
241 {
242 *ep = str;
243 return 0;
244 } /* not a string! */
245
246 while (*ptr != '\"' && *ptr && ++len)
247 if (*ptr++ == '\\')
248 ptr++; /* Skip escaped quotes. */
249
250 out = (char*) cJSON_malloc(len + 1); /* This is how long we need for the string, roughly. */
251 if (!out)
252 return 0;
253
254 ptr = str + 1;
255 ptr2 = out;
256 while (*ptr != '\"' && *ptr)
257 {
258 if (*ptr != '\\')
259 *ptr2++ = *ptr++;
260 else
261 {
262 ptr++;
263 switch (*ptr)
264 {
265 case 'b':
266 *ptr2++ = '\b';
267 break;
268 case 'f':
269 *ptr2++ = '\f';
270 break;
271 case 'n':
272 *ptr2++ = '\n';
273 break;
274 case 'r':
275 *ptr2++ = '\r';
276 break;
277 case 't':
278 *ptr2++ = '\t';
279 break;
280 case 'u': /* transcode utf16 to utf8. */
281 sscanf(ptr + 1, "%4x", &uc);
282 ptr += 4; /* get the unicode char. */
283
284 if ((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0)
285 break; // check for invalid.
286
287 if (uc >= 0xD800 && uc <= 0xDBFF) // UTF16 surrogate pairs.
288 {
289 if (ptr[1] != '\\' || ptr[2] != 'u')
290 break; // missing second-half of surrogate.

Callers 2

parse_valueFunction · 0.85
parse_objectFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected