Beispiel #1
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;
 }
Beispiel #2
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());
    }
  }
Beispiel #3
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)");
    }
  }
  @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;
    }
  }
Beispiel #5
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 = "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;
 }
Beispiel #7
0
 public RubyValue clone() {
   //        RubyString s = (RubyString)super.clone();
   //        s.sb_ = new StringBuffer(sb_);
   //        return s;
   RubyString cl = new RubyString(this.sb_);
   cl.sb_ = new StringBuffer(sb_.toString());
   cl.doClone(this);
   return cl;
 }
Beispiel #8
0
  // @RubyLevelMethod(name="delete")
  public RubyValue delete(RubyArray args) {
    if (null == args) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "wrong number of arguments");
    }

    RubyString string = ObjectFactory.createString(toString());
    String arg = ((RubyString) args.get(0)).toString();
    string.delete(arg);
    return string;
  }
Beispiel #9
0
  // @RubyLevelMethod(name="end_with?")
  public RubyValue opEndWith(RubyValue v) {
    if (this == v) {
      return RubyConstant.QTRUE;
    }

    if (v instanceof RubyString) {
      RubyString str = ((RubyString) v);

      return this.sb_.toString().endsWith(str.toStr()) ? RubyConstant.QTRUE : RubyConstant.QFALSE;
    }

    return RubyConstant.QFALSE;
  }
Beispiel #10
0
  public int appendString2(RubyValue v) {
    RubyString str = null;
    if (v instanceof RubyString) {
      str = (RubyString) v;
    } else {
      RubyValue r = RubyAPI.callPublicNoArgMethod(v, null, RubyID.toSID);
      str = (RubyString) r;
    }

    appendString(str);

    return str.length();
  }
Beispiel #11
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);
  }
Beispiel #12
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);
  }
Beispiel #13
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;
 }
Beispiel #14
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;
  }
Beispiel #15
0
 // @RubyLevelMethod(name="unpack")
 public RubyValue unpack(RubyValue arg) {
   RubyString format = ((RubyString) arg);
   return ArrayPacker.unpack(toString(), format.toString());
 }
Beispiel #16
0
 // @RubyLevelMethod(name="chomp")
 public RubyValue chomp(RubyArray args) {
   RubyString string = ObjectFactory.createString(toString());
   RubyValue separator = (null != args) ? args.get(0) : GlobalVariables.get("$/");
   string.chomp(((RubyString) separator).toString());
   return string;
 }
Beispiel #17
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;
 }
Beispiel #18
0
 // @RubyLevelMethod(name="chop")
 public RubyValue chop() {
   RubyString rs = (RubyString) this.clone();
   rs.chopBang();
   return rs;
 }
Beispiel #19
0
 // @RubyLevelMethod(name="squeeze")
 public RubyValue squeeze(RubyArray args) {
   RubyString string = ObjectFactory.createString(toString());
   String arg = ((null == args) ? null : ((RubyString) args.get(0)).toString());
   string.squeeze(arg);
   return string;
 }
Beispiel #20
0
 // @RubyLevelMethod(name="reverse")
 public RubyValue reverse() {
   RubyString string = ObjectFactory.createString(toString());
   return string.reverse_danger();
 }