diff --git a/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java b/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java index db33717881..1322deffa3 100644 --- a/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java +++ b/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java @@ -263,20 +263,17 @@ private IRubyObject extractBegLen(Ruby runtime, int beg, int len) { private IRubyObject scan(ThreadContext context, IRubyObject regex, boolean succptr, boolean getstr, boolean headonly) { final Ruby runtime = context.runtime; check(context); - - ByteList strBL = str.getByteList(); - int strBeg = strBL.getBegin(); - clearMatched(); if (restLen() < 0) { return context.nil; } + ByteList strBL = str.getByteList(); + int currPtr = currPtr(); + if (regex instanceof RubyRegexp) { pattern = ((RubyRegexp) regex).preparePattern(str); - - int currPtr = currPtr(); int range = currPtr + restLen(); Matcher matcher = pattern.matcher(strBL.getUnsafeBytes(), matchTarget(), range); @@ -300,23 +297,20 @@ private IRubyObject scan(ThreadContext context, IRubyObject regex, boolean succp if (ret < 0) return context.nil; } else { RubyString pattern = regex.convertToString(); - Encoding patternEnc = str.checkEncoding(pattern); - - if (restLen() < pattern.size()) { - return context.nil; - } - ByteList patternBL = pattern.getByteList(); int patternSize = patternBL.realSize(); if (headonly) { - if (ByteList.memcmp(strBL.unsafeBytes(), strBeg + curr, patternBL.unsafeBytes(), patternBL.begin(), patternSize) != 0) { + if (restLen() < pattern.size()) { + return context.nil; + } + if (ByteList.memcmp(strBL.unsafeBytes(), currPtr, patternBL.unsafeBytes(), patternBL.begin(), patternSize) != 0) { return context.nil; } setRegisters(patternSize); } else { - int pos = StringSupport.index(strBL, patternBL, strBeg + curr, patternEnc); + int pos = StringSupport.index(strBL, patternBL, currPtr, patternEnc); if (pos == -1) { return context.nil; } diff --git a/ext/strscan/strscan.c b/ext/strscan/strscan.c index 24cc5d734e..62a7123499 100644 --- a/ext/strscan/strscan.c +++ b/ext/strscan/strscan.c @@ -709,19 +709,20 @@ strscan_do_scan(VALUE self, VALUE pattern, int succptr, int getstr, int headonly } else { StringValue(pattern); - rb_enc_check(p->str, pattern); - if (S_RESTLEN(p) < RSTRING_LEN(pattern)) { - return Qnil; - } + rb_encoding *enc = rb_enc_check(p->str, pattern); if (headonly) { + if (S_RESTLEN(p) < RSTRING_LEN(pattern)) { + return Qnil; + } if (memcmp(CURPTR(p), RSTRING_PTR(pattern), RSTRING_LEN(pattern)) != 0) { return Qnil; } set_registers(p, RSTRING_LEN(pattern)); - } else { + } + else { long pos = rb_memsearch(RSTRING_PTR(pattern), RSTRING_LEN(pattern), - CURPTR(p), S_RESTLEN(p), rb_enc_get(pattern)); + CURPTR(p), S_RESTLEN(p), enc); if (pos == -1) { return Qnil; }