Skip to content
Open
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
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@ final long[] begLen(ThreadContext context, long len, int err) {
if (beg < 0) {
beg += len;
if (beg < 0) {
if (err != 0) throw rangeError(context, beg + ".." + (isExclusive ? "." : "") + end + " out of range");
if (err != 0) throw rangeError(context, begin + ".." + (isExclusive ? "." : "") + this.end + " out of range");
return null;
}
}

if (err == 0 || err == 2) {
if (beg > len) {
if (err != 0) throw rangeError(context, beg + ".." + (isExclusive ? "." : "") + end + " out of range");
if (err != 0) throw rangeError(context, begin + ".." + (isExclusive ? "." : "") + this.end + " out of range");
return null;
}
if (end > len) end = len;
Expand Down
18 changes: 10 additions & 8 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -2629,7 +2629,7 @@ private static byte unescapeAscii(byte c) {
@JRubyMethod(name = "insert")
public IRubyObject insert(ThreadContext context, IRubyObject indexArg, IRubyObject arg) {
RubyString str = arg.convertToString();
int index = toInt(context, indexArg);
long index = toLong(context, indexArg);
if (index == -1) {
modifyCheck();
return catWithCodeRange(str);
Expand Down Expand Up @@ -4293,7 +4293,7 @@ private IRubyObject subpat(ThreadContext context, RubyRegexp regex) {
*/
@JRubyMethod(name = "[]=", writes = BACKREF)
public IRubyObject op_aset(ThreadContext context, IRubyObject arg0, IRubyObject arg1) {
if (arg0 instanceof RubyFixnum fixnum) return op_aset(context, fixnum.asInt(context), arg1);
if (arg0 instanceof RubyFixnum fixnum) return op_aset(context, fixnum.getValue(), arg1);
if (arg0 instanceof RubyRegexp regexp) {
subpatSet(context, regexp, null, arg1);
return arg1;
Expand All @@ -4305,23 +4305,23 @@ public IRubyObject op_aset(ThreadContext context, IRubyObject arg0, IRubyObject
strUpdate(context, beg, len, this, arg1.convertToString());
return arg1;
} else if (arg0 instanceof RubyRange range) {
int[] begLen = range.begLenInt(context, strLength(), 2);
long[] begLen = range.begLen(context, strLength(), 2);
strUpdate(context, begLen[0], begLen[1], this, arg1.convertToString());
return arg1;
} else {
StringSites sites = sites(context);
if (RubyRange.isRangeLike(context, arg0, sites.respond_to_begin, sites.respond_to_end)) {
RubyRange rng = RubyRange.rangeFromRangeLike(context, arg0, sites.begin, sites.end, sites.exclude_end);
int[] begLen = rng.begLenInt(context, strLength(), 2);
long[] begLen = rng.begLen(context, strLength(), 2);
strUpdate(context, begLen[0], begLen[1], this, arg1.convertToString());

return arg1;
}
}
return op_aset(context, toInt(context, arg0), arg1);
return op_aset(context, toLong(context, arg0), arg1);
}

private IRubyObject op_aset(ThreadContext context, int idx, IRubyObject arg1) {
private IRubyObject op_aset(ThreadContext context, long idx, IRubyObject arg1) {
strUpdate(context, idx, 1, this, arg1.convertToString());
return arg1;
}
Expand All @@ -4331,8 +4331,10 @@ public IRubyObject op_aset(ThreadContext context, IRubyObject arg0, IRubyObject
if (arg0 instanceof RubyRegexp regexp) {
subpatSet(context, regexp, arg1, arg2);
} else {
int beg = toInt(context, arg0);
int len = checkLength(context, toInt(context, arg1));
long beg = toLong(context, arg0);
long len = toLong(context, arg1);
// before converting arg2, like CRuby
if (len < 0) throw indexError(context, "negative length " + len);
strUpdate(context, beg, len, this, arg2.convertToString());
}
return arg2;
Expand Down
18 changes: 11 additions & 7 deletions core/src/main/java/org/jruby/util/StringSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -1954,20 +1954,24 @@ public static void strUpdate(Ruby runtime, int beg, int len, RubyString source,
strUpdate(runtime.getCurrentContext(), beg, len, source, repl);
}

// MRI: rb_str_update, first half
public static void strUpdate(ThreadContext context, int beg, int len, RubyString source, RubyString repl) {
strUpdate(context, (long) beg, (long) len, source, repl);
}

@CRuby(value = "rb_str_update", file = "string.c", note = "bounds check")
public static void strUpdate(ThreadContext context, long beg, long len, RubyString source, RubyString repl) {
if (len < 0) throw indexError(context, "negative length " + len);

int slen = strLengthFromRubyString(source, source.checkEncoding(repl));

if (slen < beg) throw indexError(context, "index " + beg + " out of string");
if (beg < 0) {
if (-beg > slen) throw indexError(context, "index " + beg + " out of string");
beg += slen;
if (slen < beg || (beg < 0 && beg + slen < 0)) {
throw indexError(context, "index " + beg + " out of string");
}
if (slen < len || slen < beg + len) len = slen - beg;
if (beg < 0) beg += slen;
if (len > slen - beg) len = slen - beg;

strUpdate(beg, len, source, repl);
// both are within slen now
strUpdate((int) beg, (int) len, source, repl);
}

public static boolean isAsciiOnly(CodeRangeable string) {
Expand Down
18 changes: 18 additions & 0 deletions spec/ruby/core/string/element_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
-> { str[-20] = "bam" }.should.raise(IndexError)
str.should == "hello"

-> { str[fixnum_max] = "bam" }.should.raise(IndexError)
str.should == "hello"

-> { str[fixnum_min] = "bam" }.should.raise(IndexError)
str.should == "hello"

-> { ""[-1] = "bam" }.should.raise(IndexError)
end

Expand Down Expand Up @@ -357,12 +363,19 @@
str.should == "aBe"
end

it "replaces up to the end of the String if the Range end is beyond it" do
str = "abcde"
str[1..fixnum_max] = "x"
str.should == "ax"
end

it "raises a RangeError if negative Range begin is out of range" do
-> { "abc"[-4..-2] = "x" }.should.raise(RangeError, "-4..-2 out of range")
end

it "raises a RangeError if positive Range begin is greater than String size" do
-> { "abc"[4..2] = "x" }.should.raise(RangeError, "4..2 out of range")
-> { "abc"[fixnum_max..] = "x" }.should.raise(RangeError, "#{fixnum_max}.. out of range")
end

it "uses the Range end as an index rather than a count" do
Expand Down Expand Up @@ -474,6 +487,9 @@
a = "hello"
a[0, 100] = ""
a.should == ""
a = "hello"
a[1, fixnum_max] = "x"
a.should == "hx"
end

it "appends other_str to the end of the string if idx == the length of the string" do
Expand Down Expand Up @@ -527,6 +543,8 @@
it "raises an IndexError if |idx| is greater than the length of the string" do
-> { "hello"[6, 0] = "bob" }.should.raise(IndexError)
-> { "hello"[-6, 0] = "bob" }.should.raise(IndexError)
-> { "hello"[fixnum_max, 0] = "bob" }.should.raise(IndexError)
-> { "hello"[fixnum_min, 0] = "bob" }.should.raise(IndexError)
end

it "raises an IndexError if count < 0" do
Expand Down
2 changes: 2 additions & 0 deletions spec/ruby/core/string/insert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
it "raises an IndexError if the index is beyond string" do
-> { "abcd".insert(5, 'X') }.should.raise(IndexError)
-> { "abcd".insert(-6, 'X') }.should.raise(IndexError)
-> { "abcd".insert(fixnum_max, 'X') }.should.raise(IndexError)
-> { "abcd".insert(fixnum_min, 'X') }.should.raise(IndexError)
end

it "converts index to an integer using to_int" do
Expand Down
8 changes: 8 additions & 0 deletions spec/ruby/core/string/slice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@

a.slice!(-2, 4).should == "lo"
a.should == "h"

a = "hello"
a.slice!(1, fixnum_max).should == "ello"
a.should == "h"
end

it "returns nil if the given position is out of self" do
Expand Down Expand Up @@ -138,6 +142,10 @@ def obj.method_missing(name, *) name == :to_int ? 2 : super; end

# Edge Case?
"hello".slice!(-3..-9).should == ""

a = "hello"
a.slice!(1..fixnum_max).should == "ello"
a.should == "h"
end

it "returns nil if the given range is out of self" do
Expand Down
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_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"
exclude :test_delete_prefix_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_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"
exclude :test_delete_prefix_broken_encoding, "needs investigation"
Expand Down
Loading