Example #1
0
 @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());
 }
Example #2
0
 @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());
 }
Example #3
0
 @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;
 }
Example #4
0
    @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);
    }
Example #5
0
    @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)));
      }
    }
Example #6
0
 @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));
 }
Example #7
0
    @Specialization
    public Object match(RubyRegexp regexp, RubyString string) {
      notDesignedForCompilation();

      return regexp.matchCommon(string.getBytes(), true, false)
          != getContext().getCoreLibrary().getNilObject();
    }
Example #8
0
    @Specialization
    public RubyRegexp initialize(RubyRegexp regexp, RubyString string) {
      notDesignedForCompilation();

      regexp.initialize(this, string.getBytes());
      return regexp;
    }
Example #9
0
 @TruffleBoundary
 private DateTime localtime(long milliseconds, RubyString timeZone) {
   return new DateTime(
       milliseconds,
       org.jruby.RubyTime.getTimeZoneFromTZString(
           getContext().getRuntime(), timeZone.toString()));
 }
Example #10
0
    @Specialization
    public RubyString forceEncoding(RubyString string, RubyString encodingName) {
      RubyEncoding encoding = RubyEncoding.findEncodingByName(encodingName);
      string.forceEncoding(encoding.getRubyEncoding().getEncoding());

      return string;
    }
Example #11
0
    @Specialization
    public RubyString escape(RubyString pattern) {
      notDesignedForCompilation();

      return getContext()
          .makeString(
              org.jruby.RubyRegexp.quote19(new ByteList(pattern.getBytes()), true).toString());
    }
Example #12
0
 @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);
 }
Example #13
0
 @Specialization
 public RubyArray scan(RubyString string, RubyString regexpString) {
   final RubyRegexp regexp =
       new RubyRegexp(
           getContext().getCoreLibrary().getRegexpClass(),
           regexpString.toString(),
           Option.DEFAULT);
   return scan(string, regexp);
 }
Example #14
0
    @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());
    }
Example #15
0
    @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));
    }
Example #16
0
    @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));
    }
Example #17
0
    @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));
      }
    }
Example #18
0
 @Specialization
 public Object match(VirtualFrame frame, RubyString string, RubyRegexp regexp) {
   return regexp.match(frame.getCaller().unpack(), string.toString());
 }
Example #19
0
 @Specialization
 public RubyString reverse(RubyString string) {
   string.set(new StringBuilder(string.toString()).reverse().toString());
   return string;
 }
Example #20
0
 @Specialization
 public RubyString reverse(RubyString string) {
   return RubyString.fromJavaString(
       string.getRubyClass(), new StringBuilder(string.toString()).reverse().toString());
 }
Example #21
0
 @Specialization
 public RubySymbol toSym(RubyString string) {
   return getContext().newSymbol(string.toString());
 }
Example #22
0
 @Specialization
 public Object toI(RubyString string) {
   return string.toInteger();
 }
Example #23
0
 @Specialization
 public double toF(RubyString string) {
   return Double.parseDouble(string.toString());
 }
Example #24
0
 @Specialization
 public boolean endWith(RubyString string, RubyString b) {
   return string.toString().startsWith(b.toString());
 }
Example #25
0
 @Specialization
 public RubyArray split(RubyString string, RubyRegexp sep) {
   return RubyArray.specializedFromObjects(
       getContext().getCoreLibrary().getArrayClass(), sep.split(string.toString()));
 }
Example #26
0
 @Specialization
 public RubyString initialize(RubyString self, RubyString from) {
   self.set(from);
   return self;
 }
Example #27
0
 @Specialization
 public RubyString rjust(RubyString string, int length, RubyString padding) {
   return getContext()
       .makeString(RubyString.rjust(string.toString(), length, padding.toString()));
 }
Example #28
0
 @Specialization
 public RubyString rjust(
     RubyString string, int length, @SuppressWarnings("unused") UndefinedPlaceholder padding) {
   return getContext().makeString(RubyString.rjust(string.toString(), length, " "));
 }
Example #29
0
 @Specialization
 public int size(RubyString string) {
   return string.toString().length();
 }
Example #30
0
 @Specialization
 public RubyString slice(RubyString string, int start, int length) {
   return getContext().makeString(string.toString().substring(start, start + length));
 }