int RegularExpression::match(const String& str, int startFrom, int* matchLength) const
{
    if (!d->regexp())
        return -1;

    if (str.isNull())
        return -1;

    // First 2 offsets are start and end offsets; 3rd entry is used internally by pcre
    static const size_t maxOffsets = 3;
    int offsets[maxOffsets];
    int result = jsRegExpExecute(d->regexp(), str.characters(), str.length(), startFrom, offsets, maxOffsets);
    if (result < 0) {
        if (result != JSRegExpErrorNoMatch)
            LOG_ERROR("RegularExpression: pcre_exec() failed with result %d", result);
        d->lastMatchLength = -1;
        return -1;
    }

    // 1 means 1 match; 0 means more than one match. First match is recorded in offsets.
    d->lastMatchLength = offsets[1] - offsets[0];
    if (matchLength)
        *matchLength = d->lastMatchLength;
    return offsets[0];
}
int RegularExpression::match(const String& str, int startFrom, int* matchLength) const
{
    if (str.isNull())
        return -1;

    d->lastMatchString = str;
    // First 2 offsets are start and end offsets; 3rd entry is used internally by pcre
    d->lastMatchCount = jsRegExpExecute(d->regex, d->lastMatchString.characters(),
        d->lastMatchString.length(), startFrom, d->lastMatchOffsets, maxOffsets);
    if (d->lastMatchCount < 0) {
        if (d->lastMatchCount != JSRegExpErrorNoMatch)
            LOG_ERROR("RegularExpression: pcre_exec() failed with result %d", d->lastMatchCount);
        d->lastMatchPos = -1;
        d->lastMatchLength = -1;
        d->lastMatchString = String();
        return -1;
    }
    
    // 1 means 1 match; 0 means more than one match. First match is recorded in offsets.
    d->lastMatchPos = d->lastMatchOffsets[0];
    d->lastMatchLength = d->lastMatchOffsets[1] - d->lastMatchOffsets[0];
    if (matchLength)
        *matchLength = d->lastMatchLength;
    return d->lastMatchPos;
}
Beispiel #3
0
int RegExp::match(const UString& s, int startOffset, Vector<int, 32>* ovector)
{
    if (startOffset < 0)
        startOffset = 0;
    if (ovector)
        ovector->clear();

    if (startOffset > s.size() || s.isNull())
        return -1;

#if ENABLE(WREC)
    if (m_wrecFunction) {
        int offsetVectorSize = (m_numSubpatterns + 1) * 2;
        int* offsetVector;
        Vector<int, 32> nonReturnedOvector;
        if (ovector) {
            ovector->resize(offsetVectorSize);
            offsetVector = ovector->data();
        } else {
            nonReturnedOvector.resize(offsetVectorSize);
            offsetVector = nonReturnedOvector.data();
        }
        ASSERT(offsetVector);
        for (int j = 0; j < offsetVectorSize; ++j)
            offsetVector[j] = -1;

        int result = m_wrecFunction(s.data(), startOffset, s.size(), offsetVector);

        if (result < 0) {
#ifndef NDEBUG
            // TODO: define up a symbol, rather than magic -1
            if (result != -1)
                fprintf(stderr, "jsRegExpExecute failed with result %d\n", result);
#endif
            if (ovector)
                ovector->clear();
        }
        return result;
    } else
#endif
        if (m_regExp) {
            // Set up the offset vector for the result.
            // First 2/3 used for result, the last third used by PCRE.
            int* offsetVector;
            int offsetVectorSize;
            int fixedSizeOffsetVector[3];
            if (!ovector) {
                offsetVectorSize = 3;
                offsetVector = fixedSizeOffsetVector;
            } else {
                offsetVectorSize = (m_numSubpatterns + 1) * 3;
                ovector->resize(offsetVectorSize);
                offsetVector = ovector->data();
            }

            int numMatches = jsRegExpExecute(m_regExp, reinterpret_cast<const UChar*>(s.data()), s.size(), startOffset, offsetVector, offsetVectorSize);

            if (numMatches < 0) {
#ifndef NDEBUG
                if (numMatches != JSRegExpErrorNoMatch)
                    fprintf(stderr, "jsRegExpExecute failed with result %d\n", numMatches);
#endif
                if (ovector)
                    ovector->clear();
                return -1;
            }

            return offsetVector[0];
        }

    return -1;
}