diff --git a/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java b/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java index 8df813f97b..631421729e 100644 --- a/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java +++ b/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java @@ -240,12 +240,13 @@ public IRubyObject charpos(ThreadContext context) { } private IRubyObject extractRange(Ruby runtime, int beg, int end) { - int size = str.getByteList().getRealSize(); + ByteList byteList = str.getByteList(); + int size = byteList.getRealSize(); if (beg > size) return runtime.getNil(); if (end > size) end = size; - return str.makeSharedString(runtime, beg, end - beg); + return newString(runtime, beg, end - beg); } private IRubyObject extractBegLen(Ruby runtime, int beg, int len) { @@ -256,7 +257,7 @@ private IRubyObject extractBegLen(Ruby runtime, int beg, int len) { if (beg > size) return runtime.getNil(); len = Math.min(len, size - beg); - return str.makeSharedString(runtime, beg, len); + return newString(runtime, beg, len); } // MRI: strscan_do_scan @@ -868,6 +869,16 @@ public IRubyObject values_at(ThreadContext context, IRubyObject index1, IRubyObj return RubyArray.newArray(context.runtime, op_aref(context, index1), op_aref(context, index2), op_aref(context, index3)); } + // MRI: str_new + private RubyString newString(Ruby runtime, int start, int length) { + ByteList byteList = str.getByteList(); + int begin = byteList.begin(); + + ByteList newByteList = new ByteList(byteList.unsafeBytes(), begin + start, begin + length, byteList.getEncoding(), true); + + return RubyString.newString(runtime, newByteList); + } + /** * @deprecated Only defined for backward compatibility in CRuby. */