Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 40 additions & 22 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -3800,7 +3800,8 @@ private IRubyObject byteSubstr(ThreadContext context, long beg, long len) {
if (beg < 0) return context.nil;
}

if (beg + len > length) len = length - beg;
// avoids beg + len overflow
if (len > length - beg) len = length - beg;
if (len <= 0) len = 0;

// above boundary checks confirms we can safely cast to int for beg + len.
Expand All @@ -3809,28 +3810,26 @@ private IRubyObject byteSubstr(ThreadContext context, long beg, long len) {

@CRuby(value = "str_byte_aref", file = "string.c")
private IRubyObject byteARef(ThreadContext context, IRubyObject idx) {
final int index;
final long index;

if (idx instanceof RubyRange range){
int[] begLen = range.begLenInt(context, getByteList().length(), 0);
long[] begLen = range.begLen(context, getByteList().length(), 0);
return begLen == null ? context.nil : byteSubstr(context, begLen[0], begLen[1]);
} else if (idx instanceof RubyFixnum fixnum) {
long i = fixnum.getValue();
if (i > Integer.MAX_VALUE || i < Integer.MIN_VALUE) return context.nil;
index = (int) i;
index = fixnum.getValue();
} else {
StringSites sites = sites(context);
if (RubyRange.isRangeLike(context, idx, sites.respond_to_begin, sites.respond_to_end)) {
RubyRange range = RubyRange.rangeFromRangeLike(context, idx, sites.begin, sites.end, sites.exclude_end);

int[] begLen = range.begLenInt(context, getByteList().length(), 0);
long[] begLen = range.begLen(context, getByteList().length(), 0);
return begLen == null ? context.nil : byteSubstr(context, begLen[0], begLen[1]);
} else {
index = toInt(context, idx);
index = toLong(context, idx);
}
}

IRubyObject obj = byteSubstr(context, index, 1);
IRubyObject obj = byteSubstr(context, index, 1L);
if (obj.isNil() || ((RubyString)obj).getByteList().length() == 0) return context.nil;
return obj;
}
Expand All @@ -3857,11 +3856,13 @@ public final IRubyObject substrEnc(ThreadContext context, int beg, int len) {
beg += length;
if (beg < 0) return context.nil;
}
if (beg + len > length) len = length - beg;
// avoids beg + len overflow
if (len > length - beg) len = length - beg;
if (len <= 0) len = beg = 0;
return makeSharedString(context.runtime, beg, len);
} else {
if (beg + len > length) len = length - beg;
// beg may still be negative here, so do the sum and the difference in long
if ((long) beg + len > length) len = (int) Math.min(Integer.MAX_VALUE, (long) length - beg);
return multibyteSubstr(context.runtime, enc, len, beg, length);
}
}
Expand Down Expand Up @@ -3964,31 +3965,45 @@ private void replaceInternal19(Ruby runtime, int beg, int len, RubyString repl)
@JRubyMethod(name = {"[]", "slice"}, writes = BACKREF)
public IRubyObject op_aref(ThreadContext context, IRubyObject arg) {
if (arg instanceof RubyFixnum fixnum) {
return op_aref(context, fixnum.asInt(context));
return op_aref(context, fixnum.getValue());
} else if (arg instanceof RubyRegexp regexp) {
return subpat(context, regexp);
} else if (arg instanceof RubyString str) {
return StringSupport.index(this, str, 0, this.checkEncoding(str)) != -1 ? dupString(context, str) : context.nil;
} else if (arg instanceof RubyRange range) {
int[] begLen = range.begLenInt(context, strLength(), 0);
return begLen == null ? context.nil : substrEnc(context, begLen[0], begLen[1]);
return substrEnc(context, range);
} else {
StringSites sites = sites(context);
if (RubyRange.isRangeLike(context, arg, sites.respond_to_begin, sites.respond_to_end)) {
int len = strLength();
RubyRange range = RubyRange.rangeFromRangeLike(context, arg, sites.begin, sites.end, sites.exclude_end);

int[] begLen = range.begLenInt(context, len, 0);
return begLen == null ? context.nil : substrEnc(context, begLen[0], begLen[1]);
return substrEnc(context, range);
}
}
return op_aref(context, toInt(context, arg));
return op_aref(context, toLong(context, arg));
}

@JRubyMethod(name = {"[]", "slice"}, writes = BACKREF)
public IRubyObject op_aref(ThreadContext context, IRubyObject arg1, IRubyObject arg2) {
return arg1 instanceof RubyRegexp regexp ?
subpat(context, regexp, arg2) : substrEnc(context, toInt(context, arg1), toInt(context, arg2));
subpat(context, regexp, arg2) : substrEnc(context, toLong(context, arg1), toLong(context, arg2));
}

private IRubyObject substrEnc(ThreadContext context, RubyRange range) {
// with err 0 the range is clamped to the string length, so both values fit in an int
long[] begLen = range.begLen(context, strLength(), 0);
return begLen == null ? context.nil : substrEnc(context, (int) begLen[0], (int) begLen[1]);
}

