Search in sources :

Example 1 with MatchState

use of com.github.anba.es6draft.regexp.MatchState in project es6draft by anba.

the class RegExpPrototype method matchOrNull.

/**
     * 21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S ) (1)
     * 
     * @param r
     *            the regular expression object
     * @param s
     *            the string
     * @param lastIndex
     *            the lastIndex position
     * @param global
     *            the global flag
     * @param sticky
     *            the sticky flag
     * @return the match state or {@code null}
     */
private static MatchState matchOrNull(RegExpObject r, String s, int lastIndex, boolean global, boolean sticky) {
    /* step 1 */
    assert r.getRegExpMatcher() != null;
    /* step 10 */
    if (!global && !sticky) {
        lastIndex = 0;
    }
    /* step 15.a */
    if (lastIndex > s.length()) {
        return null;
    }
    /* step 11 */
    RegExpMatcher matcher = r.getRegExpMatcher();
    /* steps 12-13 (not applicable) */
    /* steps 14-15 */
    MatchState m = matcher.matcher(s);
    boolean matchSucceeded;
    if (!sticky) {
        matchSucceeded = m.find(lastIndex);
    } else {
        matchSucceeded = m.matches(lastIndex);
    }
    /* step 15.a, 15.c */
    if (!matchSucceeded) {
        return null;
    }
    return m;
}
Also used : RegExpMatcher(com.github.anba.es6draft.regexp.RegExpMatcher) MatchState(com.github.anba.es6draft.regexp.MatchState)

Example 2 with MatchState

use of com.github.anba.es6draft.regexp.MatchState in project es6draft by anba.

the class RegExpPrototype method matchResultOrNull.

/**
     * 21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S ) (1)
     * 
     * @param cx
     *            the execution context
     * @param r
     *            the regular expression object
     * @param s
     *            the string
     * @param storeResult
     *            {@code true} if the match result is stored
     * @return the match result or {@code null}
     */
private static MatchResult matchResultOrNull(ExecutionContext cx, RegExpObject r, String s, boolean storeResult) {
    /* step 1 */
    assert r.getRegExpMatcher() != null;
    /* steps 2-3 (not applicable) */
    /* steps 4-5 */
    int lastIndex = (int) Math.min(ToLength(cx, Get(cx, r, "lastIndex")), Integer.MAX_VALUE);
    /* steps 6-7 */
    boolean global = ToBoolean(Get(cx, r, "global"));
    /* steps 8-9 */
    boolean sticky = ToBoolean(Get(cx, r, "sticky"));
    /* steps 10-15 */
    MatchState m = matchOrNull(r, s, lastIndex, global, sticky);
    /* step 15.a, 15.c */
    if (m == null) {
        Set(cx, r, "lastIndex", 0, true);
        return null;
    }
    /* steps 16-17 */
    int e = m.end();
    /* step 18 */
    if (global || sticky) {
        Set(cx, r, "lastIndex", e, true);
    }
    MatchResult result = m.toMatchResult();
    if (storeResult) {
        RegExpConstructor.storeLastMatchResult(cx, s, result);
    }
    return result;
}
Also used : MatchState(com.github.anba.es6draft.regexp.MatchState) MatchResult(java.util.regex.MatchResult) IterableMatchResult(com.github.anba.es6draft.regexp.IterableMatchResult)

Example 3 with MatchState

use of com.github.anba.es6draft.regexp.MatchState in project es6draft by anba.

the class RegExpPrototype method RegExpSplit.

/**
     * Internal {@code RegExp.prototype.split()} function.
     * 
     * @param cx
     *            the execution context
     * @param rx
     *            the regular expression object
     * @param s
     *            the string
     * @param unicodeMatching
     *            {@code true} for unicode matching
     * @param lim
     *            the split limit
     * @return the split result array
     */
private static ArrayObject RegExpSplit(ExecutionContext cx, RegExpObject rx, String s, boolean unicodeMatching, long lim) {
    /* steps 1-12, 17-18 (not applicable) */
    /* step 15 */
    ArrayObject a = ArrayCreate(cx, 0);
    /* step 16 */
    int lengthA = 0;
    /* step 19 */
    int size = s.length();
    /* step 20 */
    int p = 0;
    /* step 21 */
    if (lim == 0) {
        return a;
    }
    /* step 22 */
    if (size == 0) {
        MatchState matcher = rx.getRegExpMatcher().matcher(s);
        if (matcher.find(0)) {
            RegExpConstructor.storeLastMatchResult(cx, s, matcher.toMatchResult());
            return a;
        }
        CreateDataProperty(cx, a, 0, s);
        return a;
    }
    /* step 23 */
    int q = p;
    /* step 24 */
    int lastStart = -1;
    MatchState matcher = rx.getRegExpMatcher().matcher(s);
    while (q != size) {
        /* steps 24.a-d */
        boolean match = matcher.find(q);
        /* step 24.e (implicit) */
        if (!match) {
            break;
        }
        RegExpConstructor.storeLastMatchResult(cx, s, matcher.toMatchResult());
        /* steps 24.f.i-ii */
        int e = matcher.end();
        /* step 24.f.iii-iv */
        if (e == p) {
            /* step 24.f.iii */
            q = AdvanceStringIndex(s, q, unicodeMatching);
        } else {
            /* step 24.f.iv */
            String t = s.substring(p, lastStart = matcher.start());
            CreateDataProperty(cx, a, lengthA, t);
            lengthA += 1;
            if (lengthA == lim) {
                return a;
            }
            Iterator<String> iterator = groupIterator(matcher, matcher.groupCount());
            while (iterator.hasNext()) {
                String cap = iterator.next();
                CreateDataProperty(cx, a, lengthA, cap != null ? cap : UNDEFINED);
                lengthA += 1;
                if (lengthA == lim) {
                    return a;
                }
            }
            p = e;
            q = p;
        }
    }
    if (lastStart == size) {
        return a;
    }
    /* step 25 */
    String t = s.substring(p, size);
    /* steps 26-27 */
    CreateDataProperty(cx, a, lengthA, t);
    /* step 28 */
    return a;
}
Also used : ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) MatchState(com.github.anba.es6draft.regexp.MatchState)

Aggregations

MatchState (com.github.anba.es6draft.regexp.MatchState)3 IterableMatchResult (com.github.anba.es6draft.regexp.IterableMatchResult)1 RegExpMatcher (com.github.anba.es6draft.regexp.RegExpMatcher)1 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)1 MatchResult (java.util.regex.MatchResult)1