예제 #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));
 }