MCPcopy Index your code
hub / github.com/armink/struct2json / parse_string

Function parse_string

struct2json/src/cJSON.c:193–250  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 2

parse_valueFunction · 0.85
parse_objectFunction · 0.85

Calls 1

parse_hex4Function · 0.85

Tested by

no test coverage detected