MCPcopy Create free account
hub / github.com/GaijinEntertainment/daScript / unescapeString

Function unescapeString

src/simulate/runtime_string.cpp:365–431  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

363 }
364
365 string unescapeString ( const string & input, bool * error, bool ) {
366 if ( error ) *error = false;
367 const char* str = input.c_str();
368 const char* strEnd = str + input.length();
369 string result;
370 result.reserve(input.size());
371 for( ; str < strEnd; ++str ) {
372 if ( *str=='\\' ) {
373 ++str;
374 if ( str == strEnd ) {
375 if ( error ) *error = true;
376 return result; // invalid escape sequence
377 }
378 switch ( *str ) {
379 case '"':
380 case '/':
381 case '\\': result += *str; break;
382 case 'b': result += '\b'; break;
383 case 'f': result += '\f'; break;
384 case 'n': result += '\n'; break;
385 case 'r': result += '\r'; break;
386 case 't': result += '\t'; break;
387 case 'v': result += '\v'; break;
388 case '\n': break; // skip LF
389 case '{': result += '{'; break;
390 case '}': result += '}'; break;
391 case '\r': if ( str+1!=strEnd && str[1]=='\n' ) str++; break; // skip CR LF or just CR
392 case 'x':
393 case 'u':
394 case 'U': {
395 int symbols = (*str == 'x') ? 2 : (*str == 'u') ? 4 : 8;
396 uint32_t charCode = 0;
397 int x = 0;
398 str++;
399 for (; x < symbols && str != strEnd; x++, str++) {
400 int h = hexChar(*str);
401 if (h < 0)
402 break;
403 charCode = charCode * 16 + h;
404 }
405 str--;
406
407 if (symbols == 2) { // \xNN
408 if (!x) {
409 if (error) *error = true;
410 }
411 else
412 result += char(charCode);
413 }
414 else if (x != symbols) { // \u \U requires exactly 4 or 8 hex symbols
415 if (error) *error = true;
416 }
417 else {
418 char buf[8];
419 if (!encodeUtf8Char(charCode, buf) && error)
420 *error = true;
421 result += buf;
422 }

Callers 5

yyparseFunction · 0.85
yyparseFunction · 0.85
yyparseFunction · 0.85
builtin_string_unescapeFunction · 0.85

Calls 6

hexCharFunction · 0.85
encodeUtf8CharFunction · 0.85
c_strMethod · 0.45
lengthMethod · 0.45
reserveMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected