示例#1
0
// Shared implementation used by test and exec.
bool RegExpObject::match(ExecState* exec)
{
    RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
    UString input = exec->argument(0).toString(exec);
    JSGlobalData* globalData = &exec->globalData();
    if (!regExp()->global()) {
        int position;
        int length;
        regExpConstructor->performMatch(*globalData, d->regExp.get(), input, 0, position, length);
        return position >= 0;
    }

    JSValue jsLastIndex = getLastIndex();
    unsigned lastIndex;
    if (LIKELY(jsLastIndex.isUInt32())) {
        lastIndex = jsLastIndex.asUInt32();
        if (lastIndex > input.length()) {
            setLastIndex(0);
            return false;
        }
    } else {
        double doubleLastIndex = jsLastIndex.toInteger(exec);
        if (doubleLastIndex < 0 || doubleLastIndex > input.length()) {
            setLastIndex(0);
            return false;
        }
        lastIndex = static_cast<unsigned>(doubleLastIndex);
    }

    int position;
    int length = 0;
    regExpConstructor->performMatch(*globalData, d->regExp.get(), input, lastIndex, position, length);
    if (position < 0) {
        setLastIndex(0);
        return false;
    }

    setLastIndex(position + length);
    return true;
}
示例#2
0
JSValue RegExpObject::matchGlobal(ExecState* exec, JSGlobalObject* globalObject, JSString* string)
{
    RegExp* regExp = this->regExp();

    ASSERT(regExp->global());

    VM* vm = &globalObject->vm();

    setLastIndex(exec, 0);
    if (exec->hadException())
        return jsUndefined();

    String s = string->value(exec);
    RegExpConstructor* regExpConstructor = globalObject->regExpConstructor();
    MatchResult result = regExpConstructor->performMatch(*vm, regExp, string, s, 0);

    // return array of matches
    MarkedArgumentBuffer list;
    // We defend ourselves from crazy.
    const size_t maximumReasonableMatchSize = 1000000000;

    if (regExp->unicode()) {
        unsigned stringLength = s.length();
        while (result) {
            if (list.size() > maximumReasonableMatchSize) {
                throwOutOfMemoryError(exec);
                return jsUndefined();
            }

            size_t end = result.end;
            size_t length = end - result.start;
            list.append(jsSubstring(exec, s, result.start, length));
            if (!length)
                end = advanceStringUnicode(s, stringLength, end);
            result = regExpConstructor->performMatch(*vm, regExp, string, s, end);
        }
    } else {
        while (result) {
            if (list.size() > maximumReasonableMatchSize) {
                throwOutOfMemoryError(exec);
                return jsUndefined();
            }

            size_t end = result.end;
            size_t length = end - result.start;
            list.append(jsSubstring(exec, s, result.start, length));
            if (!length)
                ++end;
            result = regExpConstructor->performMatch(*vm, regExp, string, s, end);
        }
    }

    if (list.isEmpty()) {
        // if there are no matches at all, it's important to return
        // Null instead of an empty array, because this matches
        // other browsers and because Null is a false value.
        return jsNull();
    }

    return constructArray(exec, static_cast<ArrayAllocationProfile*>(0), list);
}