Ejemplo n.º 1
0
  // @RubyLevelMethod(name="hex")
  public RubyValue hex() {
    String s = toString();
    if (s.startsWith("0x")) {
      s = s.substring("0x".length());
    }

    try {
      return ObjectFactory.createFixnum(Long.parseLong(s, 16));
    } catch (NumberFormatException e) {
      return ObjectFactory.FIXNUM0;
    }
  }
Ejemplo n.º 2
0
 // @RubyLevelMethod(name="=~")
 public RubyValue operator_match(RubyValue arg) {
   if (arg instanceof RubyRegexp) {
     RubyRegexp reg = (RubyRegexp) arg;
     int p = reg.matchPosition(toString());
     if (p >= 0) {
       return ObjectFactory.createFixnum(p);
     } else {
       return RubyConstant.QNIL;
     }
   } else {
     return RubyAPI.callPublicOneArgMethod(arg, this, null, RubyID.matchID);
   }
 }
Ejemplo n.º 3
0
  // @RubyLevelMethod(name="casecmp")
  RubyValue run(RubyValue arg) {
    if (!(arg instanceof RubyString)) {
      return RubyConstant.QNIL;
    }

    RubyString value2 = (RubyString) arg;
    int compare = toString().toUpperCase().compareTo(value2.toString().toUpperCase());
    if (compare > 0) {
      compare = 1;
    } else if (compare < 0) {
      compare = -1;
    }
    return ObjectFactory.createFixnum(compare);
  }
Ejemplo n.º 4
0
  // @RubyLevelMethod(name="count")
  public RubyValue count(RubyArray args) {
    if (null == args) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "wrong number of arguments");
    }

    // TODO incomplete
    int n = 0;
    //        for (RubyValue v : args) {
    for (Iterator iter = args.iterator(); iter.hasNext(); ) {
      RubyValue v = (RubyValue) iter.next();
      RubyString other_str = (RubyString) v;
      n += count(other_str.toString());
    }
    return ObjectFactory.createFixnum(n);
  }
Ejemplo n.º 5
0
  // @RubyLevelMethod(name="each_byte")
  public RubyValue each_byte(RubyBlock block) {
    if (block == null) return this;

    String string = toString();
    byte bytes[] = null;
    try {
      bytes = string.getBytes("UTF-8");
    } catch (java.io.UnsupportedEncodingException exc) {
    }

    for (int i = 0; bytes != null && i < bytes.length; ++i) {
      int nByte = bytes[i];
      if (nByte < 0) nByte = 256 + nByte;

      block.invoke(this, ObjectFactory.createFixnum(nByte));
    }

    return this;
  }
Ejemplo n.º 6
0
  // @RubyLevelMethod(name="[]")
  public RubyValue array_access(RubyArray args) {
    String string = toString();
    if (args.size() == 1) {
      RubyValue arg = args.get(0);
      if (arg instanceof RubyString) {
        String str = ((RubyString) arg).toString();
        if (string.indexOf(str) >= 0) {
          return ObjectFactory.createString(str);
        } else {
          return RubyConstant.QNIL;
        }
      } else if (arg instanceof RubyRange) {
        RubyRange range = (RubyRange) arg;
        int start = range.getLeft().toInt();
        int end = range.getRight().toInt();
        return substring(string, start, end, range.isExcludeEnd());
      } else if (arg instanceof RubyRegexp) {
        RubyRegexp regexp = (RubyRegexp) arg;
        RubyMatchData match = regexp.match(string);
        if (match != null) {
          return ObjectFactory.createString(match.toString());
        } else {
          return RubyConstant.QNIL;
        }
      } else {
        int index = arg.toInt();
        if (index < 0) {
          index = string.length() + index;
        }

        if (index < 0 || index >= string.length()) {
          return RubyConstant.QNIL;
        } else {
          return ObjectFactory.createFixnum(string.charAt(index));
        }
      }
    } else {
      int start = args.get(0).toInt();
      int length = args.get(1).toInt() - 1;

      return substring(string, start, start + length, false);
    }
  }
Ejemplo n.º 7
0
  // @RubyLevelMethod(name="ord")
  public RubyValue ord() {
    if (sb_ == null || sb_.length() == 0) return ObjectFactory.createFixnum(0);

    return ObjectFactory.createFixnum(sb_.charAt(0));
  }
Ejemplo n.º 8
0
 // @RubyLevelMethod(name="bytesize")
 public RubyFixnum rubyBytesize() {
   return ObjectFactory.createFixnum(sb_.length());
 }