MCPcopy Create free account
hub / github.com/Tencent/TAD_Sim / parse_string

Function parse_string

common/map_sdk/transmission/src/cJSON.cpp:177–268  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 2

parse_valueFunction · 0.85
parse_objectFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected