Function CharTypeStringRSearch

Synopsis

#include <include/EASTL/string.h>

static const value_type * CharTypeStringRSearch(const value_type *p1Begin, const value_type *p1End, const value_type *p2Begin, const value_type *p2End)

Description

CharTypeStringRSearch Specialized value_type version of STL find_end() function (which really is a reverse search function). Purpose: find last instance of p2 within p1. Return p1End if not found or if either string is zero length.

Source

Lines 3470-3514 in include/EASTL/string.h. Line 784 in include/EASTL/string.h.

template <typename T, typename Allocator>
const typename basic_string<T, Allocator>::value_type*
basic_string<T, Allocator>::CharTypeStringRSearch(const value_type* p1Begin, const value_type* p1End,
                                                  const value_type* p2Begin, const value_type* p2End)
{
    // Test for zero length strings, in which case we have a match or a failure,
    // but the return value is the same either way.
    if((p1Begin == p1End) || (p2Begin == p2End))
        return p1Begin;
    // Test for a pattern of length 1.
    if((p2Begin + 1) == p2End)
        return CharTypeStringFindEnd(p1Begin, p1End, *p2Begin);
    // Test for search string length being longer than string length.
    if((p2End - p2Begin) > (p1End - p1Begin))
        return p1End;
    // General case.
    const value_type* pSearchEnd = (p1End - (p2End - p2Begin) + 1);
    const value_type* pCurrent1;
    const value_type* pCurrent2;
    while(pSearchEnd != p1Begin)
    {
        // Search for the last occurrence of *p2Begin.
        pCurrent1 = CharTypeStringFindEnd(p1Begin, pSearchEnd, *p2Begin);
        if(pCurrent1 == pSearchEnd) // If the first char of p2 wasn't found,
            return p1End;           // then we immediately have failure.
        // In this case, *pTemp == *p2Begin. So compare the rest.
        pCurrent2 = p2Begin;
        while(*pCurrent1++ == *pCurrent2++)
        {
            if(pCurrent2 == p2End)
                return (pCurrent1 - (p2End - p2Begin));
        }
        // A smarter algorithm might know to subtract more than just one,
        // but in most cases it won't make much difference anyway.
        --pSearchEnd;
    }
    return p1End;
}





Add Discussion as Guest

Log in