Skip to content

Commit 5634bbe

Browse files
committed
jruby: copy code-range for created sub strings
1 parent b4f29f3 commit 5634bbe

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,23 @@ public IRubyObject values_at(ThreadContext context, IRubyObject index1, IRubyObj
947947
private RubyString newString(Ruby runtime, int start, int length) {
948948
final ByteList strBytes = this.str.getByteList();
949949
ByteList newBytes = new ByteList(strBytes.unsafeBytes(), strBytes.begin() + start, length, true);
950-
return RubyString.newString(runtime, newBytes, strBytes.getEncoding());
950+
951+
final RubyString newStr = RubyString.newString(runtime, newBytes, strBytes.getEncoding());
952+
copyCodeRangeForSubstr(newStr, this.str);
953+
return newStr;
954+
}
955+
956+
/**
957+
* Same as JRuby's (private) <code>RubyString#copyCodeRangeForSubstr</code>.
958+
* Isn't really necessary, but will avoid extra code-range scans for the substrings returned.
959+
*/
960+
private void copyCodeRangeForSubstr(RubyString str, RubyString from) {
961+
if (str.size() == 0) {
962+
str.setCodeRange(from.getEncoding().isAsciiCompatible() ? StringSupport.CR_7BIT : StringSupport.CR_VALID);
963+
} else {
964+
if (from.getCodeRange() == StringSupport.CR_7BIT) str.setCodeRange(StringSupport.CR_7BIT);
965+
// otherwise, leave it as CR_UNKNOWN
966+
}
951967
}
952968

953969
/**

0 commit comments

Comments
 (0)