Skip to content

Commit

Permalink
Showing 2 changed files with 33 additions and 4 deletions.
18 changes: 14 additions & 4 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
@@ -1110,13 +1110,23 @@ private IRubyObject op_equalCommon(ThreadContext context, IRubyObject other) {
return invokedynamic(context, other, OP_EQUAL, this).isTrue() ? runtime.getTrue() : runtime.getFalse();
}

public IRubyObject op_plus(ThreadContext context, IRubyObject _str) {
return op_plus19(context, _str);
@JRubyMethod(name = "-@") // -'foo' returns frozen string
public final IRubyObject minus_at() {
return isFrozen() ? this : this.dupFrozen();
}

@JRubyMethod(name = "+@") // +'foo' returns modifiable string
public final IRubyObject plus_at() {
return isFrozen() ? this.dup() : this;
}

public IRubyObject op_plus(ThreadContext context, IRubyObject arg) {
return op_plus19(context, arg);
}

@JRubyMethod(name = "+", required = 1)
public IRubyObject op_plus19(ThreadContext context, IRubyObject _str) {
RubyString str = _str.convertToString();
public IRubyObject op_plus19(ThreadContext context, IRubyObject arg) {
RubyString str = arg.convertToString();
Encoding enc = checkEncoding(str);
RubyString resultStr = newStringNoCopy(context.runtime, StringSupport.addByteLists(value, str.value),
enc, CodeRangeSupport.codeRangeAnd(getCodeRange(), str.getCodeRange()));
19 changes: 19 additions & 0 deletions test/mri/ruby/test_string.rb
Original file line number Diff line number Diff line change
@@ -2301,6 +2301,25 @@ def test_LSHIFT_neary_long_max
end;
end if [0].pack("l!").bytesize < [nil].pack("p").bytesize
# enable only when string size range is smaller than memory space

def test_uplus_minus
str = "foo"
assert_equal(false, str.frozen?)
assert_equal(false, (+str).frozen?)
assert_equal(true, (-str).frozen?)

assert_equal(str.object_id, (+str).object_id)
assert_not_equal(str.object_id, (-str).object_id)

str = "bar".freeze
assert_equal(true, str.frozen?)
assert_equal(false, (+str).frozen?)
assert_equal(true, (-str).frozen?)

assert_not_equal(str.object_id, (+str).object_id)
assert_equal(str.object_id, (-str).object_id)
end

end

class TestString2 < TestString

0 comments on commit 64213e6

Please sign in to comment.