Esempio n. 1
0
 @TruffleBoundary
 @Specialization
 public DynamicObject toS(DynamicObject value, NotProvided base) {
   return create7BitString(
       StringOperations.encodeByteList(
           Layouts.BIGNUM.getValue(value).toString(), USASCIIEncoding.INSTANCE));
 }
Esempio n. 2
0
 public Object isDefined(VirtualFrame frame) {
   return Layouts.STRING.createString(
       Layouts.CLASS.getInstanceFactory(getContext().getCoreLibrary().getStringClass()),
       StringOperations.encodeByteList("expression", UTF8Encoding.INSTANCE),
       StringSupport.CR_7BIT,
       null);
 }
Esempio n. 3
0
    @TruffleBoundary
    @Specialization
    public DynamicObject toS(DynamicObject value, int base) {
      if (base < 2 || base > 36) {
        CompilerDirectives.transferToInterpreter();
        throw new RaiseException(
            getContext().getCoreLibrary().argumentErrorInvalidRadix(base, this));
      }

      return create7BitString(
          StringOperations.encodeByteList(
              Layouts.BIGNUM.getValue(value).toString(base), USASCIIEncoding.INSTANCE));
    }
  @Override
  public Object isDefined(VirtualFrame frame) {
    CompilerDirectives.transferToInterpreter();
    final DynamicObject threadLocalVariablesObject =
        threadLocalVariablesObjectNode.executeDynamicObject(frame);

    if (readNode.getName().equals("$~")
        || readNode.getName().equals("$!")
        || readNode.execute(threadLocalVariablesObject) != nil()) {
      return create7BitString(
          StringOperations.encodeByteList("global-variable", UTF8Encoding.INSTANCE));
    } else {
      return nil();
    }
  }
Esempio n. 5
0
  @Specialization
  public Object read(VirtualFrame frame, byte[] source) {
    // Bit string logic copied from jruby.util.Pack - see copyright and authorship there

    final ByteBuffer encode =
        ByteBuffer.wrap(
            source, getSourcePosition(frame), getSourceLength(frame) - getSourcePosition(frame));

    int occurrences = encode.remaining();

    int length = encode.remaining() * 3 / 4;
    byte[] lElem = new byte[length];
    int a = -1, b = -1, c = 0, d;
    int index = 0;
    int s = -1;

    if (occurrences == 0) {
      if (encode.remaining() % 4 != 0) {
        throw new FormatException("invalid base64");
      }
      while (encode.hasRemaining() && s != '=') {
        a = b = c = -1;
        d = -2;

        // obtain a
        s = Pack.safeGet(encode);
        a = Pack.b64_xtable[s];
        if (a == -1) throw new FormatException("invalid base64");

        // obtain b
        s = Pack.safeGet(encode);
        b = Pack.b64_xtable[s];
        if (b == -1) throw new FormatException("invalid base64");

        // obtain c
        s = Pack.safeGet(encode);
        c = Pack.b64_xtable[s];
        if (s == '=') {
          if (Pack.safeGet(encode) != '=') throw new FormatException("invalid base64");
          break;
        }
        if (c == -1) throw new FormatException("invalid base64");

        // obtain d
        s = Pack.safeGet(encode);
        d = Pack.b64_xtable[s];
        if (s == '=') break;
        if (d == -1) throw new FormatException("invalid base64");

        // calculate based on a, b, c and d
        lElem[index++] = (byte) ((a << 2 | b >> 4) & 255);
        lElem[index++] = (byte) ((b << 4 | c >> 2) & 255);
        lElem[index++] = (byte) ((c << 6 | d) & 255);
      }

      if (encode.hasRemaining()) throw new FormatException("invalid base64");

      if (a != -1 && b != -1) {
        if (c == -1 && s == '=') {
          if ((b & 15) != 0) throw new FormatException("invalid base64");
          lElem[index++] = (byte) ((a << 2 | b >> 4) & 255);
        } else if (c != -1 && s == '=') {
          if ((c & 3) != 0) throw new FormatException("invalid base64");
          lElem[index++] = (byte) ((a << 2 | b >> 4) & 255);
          lElem[index++] = (byte) ((b << 4 | c >> 2) & 255);
        }
      }
    } else {

      while (encode.hasRemaining()) {
        a = b = c = d = -1;

        // obtain a
        s = Pack.safeGet(encode);
        while (((a = Pack.b64_xtable[s]) == -1) && encode.hasRemaining()) {
          s = Pack.safeGet(encode);
        }
        if (a == -1) break;

        // obtain b
        s = Pack.safeGet(encode);
        while (((b = Pack.b64_xtable[s]) == -1) && encode.hasRemaining()) {
          s = Pack.safeGet(encode);
        }
        if (b == -1) break;

        // obtain c
        s = Pack.safeGet(encode);
        while (((c = Pack.b64_xtable[s]) == -1) && encode.hasRemaining()) {
          if (s == '=') break;
          s = Pack.safeGet(encode);
        }
        if ((s == '=') || c == -1) {
          if (s == '=') {
            encode.position(encode.position() - 1);
          }
          break;
        }

        // obtain d
        s = Pack.safeGet(encode);
        while (((d = Pack.b64_xtable[s]) == -1) && encode.hasRemaining()) {
          if (s == '=') break;
          s = Pack.safeGet(encode);
        }
        if ((s == '=') || d == -1) {
          if (s == '=') {
            encode.position(encode.position() - 1);
          }
          break;
        }

        // calculate based on a, b, c and d
        lElem[index++] = (byte) ((a << 2 | b >> 4) & 255);
        lElem[index++] = (byte) ((b << 4 | c >> 2) & 255);
        lElem[index++] = (byte) ((c << 6 | d) & 255);
      }

      if (a != -1 && b != -1) {
        if (c == -1 && s == '=') {
          lElem[index++] = (byte) ((a << 2 | b >> 4) & 255);
        } else if (c != -1 && s == '=') {
          lElem[index++] = (byte) ((a << 2 | b >> 4) & 255);
          lElem[index++] = (byte) ((b << 4 | c >> 2) & 255);
        }
      }
    }

    final Encoding encoding = Encoding.load("ASCII");
    final ByteList result = new ByteList(lElem, 0, index, encoding, false);
    setSourcePosition(frame, encode.position());

    return Layouts.STRING.createString(
        getContext().getCoreLibrary().getStringFactory(),
        StringOperations.ropeFromByteList(result, StringSupport.CR_UNKNOWN),
        null);
  }
Esempio n. 6
0
 @Override
 public Object isDefined(VirtualFrame frame) {
   return create7BitString(
       StringOperations.encodeByteList(Boolean.toString(value), UTF8Encoding.INSTANCE));
 }