示例#1
0
  private void defineEncodingConstants(
      Ruby runtime, RubyEncoding encoding, byte[] name, int p, int end) {
    Encoding enc = ASCIIEncoding.INSTANCE;
    int s = p;

    int code = name[s] & 0xff;
    if (enc.isDigit(code)) return;

    boolean hasUpper = false;
    boolean hasLower = false;
    if (enc.isUpper(code)) {
      hasUpper = true;
      while (++s < end && (enc.isAlnum(name[s] & 0xff) || name[s] == (byte) '_')) {
        if (enc.isLower(name[s] & 0xff)) hasLower = true;
      }
    }

    boolean isValid = false;
    if (s >= end) {
      isValid = true;
      defineEncodingConstant(runtime, encoding, name, p, end);
    }

    if (!isValid || hasLower) {
      if (!hasLower || !hasUpper) {
        do {
          code = name[s] & 0xff;
          if (enc.isLower(code)) hasLower = true;
          if (enc.isUpper(code)) hasUpper = true;
        } while (++s < end && (!hasLower || !hasUpper));
      }

      byte[] constName = new byte[end - p];
      System.arraycopy(name, p, constName, 0, end - p);
      s = 0;
      code = constName[s] & 0xff;

      if (!isValid) {
        if (enc.isLower(code)) constName[s] = AsciiTables.ToUpperCaseTable[code];
        for (; s < constName.length; ++s) {
          if (!enc.isAlnum(constName[s] & 0xff)) constName[s] = (byte) '_';
        }
        if (hasUpper) {
          defineEncodingConstant(runtime, encoding, constName, 0, constName.length);
        }
      }
      if (hasLower) {
        for (s = 0; s < constName.length; ++s) {
          code = constName[s] & 0xff;
          if (enc.isLower(code)) constName[s] = AsciiTables.ToUpperCaseTable[code];
        }
        defineEncodingConstant(runtime, encoding, constName, 0, constName.length);
      }
    }
  }