Package org.joni

Examples of org.joni.Matcher


        ByteList value = str.getByteList();

        if (pos <= value.getRealSize() && pos >= 0) {
            int realSize = value.getRealSize();
            int begin = value.getBegin();
            Matcher matcher = preparePattern(str).matcher(value.getUnsafeBytes(), begin, begin + realSize);

            int result = matcher.search(begin + pos, begin + (reverse ? 0 : realSize), Option.NONE);
            if (result >= 0) {
                updateBackRef(context, str, scope, matcher).charOffsetUpdated = false;;
                return result;
            }
        }
View Full Code Here


        clearMatched();
        int rest = str.getByteList().getRealSize() - pos;
        if (rest < 0) return getRuntime().getNil();

        ByteList value = str.getByteList();
        Matcher matcher = pattern.matcher(value.getUnsafeBytes(), value.getBegin() + pos, value.getBegin() + value.getRealSize());

        final int ret;
        if (headonly) {
            ret = matcher.match(value.getBegin() + pos, value.getBegin() + value.getRealSize(), Option.NONE);
        } else {
            ret = matcher.search(value.getBegin() + pos, value.getBegin() + value.getRealSize(), Option.NONE);
        }

        regs = matcher.getRegion();
        if (regs == null) {
            beg = matcher.getBegin();
            end = matcher.getEnd();
        } else {
            beg = regs.beg[0];
            end = regs.end[0];
        }
