Skip to content

Commit

Permalink
Arity-split String#encode!
Browse files Browse the repository at this point in the history
This led to a few other things getting split, plus a lambda form
that avoids the temporary string carrier array.
  • Loading branch information
headius committed Dec 21, 2023
1 parent 1fd1f73 commit 60b4d48
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 95 deletions.
57 changes: 35 additions & 22 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -6596,39 +6596,43 @@ public IRubyObject encoding(ThreadContext context) {
return context.runtime.getEncodingService().getEncoding(value.getEncoding());
}

// TODO: re-split this
public IRubyObject encode_bang(ThreadContext context, IRubyObject arg0) {
return encode_bang(context, new IRubyObject[]{arg0});
}
@JRubyMethod(name = "encode!")
public IRubyObject encode_bang(ThreadContext context) {
modify19();

public IRubyObject encode_bang(ThreadContext context, IRubyObject arg0, IRubyObject arg1) {
return encode_bang(context, new IRubyObject[]{arg0,arg1});
return EncodingUtils.strTranscode(context, this, RubyString::updateFromTranscode);
}

public IRubyObject encode_bang(ThreadContext context, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
return encode_bang(context, new IRubyObject[]{arg0,arg1,arg2});
@JRubyMethod(name = "encode!")
public IRubyObject encode_bang(ThreadContext context, IRubyObject arg0) {
modify19();

return EncodingUtils.strTranscode(context, arg0, this, RubyString::updateFromTranscode);
}

@JRubyMethod(name = "encode!", optional = 3, checkArity = false)
public IRubyObject encode_bang(ThreadContext context, IRubyObject[] args) {
Arity.checkArgumentCount(context, args, 0, 3);
@JRubyMethod(name = "encode!")
public IRubyObject encode_bang(ThreadContext context, IRubyObject arg0, IRubyObject arg1) {
modify19();

IRubyObject[] newstr_p;
Encoding encindex;
return EncodingUtils.strTranscode(context, arg0, arg1, this, RubyString::updateFromTranscode);
}

@JRubyMethod(name = "encode!")
public IRubyObject encode_bang(ThreadContext context, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
modify19();

newstr_p = new IRubyObject[]{this};
encindex = EncodingUtils.strTranscode(context, args, newstr_p);
return EncodingUtils.strTranscode(context, arg0, arg1, arg2, this, RubyString::updateFromTranscode);
}

if (encindex == null) return this;
if (newstr_p[0] == this) {
setEncoding(encindex);
return this;
private static RubyString updateFromTranscode(ThreadContext context, RubyString self, Encoding encindex, RubyString newstr) {
if (encindex == null) return self;
if (newstr == self) {
self.setEncoding(encindex);
return self;
}
replace(newstr_p[0]);
setEncoding(encindex);
return this;
self.replace(newstr);
self.setEncoding(encindex);
return self;
}

@JRubyMethod
Expand Down Expand Up @@ -7244,4 +7248,13 @@ public RubyArray unpack(IRubyObject obj) {
return Pack.unpack(getRuntime(), this.value, stringValue(obj).value);
}

@Deprecated
public IRubyObject encode_bang(ThreadContext context, IRubyObject[] args) {
Arity.checkArgumentCount(context, args, 0, 2);

modify19();

return EncodingUtils.strTranscode(context, args, this, RubyString::updateFromTranscode);
}

}
Loading

0 comments on commit 60b4d48

Please sign in to comment.