The value of a TOML basic string's body, or null for an invalid escape.
(raw: string)
| 492 | |
| 493 | /** The value of a TOML basic string's body, or null for an invalid escape. */ |
| 494 | function decodeBasicString(raw: string): string | null { |
| 495 | let out = ''; |
| 496 | for (let i = 0; i < raw.length; i++) { |
| 497 | if (raw[i] !== '\\') { |
| 498 | out += raw[i]; |
| 499 | continue; |
| 500 | } |
| 501 | const esc = raw[++i] ?? ''; |
| 502 | if (esc in SIMPLE_ESCAPES) { |
| 503 | out += SIMPLE_ESCAPES[esc]; |
| 504 | continue; |
| 505 | } |
| 506 | const width = esc === 'x' ? 2 : esc === 'u' ? 4 : esc === 'U' ? 8 : 0; |
| 507 | const hex = raw.slice(i + 1, i + 1 + width); |
| 508 | if (width === 0 || hex.length !== width || !/^[0-9A-Fa-f]+$/.test(hex)) return null; |
| 509 | const codePoint = parseInt(hex, 16); |
| 510 | if (codePoint > 0x10ffff || (codePoint >= 0xd800 && codePoint <= 0xdfff)) return null; |
| 511 | out += String.fromCodePoint(codePoint); |
| 512 | i += width; |
| 513 | } |
| 514 | return out; |
| 515 | } |
| 516 | |
| 517 | type MultilineStringDelimiter = '"""' | "'''"; |
| 518 |