@@ -240,12 +240,13 @@ public IRubyObject charpos(ThreadContext context) {
240240 }
241241
242242 private IRubyObject extractRange (Ruby runtime , int beg , int end ) {
243- int size = str .getByteList ().getRealSize ();
243+ ByteList byteList = str .getByteList ();
244+ int size = byteList .getRealSize ();
244245
245246 if (beg > size ) return runtime .getNil ();
246247 if (end > size ) end = size ;
247248
248- return str . makeSharedString (runtime , beg , end - beg );
249+ return newString (runtime , beg , end - beg );
249250 }
250251
251252 private IRubyObject extractBegLen (Ruby runtime , int beg , int len ) {
@@ -256,7 +257,7 @@ private IRubyObject extractBegLen(Ruby runtime, int beg, int len) {
256257 if (beg > size ) return runtime .getNil ();
257258 len = Math .min (len , size - beg );
258259
259- return str . makeSharedString (runtime , beg , len );
260+ return newString (runtime , beg , len );
260261 }
261262
262263 // MRI: strscan_do_scan
@@ -868,6 +869,16 @@ public IRubyObject values_at(ThreadContext context, IRubyObject index1, IRubyObj
868869 return RubyArray .newArray (context .runtime , op_aref (context , index1 ), op_aref (context , index2 ), op_aref (context , index3 ));
869870 }
870871
872+ // MRI: str_new
873+ private RubyString newString (Ruby runtime , int start , int length ) {
874+ ByteList byteList = str .getByteList ();
875+ int begin = byteList .begin ();
876+
877+ ByteList newByteList = new ByteList (byteList .unsafeBytes (), begin + start , begin + length , byteList .getEncoding (), true );
878+
879+ return RubyString .newString (runtime , newByteList );
880+ }
881+
871882 /**
872883 * @deprecated Only defined for backward compatibility in CRuby.
873884 */
0 commit comments