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="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; }
// @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()); } }
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()); } }
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; }
// @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; }
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(); }
// @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; }
// @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); }
// @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); }
// @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; }
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; }
// @RubyLevelMethod(name="chop") public RubyValue chop() { RubyString rs = (RubyString) this.clone(); rs.chopBang(); return rs; }
// @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; }
// @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; }
// @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; }
// @RubyLevelMethod(name="unpack") public RubyValue unpack(RubyValue arg) { RubyString format = ((RubyString) arg); return ArrayPacker.unpack(toString(), format.toString()); }
// @RubyLevelMethod(name="reverse") public RubyValue reverse() { RubyString string = ObjectFactory.createString(toString()); return string.reverse_danger(); }