private boolean substr(byte[] srcBytes, int offset, int range, ImmutableBytesWritable outPtr) { Matcher matcher = pattern.matcher(srcBytes, 0, range); boolean ret = matcher.search(offset, range, Option.DEFAULT) >= 0; if (ret) { int len = matcher.getEnd() - matcher.getBegin(); outPtr.set(srcBytes, matcher.getBegin(), len); } else { outPtr.set(ByteUtil.EMPTY_BYTE_ARRAY); } return ret; }
break; searchResults.add(new PairInt(matcher.getBegin(), matcher.getEnd())); totalBytesNeeded += (nextCur - cur) + replaceLen; cur = matcher.getEnd();
private boolean substr(byte[] srcBytes, int offset, int range, ImmutableBytesWritable outPtr) { Matcher matcher = pattern.matcher(srcBytes, 0, range); boolean ret = matcher.search(offset, range, Option.DEFAULT) >= 0; if (ret) { int len = matcher.getEnd() - matcher.getBegin(); outPtr.set(srcBytes, matcher.getBegin(), len); } else { outPtr.set(ByteUtil.EMPTY_BYTE_ARRAY); } return ret; }
private boolean substr(byte[] srcBytes, int offset, int range, ImmutableBytesWritable outPtr) { Matcher matcher = pattern.matcher(srcBytes, 0, range); boolean ret = matcher.search(offset, range, Option.DEFAULT) >= 0; if (ret) { int len = matcher.getEnd() - matcher.getBegin(); outPtr.set(srcBytes, matcher.getBegin(), len); } else { outPtr.set(ByteUtil.EMPTY_BYTE_ARRAY); } return ret; }
private IRubyObject scanOnceNG(RubyRegexp regex, Matcher matcher, int range) { if (matcher.search(matcher.value + value.begin, range, Option.NONE) >= 0) { int end = matcher.getEnd(); if (matcher.getBegin() == end) { if (value.realSize > end) { matcher.value = end + regex.getPattern().getEncoding().length(value.bytes[value.begin + end]); } else { matcher.value = end + 1; } } else { matcher.value = end; } return substr(matcher.getBegin(), end - matcher.getBegin()).infectBy(regex); } return null; }
private boolean update(int r) { begin = matcher.getBegin(); end = matcher.getEnd(); region = matcher.getRegion(); position = position.relativeTo(begin, end); return r > Matcher.FAILED; }
static RubyString regsub(ThreadContext context, RubyString str, RubyString src, Regex pattern, Matcher matcher) { return regsub(context, str, src, pattern, matcher.getRegion(), matcher.getBegin(), matcher.getEnd()); }
static RubyString regsub(ThreadContext context, RubyString str, RubyString src, Regex pattern, Matcher matcher) { return regsub(context, str, src, pattern, matcher.getRegion(), matcher.getBegin(), matcher.getEnd()); }
private int positionEnd(Matcher matcher, Encoding enc, int begin, int range) { int end = matcher.getEnd(); if (matcher.getBegin() == end) { if (value.getRealSize() > end) { return end + enc.length(value.getUnsafeBytes(), begin + end, range); } else { return end + 1; } } else { return end; } }
private int positionEnd(Matcher matcher, Encoding enc, int begin, int range) { int end = matcher.getEnd(); if (matcher.getBegin() == end) { if (value.getRealSize() > end) { return end + enc.length(value.getUnsafeBytes(), begin + end, range); } else { return end + 1; } } else { return end; } }
/** * rb_str_scan */ public static int positionEndForScan(ByteList value, Matcher matcher, Encoding enc, int begin, int range) { int end = matcher.getEnd(); if (matcher.getBegin() == end) { if (value.getRealSize() > end) { return end + enc.length(value.getUnsafeBytes(), begin + end, range); } else { return end + 1; } } else { return end; } }
/** * rb_str_scan */ public static int positionEndForScan(ByteList value, Matcher matcher, Encoding enc, int begin, int range) { int end = matcher.getEnd(); if (matcher.getBegin() == end) { if (value.getRealSize() > end) { return end + enc.length(value.getUnsafeBytes(), begin + end, range); } else { return end + 1; } } else { return end; } }
static final RubyMatchData createMatchData(ThreadContext context, RubyString str, Matcher matcher, Regex pattern) { Ruby runtime = context.runtime; final RubyMatchData match = new RubyMatchData(runtime); // FIXME: This is pretty gross; we should have a cleaner initialization // that doesn't depend on package-visible fields and ideally is atomic, // probably using an immutable structure we replace all at once. // The region must be cloned because a subsequent match will update the // region, resulting in the MatchData created here pointing at the // incorrect region (capture/group). Region region = matcher.getRegion(); // lazy, null when no groups defined match.regs = region == null ? null : region.clone(); match.begin = matcher.getBegin(); match.end = matcher.getEnd(); match.pattern = pattern; match.str = (RubyString)str.strDup(runtime).freeze(context); match.infectBy(str); return match; }
static final RubyMatchData createMatchData(ThreadContext context, RubyString str, Matcher matcher, Regex pattern) { Ruby runtime = context.runtime; final RubyMatchData match = new RubyMatchData(runtime); // FIXME: This is pretty gross; we should have a cleaner initialization // that doesn't depend on package-visible fields and ideally is atomic, // probably using an immutable structure we replace all at once. // The region must be cloned because a subsequent match will update the // region, resulting in the MatchData created here pointing at the // incorrect region (capture/group). Region region = matcher.getRegion(); // lazy, null when no groups defined match.regs = region == null ? null : region.clone(); match.begin = matcher.getBegin(); match.end = matcher.getEnd(); match.pattern = pattern; match.str = (RubyString)str.strDup(runtime).freeze(context); match.infectBy(str); return match; }
private IRubyObject subBangIter(ThreadContext context, Regex pattern, Block block) { int range = value.getBegin() + value.getRealSize(); Matcher matcher = pattern.matcher(value.getUnsafeBytes(), value.getBegin(), range); if (RubyRegexp.matcherSearch(context.runtime, matcher, value.getBegin(), range, Option.NONE) >= 0) { frozenCheck(true); byte[] bytes = value.getUnsafeBytes(); int size = value.getRealSize(); RubyMatchData match = RubyRegexp.createMatchData(context, this, matcher, pattern); context.setBackRef(match); RubyString repl = objAsString(context, block.yield(context, makeShared(context.runtime, matcher.getBegin(), matcher.getEnd() - matcher.getBegin()))); modifyCheck(bytes, size); frozenCheck(true); context.setBackRef(match); return subBangCommon(context, pattern, matcher, repl, repl.flags); } else { return context.setBackRef(context.runtime.getNil()); } }
private IRubyObject subBangIter(ThreadContext context, Regex pattern, Block block) { int range = value.getBegin() + value.getRealSize(); Matcher matcher = pattern.matcher(value.getUnsafeBytes(), value.getBegin(), range); if (RubyRegexp.matcherSearch(context.runtime, matcher, value.getBegin(), range, Option.NONE) >= 0) { frozenCheck(true); byte[] bytes = value.getUnsafeBytes(); int size = value.getRealSize(); RubyMatchData match = RubyRegexp.createMatchData(context, this, matcher, pattern); context.setBackRef(match); RubyString repl = objAsString(context, block.yield(context, makeShared(context.runtime, matcher.getBegin(), matcher.getEnd() - matcher.getBegin()))); modifyCheck(bytes, size); frozenCheck(true); context.setBackRef(match); return subBangCommon(context, pattern, matcher, repl, repl.flags); } else { return context.setBackRef(context.runtime.getNil()); } }
private IRubyObject subBangCommon(ThreadContext context, Regex pattern, Matcher matcher, RubyString repl, int tuFlags) { final int beg = matcher.getBegin(); final int plen = matcher.getEnd() - beg; ByteList replValue = repl.value; if (replValue.getRealSize() > plen) { modify(value.getRealSize() + replValue.getRealSize() - plen); } else { modify(); } if (replValue.getRealSize() != plen) { int src = value.getBegin() + beg + plen; int dst = value.getBegin() + beg + replValue.getRealSize(); int length = value.getRealSize() - beg - plen; System.arraycopy(value.getUnsafeBytes(), src, value.getUnsafeBytes(), dst, length); } System.arraycopy(replValue.getUnsafeBytes(), replValue.getBegin(), value.getUnsafeBytes(), value.getBegin() + beg, replValue.getRealSize()); value.setRealSize(value.getRealSize() + replValue.getRealSize() - plen); infectBy(tuFlags); return this; }
private IRubyObject subBangCommon(ThreadContext context, Regex pattern, Matcher matcher, RubyString repl, int tuFlags) { final int beg = matcher.getBegin(); final int plen = matcher.getEnd() - beg; ByteList replValue = repl.value; if (replValue.getRealSize() > plen) { modify(value.getRealSize() + replValue.getRealSize() - plen); } else { modify(); } if (replValue.getRealSize() != plen) { int src = value.getBegin() + beg + plen; int dst = value.getBegin() + beg + replValue.getRealSize(); int length = value.getRealSize() - beg - plen; System.arraycopy(value.getUnsafeBytes(), src, value.getUnsafeBytes(), dst, length); } System.arraycopy(replValue.getUnsafeBytes(), replValue.getBegin(), value.getUnsafeBytes(), value.getBegin() + beg, replValue.getRealSize()); value.setRealSize(value.getRealSize() + replValue.getRealSize() - plen); infectBy(tuFlags); return this; }
final RubyMatchData updateBackRef(ThreadContext context, RubyString str, Frame frame, Matcher matcher) { Ruby runtime = context.getRuntime(); IRubyObject backref = frame.getBackRef(); final RubyMatchData match; if (backref == null || backref.isNil() || ((RubyMatchData)backref).used()) { match = new RubyMatchData(runtime); } else { match = (RubyMatchData)backref; match.setTaint(runtime.getSafeLevel() >= 3); } match.regs = matcher.getRegion(); // lazy, null when no groups defined match.begin = matcher.getBegin(); match.end = matcher.getEnd(); match.str = (RubyString)str.strDup(runtime).freeze(context); match.pattern = pattern; frame.setBackRef(match); match.infectBy(this); match.infectBy(str); return match; }