/**
* Like rb_str_substr, take a long start and length. A start beyond the byte length is out of range for any
* encoding, and no substring is longer than the byte length, so bounding both by it lets them fit in the ints
* {@link #substrEnc(ThreadContext, int, int)} takes.
*/
@CRuby(value = "rb_str_substr", file = "string.c", note = "long index and length bounds")
private IRubyObject substrEnc(ThreadContext context, long beg, long len) {
int length = value.getRealSize();
if (len < 0 || beg > length || beg < -length) return context.nil;
return substrEnc(context, (int) beg, (int) Math.min(len, length));
}

@JRubyMethod
Expand Down Expand Up @@ -4190,8 +4205,11 @@ private void strUpdate1(int beg, int len, RubyString val, int vbeg, int vlen) {
setCodeRange(cr);
}

private IRubyObject op_aref(ThreadContext context, int idx) {
IRubyObject str = substrEnc(context, idx, 1);
private IRubyObject op_aref(ThreadContext context, long idx) {
// the bound from substrEnc(long, long), inlined to keep this hot path one call shallower
int length = value.getRealSize();
if (idx > length || idx < -length) return context.nil;
IRubyObject str = substrEnc(context, (int) idx, 1);
return !str.isNil() && ((RubyString) str).value.getRealSize() == 0 ? context.nil : str;
}

Expand Down
8 changes: 8 additions & 0 deletions spec/ruby/core/string/element_reference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
it_behaves_like :string_slice_index_length, :[]
end

describe "String#[] with index, length on a multibyte String" do
it "counts characters for an index or length outside of int range" do
"héllo"[fixnum_max].should == nil
"héllo"[1, fixnum_max].should == "éllo"
"héllo"[-1, fixnum_max].should == "o"
end
end

describe "String#[] with Range" do
it_behaves_like :string_slice_range, :[]
end
Expand Down
21 changes: 21 additions & 0 deletions spec/ruby/core/string/shared/slice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@

"".send(@method, 0).should == nil
"".send(@method, -1).should == nil

"hello".send(@method, fixnum_max).should == nil
"hello".send(@method, fixnum_min).should == nil
end

platform_is c_long_size: 64 do
it "returns nil if an index converted with to_int is outside of self" do
"hello".send(@method, 2.0 ** 40).should == nil

obj = mock('2**40')
obj.should_receive(:to_int).and_return(1 << 40)
"hello".send(@method, obj).should == nil
end
end

it "calls to_int on the given index" do
Expand Down Expand Up @@ -66,6 +79,9 @@
"foo".send(@method, 3,0).should == ""
"foo".send(@method, 3,1).should == ""

"hello".send(@method, 1, (1 << 31) - 1).should == "ello"
"hello".send(@method, 1, fixnum_max).should == "ello"

"".send(@method, 0,0).should == ""
"".send(@method, 0,1).should == ""

Expand Down Expand Up @@ -210,6 +226,9 @@

"x".send(@method, 1..1).should == ""
"x".send(@method, 1..-1).should == ""

"hello".send(@method, 0..fixnum_max).should == "hello"
"hello".send(@method, 1...fixnum_max).should == "ello"
end

it "returns a String in the same encoding as self" do
Expand All @@ -222,6 +241,8 @@
"hello there".send(@method, 20..1).should == nil
"hello there".send(@method, -20..1).should == nil
"hello there".send(@method, -20..-1).should == nil
"hello there".send(@method, fixnum_max..).should == nil
"hello there".send(@method, fixnum_min..-1).should == nil

"".send(@method, -1..-1).should == nil
"".send(@method, -1...-1).should == nil
Expand Down
2 changes: 2 additions & 0 deletions spec/ruby/core/string/slice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
a.should == "hello"
a.slice!(-20).should == nil
a.should == "hello"
a.slice!(fixnum_max).should == nil
a.should == "hello"
end

it "raises a FrozenError if self is frozen" do
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/ruby/core/string/element_reference_tags.txt

This file was deleted.

2 changes: 0 additions & 2 deletions spec/tags/ruby/core/string/slice_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion test/mri/excludes/TestString.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
exclude :test_AREF_underflow, "needs investigation"
exclude :test_ASET, "expected behavior; JRuby can only do int range offsets into a string (integer -9223372036854775808 too small to convert to `int')"
exclude :test_crypt, "no working assert_no_memory_leak method"
exclude :test_delete_prefix_bang_broken_encoding, "needs investigation"
Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestString2.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
exclude :test_AREF_underflow, "needs investigation"
exclude :test_ASET, "expected behavior; JRuby can only do int range offsets into a string (integer -9223372036854775808 too small to convert to `int')"
exclude :test_crypt, "no working assert_no_memory_leak method"
exclude :test_delete_prefix_bang_broken_encoding, "needs investigation"
Expand Down
Loading