static int32_t decodeEscaped()

in runtime/under-codecs-module.cpp [172:248]


static int32_t decodeEscaped(const Byteslike& bytes, word* i,
                             word* invalid_escape_index) {
  word length = bytes.length();
  switch (byte ch = bytes.byteAt((*i)++)) {
      // \x escapes
    case '\n':
      return -1;
    case '\\':
    case '\'':
    case '\"':
      return ch;
    case 'b':
      return '\b';
    case 't':
      return '\t';
    case 'n':
      return '\n';
    case 'r':
      return '\r';
    // BEL,
    case 'a':
      return '\x07';
    // VT
    case 'v':
      return '\x0B';
    // FF
    case 'f':
      return '\x0C';

    // \OOO (octal) escapes
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7': {
      word escaped = ch - '0';
      word octal_index = *i;
      if (octal_index < length) {
        word ch2 = bytes.byteAt(octal_index);
        if ('0' <= ch2 && ch2 <= '7') {
          escaped = (escaped << 3) + ch2 - '0';
          if (++octal_index < length) {
            word ch3 = bytes.byteAt(octal_index);
            if ('0' <= ch3 && ch3 <= '7') {
              octal_index++;
              escaped = (escaped << 3) + ch3 - '0';
            }
          }
        }
      }
      *i = octal_index;
      return escaped;
    }

    // hex escapes
    // \xXX
    case 'x': {
      word hex_index = *i;
      if (hex_index + 1 < length) {
        int digit1, digit2;
        digit1 = _PyLong_DigitValue[bytes.byteAt(hex_index)];
        digit2 = _PyLong_DigitValue[bytes.byteAt(hex_index + 1)];
        if (digit1 < 16 && digit2 < 16) {
          *i += 2;
          return (digit1 << 4) + digit2;
        }
      }
      return -2;
    }
    default:
      *invalid_escape_index = *i - 1;
      return ch;
  }
}