Esempio n. 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());
 }
Esempio n. 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());
 }
Esempio n. 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;
 }
Esempio n. 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);
    }
Esempio n. 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)));
      }
    }
Esempio n. 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));
 }
Esempio n. 7
0
    @Specialization
    public Object match(RubyRegexp regexp, RubyString string) {
      notDesignedForCompilation();

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

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

      return string;
    }
Esempio n. 11
0
    @Specialization
    public RubyString escape(RubyString pattern) {
      notDesignedForCompilation();

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