// @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()); } }
@Specialization public RubyRegexp initialize(RubyRegexp regexp, RubyRegexp from) { notDesignedForCompilation(); regexp.initialize(this, from.getSource()); // TODO: is copying needed? return regexp; }
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="split") public RubyValue split(RubyArray args) { RubyValue r = (null == args) ? GlobalVariables.get("$;") : args.get(0); Collection /*<String>*/ splitResult; boolean bSkipFirstEmptyItem = false; if (r == RubyConstant.QNIL) { splitResult = split(this, " "); } else if (r instanceof RubyRegexp) { RubyRegexp reg = (RubyRegexp) r; splitResult = split(this, reg, args); if (reg.getPattern().getPattern().startsWith("(?=")) bSkipFirstEmptyItem = true; } else if (r instanceof RubyString) { splitResult = split(this, ((RubyString) r).toString()); } else { throw new RubyException( RubyRuntime.ArgumentErrorClass, "wrong argument type " + r.getRubyClass() + " (expected Regexp)"); } RubyArray a = new RubyArray(splitResult.size()); int i = 0; // for (String str : splitResult) { for (Iterator iter = splitResult.iterator(); iter.hasNext(); ) { String str = (String) iter.next(); if (!(bSkipFirstEmptyItem && 0 == i && (str == null || str.equals("")))) { // To conform ruby's behavior, discard the first empty element a.add(ObjectFactory.createString(str)); } ++i; } return a; }
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()); } }
@Specialization public Object match(VirtualFrame frame, RubyString string, RubyString regexpString) { final RubyRegexp regexp = new RubyRegexp( getContext().getCoreLibrary().getRegexpClass(), regexpString.toString(), Option.DEFAULT); return regexp.match(frame.getCaller().unpack(), string.toString()); }
// @RubyLevelMethod(name="scan") public RubyValue scan(RubyValue arg, RubyBlock block) { RubyRegexp regex = (RubyRegexp) arg; if (null != block) { regex.scan(sb_.toString(), block); return this; } else { return regex.scan(sb_.toString()); } }
@Specialization public RubyString to_s(RubyRegexp regexp) { return new RubyString( getContext().getCoreLibrary().getStringClass(), ((org.jruby.RubyString) org.jruby.RubyRegexp.newRegexp( getContext().getRuntime(), regexp.getSource(), regexp.getRegex().getOptions()) .to_s()) .getByteList()); }
@Specialization public Object initializeCopy(RubyRegexp self, RubyRegexp from) { notDesignedForCompilation(); if (self == from) { return self; } self.initialize(this, from.getSource()); // TODO: is copying needed? return self; }
// @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); } }
private IRubyObject op_arefCommon(IRubyObject idx) { if (idx instanceof RubyFixnum) { int num = RubyNumeric.fix2int(idx); if (num >= 0) return RubyRegexp.nth_match(num, this); } else { if (idx instanceof RubySymbol) { return RubyRegexp.nth_match( nameToBackrefNumber((RubyString) ((RubySymbol) idx).id2name()), this); } else if (idx instanceof RubyString) { return RubyRegexp.nth_match(nameToBackrefNumber((RubyString) idx), this); } } return null; }
@Specialization public boolean equal(RubyRegexp a, RubyRegexp b) { notDesignedForCompilation(); return ((org.jruby.RubyString) org.jruby.RubyRegexp.newRegexp( getContext().getRuntime(), a.getSource(), a.getRegex().getOptions()) .to_s()) .getByteList() .equals( ((org.jruby.RubyString) org.jruby.RubyRegexp.newRegexp( getContext().getRuntime(), b.getSource(), b.getRegex().getOptions()) .to_s()) .getByteList()); }
@Specialization public RubyRegexp initialize(RubyRegexp regexp, RubyString string) { notDesignedForCompilation(); regexp.initialize(this, string.getBytes()); return regexp; }
@Specialization public Object match(RubyRegexp regexp, RubyString string) { notDesignedForCompilation(); return regexp.matchCommon(string.getBytes(), true, false) != getContext().getCoreLibrary().getNilObject(); }
/** match_aref */ @JRubyMethod(name = "[]") public IRubyObject op_aref(IRubyObject idx, IRubyObject rest) { if (!rest.isNil() || !(idx instanceof RubyFixnum) || ((RubyFixnum) idx).getLongValue() < 0) { return ((RubyArray) to_a()).aref(idx, rest); } return RubyRegexp.nth_match(RubyNumeric.fix2int(idx), this); }
/** match_to_s */ @JRubyMethod(name = "to_s") @Override public IRubyObject to_s() { check(); IRubyObject ss = RubyRegexp.last_match(this); if (ss.isNil()) ss = RubyString.newEmptyString(getRuntime()); if (isTaint()) ss.setTaint(true); return ss; }
// @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); } }
// @RubyLevelMethod(name="gsub") public RubyValue gsub(RubyArray args, RubyBlock block) { if (null == block) { return gsub(this, args); } 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 r.gsub(this, block); } }
// This returns a list of values in the order the names are defined (named capture local var // feature uses this). public IRubyObject[] getNamedBackrefValues(Ruby runtime) { if (pattern.numberOfNames() == 0) return NULL_ARRAY; IRubyObject[] values = new IRubyObject[pattern.numberOfNames()]; int j = 0; for (Iterator<NameEntry> i = pattern.namedBackrefIterator(); i.hasNext(); ) { NameEntry e = i.next(); int nth = pattern.nameToBackrefNumber(e.name, e.nameP, e.nameEnd, regs); values[j++] = RubyRegexp.nth_match(nth, this); } return values; }
// This returns a list of values in the order the names are defined (named capture local var // feature uses this). public IRubyObject[] getNamedBackrefValues(Ruby runtime) { if (pattern.numberOfNames() == 0) return NULL_ARRAY; IRubyObject[] values = new IRubyObject[pattern.numberOfNames()]; int j = 0; for (Iterator<NameEntry> i = pattern.namedBackrefIterator(); i.hasNext(); ) { NameEntry e = i.next(); int[] refs = e.getBackRefs(); int length = refs.length; values[j++] = length == 0 ? runtime.getNil() : RubyRegexp.nth_match(refs[length - 1], this); } return values; }
@JRubyMethod(name = "inspect") @Override public IRubyObject inspect() { if (str == null) return anyToString(); Ruby runtime = getRuntime(); RubyString result = runtime.newString(); result.cat((byte) '#').cat((byte) '<'); result.append(getMetaClass().getRealClass().to_s()); NameEntry[] names = new NameEntry[regs == null ? 1 : regs.numRegs]; if (pattern.numberOfNames() > 0) { for (Iterator<NameEntry> i = pattern.namedBackrefIterator(); i.hasNext(); ) { NameEntry e = i.next(); for (int num : e.getBackRefs()) names[num] = e; } } for (int i = 0; i < names.length; i++) { result.cat((byte) ' '); if (i > 0) { NameEntry e = names[i]; if (e != null) { result.cat(e.name, e.nameP, e.nameEnd - e.nameP); } else { result.cat((byte) ('0' + i)); } result.cat((byte) ':'); } IRubyObject v = RubyRegexp.nth_match(i, this); if (v.isNil()) { result.cat("nil".getBytes()); } else { result.append(((RubyString) v).inspectCommon(runtime.is1_9())); } } return result.cat((byte) '>'); }
@Specialization public RubyArray scan(RubyString string, RubyRegexp regexp) { return RubyArray.specializedFromObjects( getContext().getCoreLibrary().getArrayClass(), regexp.scan(string)); }
@JRubyMethod(name = "names", compat = CompatVersion.RUBY1_9) public IRubyObject names(ThreadContext context, Block block) { check(); checkLazyRegexp(); return regexp.names(context); }
public IRubyObject group(int n) { return RubyRegexp.nth_match(n, this); }
public IRubyObject group(long n) { return RubyRegexp.nth_match((int) n, this); }
private void checkLazyRegexp() { if (regexp == null) regexp = RubyRegexp.newRegexp(getRuntime(), (ByteList) pattern.getUserObject(), pattern); }
@JRubyMethod public IRubyObject names(ThreadContext context, Block block) { check(); checkLazyRegexp(); return regexp.names(context); }
@Override public IRubyObject get() { return RubyRegexp.match_last( runtime.getCurrentContext().getCurrentScope().getBackRef(runtime)); }
public static void createGlobals(ThreadContext context, Ruby runtime) { runtime.defineGlobalConstant("TOPLEVEL_BINDING", runtime.newBinding()); runtime.defineGlobalConstant("TRUE", runtime.getTrue()); runtime.defineGlobalConstant("FALSE", runtime.getFalse()); runtime.defineGlobalConstant("NIL", runtime.getNil()); // define ARGV and $* for this runtime RubyArray argvArray = runtime.newArray(); String[] argv = runtime.getInstanceConfig().getArgv(); for (int i = 0; i < argv.length; i++) { argvArray.append(RubyString.newStringShared(runtime, argv[i].getBytes())); } runtime.defineGlobalConstant("ARGV", argvArray); runtime.getGlobalVariables().defineReadonly("$*", new ValueAccessor(argvArray)); IAccessor d = new ValueAccessor(runtime.newString(runtime.getInstanceConfig().displayedFileName())); runtime.getGlobalVariables().define("$PROGRAM_NAME", d); runtime.getGlobalVariables().define("$0", d); // Version information: IRubyObject version = null; IRubyObject patchlevel = null; IRubyObject release = runtime.newString(Constants.COMPILE_DATE).freeze(context); IRubyObject platform = runtime.newString(Constants.PLATFORM).freeze(context); IRubyObject engine = runtime.newString(Constants.ENGINE).freeze(context); switch (runtime.getInstanceConfig().getCompatVersion()) { case RUBY1_8: version = runtime.newString(Constants.RUBY_VERSION).freeze(context); patchlevel = runtime.newFixnum(Constants.RUBY_PATCHLEVEL).freeze(context); break; case RUBY1_9: version = runtime.newString(Constants.RUBY1_9_VERSION).freeze(context); patchlevel = runtime.newFixnum(Constants.RUBY1_9_PATCHLEVEL).freeze(context); break; } runtime.defineGlobalConstant("RUBY_VERSION", version); runtime.defineGlobalConstant("RUBY_PATCHLEVEL", patchlevel); runtime.defineGlobalConstant("RUBY_RELEASE_DATE", release); runtime.defineGlobalConstant("RUBY_PLATFORM", platform); runtime.defineGlobalConstant("RUBY_ENGINE", engine); IRubyObject description = runtime.newString(runtime.getInstanceConfig().getVersionString()).freeze(context); runtime.defineGlobalConstant("RUBY_DESCRIPTION", description); IRubyObject copyright = runtime.newString(runtime.getInstanceConfig().getCopyrightString()).freeze(context); runtime.defineGlobalConstant("RUBY_COPYRIGHT", copyright); runtime.defineGlobalConstant("VERSION", version); runtime.defineGlobalConstant("RELEASE_DATE", release); runtime.defineGlobalConstant("PLATFORM", platform); IRubyObject jrubyVersion = runtime.newString(Constants.VERSION).freeze(context); IRubyObject jrubyRevision = runtime.newString(Constants.REVISION).freeze(context); runtime.defineGlobalConstant("JRUBY_VERSION", jrubyVersion); runtime.defineGlobalConstant("JRUBY_REVISION", jrubyRevision); if (runtime.is1_9()) { // needs to be a fixnum, but our revision is a sha1 hash from git runtime.defineGlobalConstant("RUBY_REVISION", runtime.newFixnum(Constants.RUBY1_9_REVISION)); } GlobalVariable kcodeGV = new KCodeGlobalVariable(runtime, "$KCODE", runtime.newString("NONE")); runtime.defineVariable(kcodeGV); runtime.defineVariable(new GlobalVariable.Copy(runtime, "$-K", kcodeGV)); IRubyObject defaultRS = runtime.newString(runtime.getInstanceConfig().getRecordSeparator()).freeze(context); GlobalVariable rs = new StringGlobalVariable(runtime, "$/", defaultRS); runtime.defineVariable(rs); runtime.setRecordSeparatorVar(rs); runtime.getGlobalVariables().setDefaultSeparator(defaultRS); runtime.defineVariable(new StringGlobalVariable(runtime, "$\\", runtime.getNil())); runtime.defineVariable(new StringGlobalVariable(runtime, "$,", runtime.getNil())); runtime.defineVariable(new LineNumberGlobalVariable(runtime, "$.")); runtime.defineVariable(new LastlineGlobalVariable(runtime, "$_")); runtime.defineVariable(new LastExitStatusVariable(runtime, "$?")); runtime.defineVariable(new ErrorInfoGlobalVariable(runtime, "$!", runtime.getNil())); runtime.defineVariable(new NonEffectiveGlobalVariable(runtime, "$=", runtime.getFalse())); if (runtime.getInstanceConfig().getInputFieldSeparator() == null) { runtime.defineVariable(new GlobalVariable(runtime, "$;", runtime.getNil())); } else { runtime.defineVariable( new GlobalVariable( runtime, "$;", RubyRegexp.newRegexp( runtime, runtime.getInstanceConfig().getInputFieldSeparator(), 0))); } Boolean verbose = runtime.getInstanceConfig().getVerbose(); IRubyObject verboseValue = null; if (verbose == null) { verboseValue = runtime.getNil(); } else if (verbose == Boolean.TRUE) { verboseValue = runtime.getTrue(); } else { verboseValue = runtime.getFalse(); } runtime.defineVariable(new VerboseGlobalVariable(runtime, "$VERBOSE", verboseValue)); IRubyObject debug = runtime.newBoolean(runtime.getInstanceConfig().isDebug()); runtime.defineVariable(new DebugGlobalVariable(runtime, "$DEBUG", debug)); runtime.defineVariable(new DebugGlobalVariable(runtime, "$-d", debug)); runtime.defineVariable(new SafeGlobalVariable(runtime, "$SAFE")); runtime.defineVariable(new BacktraceGlobalVariable(runtime, "$@")); IRubyObject stdin = new RubyIO(runtime, STDIO.IN); IRubyObject stdout = new RubyIO(runtime, STDIO.OUT); IRubyObject stderr = new RubyIO(runtime, STDIO.ERR); runtime.defineVariable(new InputGlobalVariable(runtime, "$stdin", stdin)); runtime.defineVariable(new OutputGlobalVariable(runtime, "$stdout", stdout)); runtime.getGlobalVariables().alias("$>", "$stdout"); runtime.getGlobalVariables().alias("$defout", "$stdout"); runtime.defineVariable(new OutputGlobalVariable(runtime, "$stderr", stderr)); runtime.getGlobalVariables().alias("$deferr", "$stderr"); runtime.defineGlobalConstant("STDIN", stdin); runtime.defineGlobalConstant("STDOUT", stdout); runtime.defineGlobalConstant("STDERR", stderr); runtime.defineVariable(new LoadedFeatures(runtime, "$\"")); runtime.defineVariable(new LoadedFeatures(runtime, "$LOADED_FEATURES")); runtime.defineVariable(new LoadPath(runtime, "$:")); runtime.defineVariable(new LoadPath(runtime, "$-I")); runtime.defineVariable(new LoadPath(runtime, "$LOAD_PATH")); runtime.defineVariable(new MatchMatchGlobalVariable(runtime, "$&")); runtime.defineVariable(new PreMatchGlobalVariable(runtime, "$`")); runtime.defineVariable(new PostMatchGlobalVariable(runtime, "$'")); runtime.defineVariable(new LastMatchGlobalVariable(runtime, "$+")); runtime.defineVariable(new BackRefGlobalVariable(runtime, "$~")); // On platforms without a c-library accessable through JNA, getpid will return hashCode // as $$ used to. Using $$ to kill processes could take down many runtimes, but by basing // $$ on getpid() where available, we have the same semantics as MRI. runtime.getGlobalVariables().defineReadonly("$$", new PidAccessor(runtime)); // after defn of $stderr as the call may produce warnings defineGlobalEnvConstants(runtime); // Fixme: Do we need the check or does Main.java not call this...they should consolidate if (runtime.getGlobalVariables().get("$*").isNil()) { runtime.getGlobalVariables().defineReadonly("$*", new ValueAccessor(runtime.newArray())); } runtime .getGlobalVariables() .defineReadonly( "$-p", new ValueAccessor(runtime.newBoolean(runtime.getInstanceConfig().isAssumePrinting()))); runtime .getGlobalVariables() .defineReadonly( "$-a", new ValueAccessor(runtime.newBoolean(runtime.getInstanceConfig().isSplit()))); runtime .getGlobalVariables() .defineReadonly( "$-l", new ValueAccessor(runtime.newBoolean(runtime.getInstanceConfig().isProcessLineEnds()))); // ARGF, $< object RubyArgsFile.initArgsFile(runtime); }
@Specialization public RubyArray split(RubyString string, RubyRegexp sep) { return RubyArray.specializedFromObjects( getContext().getCoreLibrary().getArrayClass(), sep.split(string.toString())); }