@Specialization public RubyArray unpack(RubyString string, RubyString format) { final org.jruby.RubyArray jrubyArray = Pack.unpack(getContext().getRuntime(), string.getBytes(), format.getBytes()); return RubyArray.specializedFromObjects( getContext().getCoreLibrary().getArrayClass(), jrubyArray.toArray()); }
@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()); }
@Specialization public RubyString add(RubyString a, RubyString b) { // TODO(CS): which encoding do we get here? final RubyString string = new RubyString(getContext().getCoreLibrary().getStringClass(), new ByteList()); string.getBytes().append(a.getBytes()); string.getBytes().append(b.getBytes()); return string; }
@Specialization public RubyArray split(RubyString string, RubyString sep) { final String[] components = string.toString().split(Pattern.quote(sep.toString())); final Object[] objects = new Object[components.length]; for (int n = 0; n < objects.length; n++) { objects[n] = getContext().makeString(components[n]); } return RubyArray.specializedFromObjects( getContext().getCoreLibrary().getArrayClass(), objects); }
@CompilerDirectives.SlowPath private RubyString formatSlow(RubyString format, Object[] args) { final RubyContext context = getContext(); if (args.length == 1 && args[0] instanceof RubyArray) { singleArrayProfile.enter(); return context.makeString( StringFormatter.format(format.toString(), ((RubyArray) args[0]).asList())); } else { multipleArgumentsProfile.enter(); return context.makeString(StringFormatter.format(format.toString(), Arrays.asList(args))); } }
@CompilerDirectives.SlowPath @Specialization(order = 1) public Object getIndex(RubyString string, int index, UndefinedPlaceholder undefined) { final String javaString = string.toString(); final int normalisedIndex = ArrayUtilities.normaliseIndex(javaString.length(), index); return getContext().makeString(javaString.charAt(normalisedIndex)); }
@Specialization public Object match(RubyRegexp regexp, RubyString string) { notDesignedForCompilation(); return regexp.matchCommon(string.getBytes(), true, false) != getContext().getCoreLibrary().getNilObject(); }
@Specialization public RubyRegexp initialize(RubyRegexp regexp, RubyString string) { notDesignedForCompilation(); regexp.initialize(this, string.getBytes()); return regexp; }
@TruffleBoundary private DateTime localtime(long milliseconds, RubyString timeZone) { return new DateTime( milliseconds, org.jruby.RubyTime.getTimeZoneFromTZString( getContext().getRuntime(), timeZone.toString())); }
@Specialization public RubyString forceEncoding(RubyString string, RubyString encodingName) { RubyEncoding encoding = RubyEncoding.findEncodingByName(encodingName); string.forceEncoding(encoding.getRubyEncoding().getEncoding()); return string; }
@Specialization public RubyString escape(RubyString pattern) { notDesignedForCompilation(); return getContext() .makeString( org.jruby.RubyRegexp.quote19(new ByteList(pattern.getBytes()), true).toString()); }
@Specialization public RubyString gsub(RubyString string, RubyString regexpString, RubyString replacement) { final RubyRegexp regexp = new RubyRegexp( getContext().getCoreLibrary().getRegexpClass(), regexpString.toString(), Option.DEFAULT); return gsub(string, regexp, replacement); }
@Specialization public RubyArray scan(RubyString string, RubyString regexpString) { final RubyRegexp regexp = new RubyRegexp( getContext().getCoreLibrary().getRegexpClass(), regexpString.toString(), Option.DEFAULT); return scan(string, regexp); }
@CompilerDirectives.SlowPath @Specialization public RubyString add(RubyString string, int times) { final StringBuilder builder = new StringBuilder(); for (int n = 0; n < times; n++) { builder.append(string.toString()); } return getContext().makeString(builder.toString()); }
@Specialization public RubyArray chomp(RubyString string) { final byte[] bytes = string.getBytes().bytes(); final int[] ints = new int[bytes.length]; for (int n = 0; n < ints.length; n++) { ints[n] = RubyFixnum.toUnsignedInt(bytes[n]); } return new RubyArray( getContext().getCoreLibrary().getArrayClass(), new IntegerArrayStore(ints)); }
@CompilerDirectives.SlowPath @Specialization(order = 3) public Object getIndex(RubyString string, int start, int length) { final String javaString = string.toString(); if (length > javaString.length() - start) { length = javaString.length() - start; } if (start > javaString.length()) { return NilPlaceholder.INSTANCE; } return getContext().makeString(javaString.substring(start, start + length)); }
@CompilerDirectives.SlowPath @Specialization(order = 2) public Object getIndex(RubyString string, FixnumRange range, UndefinedPlaceholder undefined) { final String javaString = string.toString(); final int stringLength = javaString.length(); if (range.doesExcludeEnd()) { final int begin = ArrayUtilities.normaliseIndex(stringLength, range.getBegin()); final int exclusiveEnd = ArrayUtilities.normaliseExclusiveIndex(stringLength, range.getExclusiveEnd()); return getContext().makeString(javaString.substring(begin, exclusiveEnd)); } else { final int begin = ArrayUtilities.normaliseIndex(stringLength, range.getBegin()); final int inclusiveEnd = ArrayUtilities.normaliseIndex(stringLength, range.getInclusiveEnd()); return getContext().makeString(javaString.substring(begin, inclusiveEnd + 1)); } }
@Specialization public Object match(VirtualFrame frame, RubyString string, RubyRegexp regexp) { return regexp.match(frame.getCaller().unpack(), string.toString()); }
@Specialization public RubyString reverse(RubyString string) { string.set(new StringBuilder(string.toString()).reverse().toString()); return string; }
@Specialization public RubyString reverse(RubyString string) { return RubyString.fromJavaString( string.getRubyClass(), new StringBuilder(string.toString()).reverse().toString()); }
@Specialization public RubySymbol toSym(RubyString string) { return getContext().newSymbol(string.toString()); }
@Specialization public Object toI(RubyString string) { return string.toInteger(); }
@Specialization public double toF(RubyString string) { return Double.parseDouble(string.toString()); }
@Specialization public boolean endWith(RubyString string, RubyString b) { return string.toString().startsWith(b.toString()); }
@Specialization public RubyArray split(RubyString string, RubyRegexp sep) { return RubyArray.specializedFromObjects( getContext().getCoreLibrary().getArrayClass(), sep.split(string.toString())); }
@Specialization public RubyString initialize(RubyString self, RubyString from) { self.set(from); return self; }
@Specialization public RubyString rjust(RubyString string, int length, RubyString padding) { return getContext() .makeString(RubyString.rjust(string.toString(), length, padding.toString())); }
@Specialization public RubyString rjust( RubyString string, int length, @SuppressWarnings("unused") UndefinedPlaceholder padding) { return getContext().makeString(RubyString.rjust(string.toString(), length, " ")); }
@Specialization public int size(RubyString string) { return string.toString().length(); }
@Specialization public RubyString slice(RubyString string, int start, int length) { return getContext().makeString(string.toString().substring(start, start + length)); }