MCPcopy Create free account
hub / github.com/acl-dev/acl / parse_string

Function parse_string

lib_acl/samples/json/json5/cJSON.cpp:192–249  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

190/* Parse the input text into an unescaped cstring, and populate item. */
191static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
192static const char *parse_string(cJSON *item,const char *str)
193{
194 const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2;
195 if (*str!='\"') {ep=str;return 0;} /* not a string! */
196
197 while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
198
199 out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */
200 if (!out) return 0;
201
202 ptr=str+1;ptr2=out;
203 while (*ptr!='\"' && *ptr)
204 {
205 if (*ptr!='\\') *ptr2++=*ptr++;
206 else
207 {
208 ptr++;
209 switch (*ptr)
210 {
211 case 'b': *ptr2++='\b'; break;
212 case 'f': *ptr2++='\f'; break;
213 case 'n': *ptr2++='\n'; break;
214 case 'r': *ptr2++='\r'; break;
215 case 't': *ptr2++='\t'; break;
216 case 'u': /* transcode utf16 to utf8. */
217 uc=parse_hex4(ptr+1);ptr+=4; /* get the unicode char. */
218
219 if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; /* check for invalid. */
220
221 if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */
222 {
223 if (ptr[1]!='\\' || ptr[2]!='u') break; /* missing second-half of surrogate. */
224 uc2=parse_hex4(ptr+3);ptr+=6;
225 if (uc2<0xDC00 || uc2>0xDFFF) break; /* invalid second-half of surrogate. */
226 uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF));
227 }
228
229 len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len;
230
231 switch (len) {
232 case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
233 case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
234 case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
235 case 1: *--ptr2 =(uc | firstByteMark[len]);
236 }
237 ptr2+=len;
238 break;
239 default: *ptr2++=*ptr; break;
240 }
241 ptr++;
242 }
243 }
244 *ptr2=0;
245 if (*ptr=='\"') ptr++;
246 item->valuestring=out;
247 item->type=cJSON_String;
248 return ptr;
249}

Callers 2

parse_valueFunction · 0.85
parse_objectFunction · 0.85

Calls 1

parse_hex4Function · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…