Skip to content

Commit

Permalink
Support encoding keyword in initialize (JRuby) (#45)
Browse files Browse the repository at this point in the history
This PR will add support for the `encoding` keyword to `initialize` in
the JRuby extension.

Unfortunately there appears to be no tests for this behavior, so adding
some may have to be part of this work.
  • Loading branch information
headius committed Apr 13, 2023
1 parent 06ceff7 commit 1977eb1
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ext/java/org/jruby/ext/stringio/StringIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,19 @@ private void strioInit(ThreadContext context, IRubyObject[] args) {
StringIOData ptr = this.ptr;

synchronized (ptr) {
switch (args.length) {
int argc = args.length;
Encoding encoding = null;

IRubyObject options = ArgsUtil.getOptionsArg(runtime, args);
if (!options.isNil()) {
argc--;
IRubyObject encodingOpt = ArgsUtil.extractKeywordArg(context, "encoding", (RubyHash) options);
if (!encodingOpt.isNil()) {
encoding = EncodingUtils.toEncoding(context, encodingOpt);
}
}

switch (argc) {
case 2:
mode = args[1];
final boolean trunc;
Expand Down Expand Up @@ -192,7 +204,7 @@ private void strioInit(ThreadContext context, IRubyObject[] args) {
}

ptr.string = string;
ptr.enc = null;
ptr.enc = encoding;
ptr.pos = 0;
ptr.lineno = 0;
// funky way of shifting readwrite flags into object flags
Expand Down

0 comments on commit 1977eb1

Please sign in to comment.