Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down