Esempio n. 1
0
 // @RubyLevelMethod(name="tr_s")
 public RubyValue tr_s(RubyValue arg1, RubyValue arg2) {
   RubyString string = ObjectFactory.createString(toString());
   RubyString from = (RubyString) arg1;
   RubyString to = (RubyString) arg2;
   string.tr_s(from.toString(), to.toString());
   return string;
 }
Esempio n. 2
0
  private RubyString sub(RubyString g, RubyArray args) {
    if (null == args || args.size() != 2) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `sub': wrong number of arguments (" + actual_argc + " for 2)");
    }

    if (!(args.get(1) instanceof RubyString)) {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "can't convert " + args.get(1).getRubyClass().getName() + " into String");
    }

    RubyString s = (RubyString) args.get(1);

    if (args.get(0) instanceof RubyRegexp) {
      RubyRegexp r = (RubyRegexp) args.get(0);
      return r.sub(g, s);
    } else if (args.get(0) instanceof RubyString) {
      RubyString r = (RubyString) args.get(0);
      String result = StringMe.replaceFirst(g.toString(), r.toString(), s.toString());
      return ObjectFactory.createString(result);
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
    }
  }
Esempio n. 3
0
 // @RubyLevelMethod(name="tr_s!")
 public RubyValue trs_danger(RubyValue arg1, RubyValue arg2) {
   RubyString from = (RubyString) arg1;
   RubyString to = (RubyString) arg2;
   if (tr_s(from.toString(), to.toString())) return this;
   return RubyConstant.QNIL;
   //        return tr_s(from.toString(), to.toString()) ? this : RubyConstant.QNIL;
 }
Esempio n. 4
0
 private Collection /*<String>*/ split(RubyString g, RubyRegexp r, RubyArray args) {
   if (args.size() <= 1) {
     return r.split(g.toString(), 0);
   } else {
     RubyFixnum i = (RubyFixnum) args.get(1);
     return r.split(g.toString(), i.toInt());
   }
 }
  @RubyLevelMethod(name = "open")
  public static RubyValue open(RubyValue receiver, RubyArray args, RubyBlock block) {
    String filename = args.get(0).toStr();
    RubyIO io;
    if (args.size() <= 1) {
      io = ObjectFactory.createFile(filename, "r");
    } else if (args.get(1) instanceof RubyFixnum) {
      String mode = "r";
      int i = args.get(1).toInt();
      if ((i & RDWR) != 0) {
        mode = mode + "w";
      }
      io = ObjectFactory.createFile(filename, mode);
    } else {
      RubyString mode = (RubyString) args.get(1);
      io = ObjectFactory.createFile(filename, mode.toString());
    }

    if (null == block) {
      return io;
    } else {
      RubyValue v = block.invoke(receiver, io);
      io.close();
      return v;
    }
  }
Esempio n. 6
0
  // @RubyLevelMethod(name="gsub!")
  public RubyValue gsub_danger(RubyArray args, RubyBlock block) {
    if (null == block) {
      RubyString result = gsub(this, args);

      if (result == this) {
        return RubyConstant.QNIL;
      } else {
        return setString(result.toString());
      }
    } else {
      if (null == args || args.size() != 1) {
        int actual_argc = (null == args) ? 0 : args.size();
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "in `gsub!': wrong number of arguments (" + actual_argc + " for 1)");
      }
      if (!(args.get(0) instanceof RubyRegexp)) {
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
      }

      RubyRegexp r = (RubyRegexp) args.get(0);
      return setString(r.gsub(this, block).toString());
    }
  }
 @RubyLevelMethod(name = "p", module = true)
 public static RubyValue p(RubyValue receiver, RubyValue arg) {
   RubyValue str = RubyAPI.callNoArgMethod(arg, null, RubyID.inspectID);
   RubyString value = str.toRubyString();
   value.appendString("\n");
   System.out.print(value.toString());
   return RubyConstant.QNIL;
 }
Esempio n. 8
0
  private Collection /*<String>*/ split(RubyString s, String delimiter) {
    StringParser t = new StringParser(s.toString(), delimiter);
    //        int total = t.countTokens();
    // Collection/*<String>*/ r = new ArrayList/*<String>*/(total);
    //        for (int i = 0; i < total; ++i) {
    //            r.add(t.nextToken());
    //        }

    Collection /*<String>*/ r = new ArrayList /*<String>*/(0);
    while (t.hasMoreElements()) {
      r.add(t.nextElement());
    }
    return r;
  }
Esempio n. 9
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);
  }
Esempio n. 10
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);
  }
Esempio n. 11
0
 // @RubyLevelMethod(name="unpack")
 public RubyValue unpack(RubyValue arg) {
   RubyString format = ((RubyString) arg);
   return ArrayPacker.unpack(toString(), format.toString());
 }
Esempio n. 12
0
 // @RubyLevelMethod(name="tr")
 public RubyValue tr(RubyValue arg1, RubyValue arg2) {
   RubyString from = (RubyString) arg1;
   RubyString to = (RubyString) arg2;
   tr(from.toString(), to.toString());
   return this;
 }