Skip to content

Commit 9356ed0

Browse files
committed
Add encoding, string path for checkEncoding
The stringio library depends on being able to pass an encoding only as the first argument to rb_enc_check, which causes the downstream enc_compatible_latter to reject incompatible encodings regardless of the first string being blank. Part of fixes to pass stringio tests in ruby/stringio#116.
1 parent 00021bc commit 9356ed0

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

core/src/main/java/org/jruby/RubyEncoding.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.jruby.runtime.encoding.EncodingService;
5454
import org.jruby.runtime.opto.OptoFactory;
5555
import org.jruby.util.ByteList;
56+
import org.jruby.util.CodeRangeable;
5657
import org.jruby.util.StringSupport;
5758
import org.jruby.util.io.EncodingUtils;
5859

@@ -473,6 +474,13 @@ private static RawCoder getRawCoder() {
473474
return coder;
474475
}
475476

477+
public static Encoding checkEncoding(ThreadContext context, Encoding encoding, CodeRangeable other) {
478+
Encoding enc = StringSupport.areCompatible(encoding, other);
479+
if (enc == null) throw context.runtime.newEncodingCompatibilityError("incompatible character encodings: " +
480+
encoding + " and " + other.getByteList().getEncoding());
481+
return enc;
482+
}
483+
476484
@JRubyMethod(name = "list", meta = true)
477485
public static IRubyObject list(ThreadContext context, IRubyObject recv) {
478486
Ruby runtime = context.runtime;

core/src/main/java/org/jruby/util/StringSupport.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,15 @@ public static Encoding areCompatible(CodeRangeable str1, CodeRangeable str2) {
19461946
str2.scanForCodeRange();
19471947
return encCompatibleLatter(str1, str2, enc1, enc2);
19481948
}
1949+
1950+
public static Encoding areCompatible(Encoding enc1, CodeRangeable str2) {
1951+
Encoding enc2 = str2.getByteList().getEncoding();
1952+
1953+
if (enc1 == enc2) return enc1;
1954+
1955+
str2.scanForCodeRange();
1956+
return encCompatibleLatter(null, str2, enc1, enc2);
1957+
}
19491958

19501959
private static Encoding encCompatibleLatter(CodeRangeable str1, CodeRangeable str2, Encoding enc1, Encoding enc2) {
19511960
boolean isstr1, isstr2;
@@ -1972,9 +1981,9 @@ private static Encoding encCompatibleLatter(CodeRangeable str1, CodeRangeable st
19721981

19731982
if (!isstr1) {
19741983
CodeRangeable tmp = str1;
1975-
Encoding enc0 = enc1;
19761984
str1 = str2;
19771985
str2 = tmp;
1986+
Encoding enc0 = enc1;
19781987
enc1 = enc2;
19791988
enc2 = enc0;
19801989
boolean tmp2 = isstr1;

0 commit comments

Comments
 (0)