View Full Code Here

        return subBangNoIter(context, getQuotedPattern(arg0), arg1.convertToString());
    }

    private IRubyObject subBangIter(ThreadContext context, Regex pattern, Block block) {
        int range = value.getBegin() + value.getRealSize();
        Matcher matcher = pattern.matcher(value.getUnsafeBytes(), value.getBegin(), range);

        DynamicScope scope = context.getCurrentScope();
        if (matcher.search(value.getBegin(), range, Option.NONE) >= 0) {
            frozenCheck(true);
            byte[] bytes = value.getUnsafeBytes();
            int size = value.getRealSize();
            RubyMatchData match = RubyRegexp.updateBackRef(context, this, scope, matcher, pattern);
            RubyString repl = objAsString(context, block.yield(context,
                    makeShared(context.getRuntime(), matcher.getBegin(), matcher.getEnd() - matcher.getBegin())));
            modifyCheck(bytes, size);
            frozenCheck(true);
            scope.setBackRef(match);
            return subBangCommon(context, pattern, matcher, repl, repl.flags);
        } else {
View Full Code Here

    }

    private IRubyObject subBangNoIter(ThreadContext context, Regex pattern, RubyString repl) {
        int tuFlags = repl.flags;
        int range = value.getBegin() + value.getRealSize();
        Matcher matcher = pattern.matcher(value.getUnsafeBytes(), value.getBegin(), range);

        DynamicScope scope = context.getCurrentScope();
        if (matcher.search(value.getBegin(), range, Option.NONE) >= 0) {
            repl = RubyRegexp.regsub(repl, this, matcher, context.getRuntime().getKCode().getEncoding());
            RubyRegexp.updateBackRef(context, this, scope, matcher, pattern);
            return subBangCommon(context, pattern, matcher, repl, tuFlags);
        } else {
            return scope.setBackRef(context.getRuntime().getNil());
View Full Code Here

        int begin = value.getBegin();
        int len = value.getRealSize();
        int range = begin + len;
        byte[]bytes = value.getUnsafeBytes();
        Encoding enc = value.getEncoding();
        final Matcher matcher = prepared.matcher(bytes, begin, range);

        DynamicScope scope = context.getCurrentScope();
        if (matcher.search(begin, range, Option.NONE) >= 0) {
            RubyMatchData match = RubyRegexp.updateBackRef19(context, this, scope, matcher, pattern);
            match.regexp = regexp;
            final RubyString repl;
            final int tuFlags;
            IRubyObject subStr = makeShared19(runtime, matcher.getBegin(), matcher.getEnd() - matcher.getBegin());
            if (hash == null) {
                tuFlags = 0;
                repl = objAsString(context, block.yield(context, subStr));
            } else {
                tuFlags = hash.flags;
View Full Code Here

    }

    private IRubyObject subBangNoIter19(Ruby runtime, ThreadContext context, Regex pattern, Regex prepared, RubyString repl, RubyRegexp regexp) {
        int begin = value.getBegin();
        int range = begin + value.getRealSize();
        final Matcher matcher = prepared.matcher(value.getUnsafeBytes(), begin, range);

        DynamicScope scope = context.getCurrentScope();
        if (matcher.search(begin, range, Option.NONE) >= 0) {
            repl = RubyRegexp.regsub19(repl, this, matcher, pattern);
            RubyMatchData match = RubyRegexp.updateBackRef19(context, this, scope, matcher, pattern);
            match.regexp = regexp;
            return subBangCommon19(context, pattern, matcher, repl, repl.flags);
        } else {
View Full Code Here

        int begin = value.getBegin();
        int slen = value.getRealSize();
        int range = begin + slen;
        byte[]bytes = value.getUnsafeBytes();
        Matcher matcher = pattern.matcher(bytes, begin, range);

        int beg = matcher.search(begin, range, Option.NONE);
        if (beg < 0) {
            scope.setBackRef(runtime.getNil());
            return bang ? runtime.getNil() : strDup(runtime); /* bang: true, no match, no substitution */
        } else if (repl == null && bang && isFrozen()) {
            throw getRuntime().newRuntimeError("can't modify frozen string");
        }

        int blen = slen + 30; /* len + margin */
        ByteList dest = new ByteList(blen);
        dest.setRealSize(blen);
        int offset = 0, buf = 0, bp = 0, cp = begin;

        Encoding enc = getEncodingForKCodeDefault(runtime, pattern, arg);

        RubyMatchData match = null;
        while (beg >= 0) {
            final RubyString val;
            final int begz = matcher.getBegin();
            final int endz = matcher.getEnd();

            if (repl == null) { // block given
                match = RubyRegexp.updateBackRef(context, this, scope, matcher, pattern);
                val = objAsString(context, block.yield(context, substr(runtime, begz, endz - begz)));
                modifyCheck(bytes, slen);
                if (bang) frozenCheck();
            } else {
                val = RubyRegexp.regsub(repl, this, matcher, enc);
            }

            tuFlags |= val.flags;

            ByteList vbuf = val.value;
            int len = (bp - buf) + (beg - offset) + vbuf.getRealSize() + 3;
            if (blen < len) {
                while (blen < len) blen <<= 1;
                len = bp - buf;
                dest.realloc(blen);
                dest.setRealSize(blen);
                bp = buf + len;
            }
            len = beg - offset; /* copy pre-match substr */
            System.arraycopy(bytes, cp, dest.getUnsafeBytes(), bp, len);
            bp += len;
            System.arraycopy(vbuf.getUnsafeBytes(), vbuf.getBegin(), dest.getUnsafeBytes(), bp, vbuf.getRealSize());
            bp += vbuf.getRealSize();
            offset = endz;

            if (begz == endz) {
                if (slen <= endz) break;
                len = enc.length(bytes, begin + endz, range);
                System.arraycopy(bytes, begin + endz, dest.getUnsafeBytes(), bp, len);
                bp += len;
                offset = endz + len;
            }
            cp = begin + offset;
            if (offset > slen) break;
            beg = matcher.search(cp, range, Option.NONE);
        }

        if (repl == null) { // block given
            scope.setBackRef(match);
        } else {
View Full Code Here

        final int begin = value.getBegin();
        int slen = value.getRealSize();
        final int range = begin + slen;
        byte[]bytes = value.getUnsafeBytes();
        final Matcher matcher = prepared.matcher(bytes, begin, range);

        final DynamicScope scope = context.getCurrentScope();
        int beg = matcher.search(begin, range, Option.NONE);
        if (beg < 0) {
            scope.setBackRef(runtime.getNil());
            return bang ? runtime.getNil() : strDup(runtime); /* bang: true, no match, no substitution */
        }

        RubyString dest = new RubyString(runtime, getMetaClass(), new ByteList(slen + 30));
        int offset = 0, cp = begin;
        Encoding enc = value.getEncoding();

        RubyMatchData match = null;
        do {
            final RubyString val;
            int begz = matcher.getBegin();
            int endz = matcher.getEnd();

            if (repl != null) {     // string given
                val = RubyRegexp.regsub19(repl, this, matcher, pattern);
            } else {
                final RubyString substr = makeShared19(runtime, begz, endz - begz)
                if (hash != null) { // hash given
                    val = objAsString(context, hash.op_aref(context, substr));
                } else {            // block given
                    match = RubyRegexp.updateBackRef19(context, this, scope, matcher, pattern);
                    match.regexp = regexp;
                    val = objAsString(context, block.yield(context, substr));
                }
                modifyCheck(bytes, slen, enc);
                if (bang) frozenCheck();
            }

            tuFlags |= val.flags;

            int len = beg - offset;
            if (len != 0) dest.cat(bytes, cp, len, enc);
            dest.cat19(val);
            offset = endz;
            if (begz == endz) {
                if (slen <= endz) break;
                len = StringSupport.length(enc, bytes, begin + endz, range);
                dest.cat(bytes, begin + endz, len, enc);
                offset = endz + len;
            }
            cp = begin + offset;
            if (offset > slen) break;
            beg = matcher.search(cp, range, Option.NONE);
        } while (beg >= 0);

        if (slen > offset) dest.cat(bytes, cp, slen - offset, enc);

        if (match != null) { // block given
View Full Code Here

        int begin = value.getBegin();
        int len = value.getRealSize();
        int range = begin + len;
        byte[]bytes = value.getUnsafeBytes();

        final Matcher matcher = pattern.matcher(bytes, begin, range);

        RubyArray result = runtime.newArray();
        Encoding enc = getEncodingForKCodeDefault(runtime, pattern, pat);

        boolean captures = pattern.numberOfCaptures() != 0;

        int end, beg = 0;
        boolean lastNull = false;
        int start = begin;
        while ((end = matcher.search(start, range, Option.NONE)) >= 0) {
            if (start == end + begin && matcher.getBegin() == matcher.getEnd()) {
                if (len == 0) {
                    result.append(newEmptyString(runtime, getMetaClass()).infectBy(this));
                    break;
                } else if (lastNull) {
                    result.append(makeShared(runtime, beg, enc.length(bytes, begin + beg, range)));
                    beg = start - begin;
                } else {
                    start += start == range ? 1 : enc.length(bytes, start, range);
                    lastNull = true;
                    continue;
                }
            } else {
                result.append(makeShared(runtime, beg, end - beg));
                beg = matcher.getEnd();
                start = begin + beg;
            }
            lastNull = false;

            if (captures) populateCapturesForSplit(runtime, result, matcher, false);
View Full Code Here

        int begin = value.getBegin();
        int len = value.getRealSize();
        int range = begin + len;
        byte[]bytes = value.getUnsafeBytes();

        final Matcher matcher = prepared.matcher(bytes, begin, range);

        RubyArray result = runtime.newArray();
        Encoding enc = value.getEncoding();
        boolean captures = pattern.numberOfCaptures() != 0;

        int end, beg = 0;
        boolean lastNull = false;
        int start = begin;
        while ((end = matcher.search(start, range, Option.NONE)) >= 0) {
            if (start == end + begin && matcher.getBegin() == matcher.getEnd()) {
                if (len == 0) {
                    result.append(newEmptyString(runtime, getMetaClass()).infectBy(this));
                    break;
                } else if (lastNull) {
                    result.append(makeShared19(runtime, beg, StringSupport.length(enc, bytes, begin + beg, range)));
                    beg = start - begin;
                } else {
                    start += start == range ? 1 : StringSupport.length(enc, bytes, start, range);
                    lastNull = true;
                    continue;
                }
            } else {
                result.append(makeShared19(runtime, beg, end - beg));
                beg = matcher.getEnd();
                start = begin + beg;
            }
            lastNull = false;

            if (captures) populateCapturesForSplit(runtime, result, matcher, true);
View Full Code Here

TOP

Related Classes of org.joni.Matcher

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.