Пример #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());
 }
Пример #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());
 }
Пример #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;
 }
Пример #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);
    }
Пример #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)));
      }
    }
Пример #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));
 }
Пример #7
0
    @Specialization
    public Object match(RubyRegexp regexp, RubyString string) {
      notDesignedForCompilation();

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

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

      return string;
    }
Пример #11
0
    @Specialization
    public RubyString escape(RubyString pattern) {
      notDesignedForCompilation();

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