----------------------------------------------------------------------------- Finds a string in another string with a case insensitive test w/ length validation -----------------------------------------------------------------------------
| 59 | // Finds a string in another string with a case insensitive test w/ length validation |
| 60 | //----------------------------------------------------------------------------- |
| 61 | char const* V_strnistr( char const* pStr, char const* pSearch, int n ) |
| 62 | { |
| 63 | Assert( pStr != NULL ); |
| 64 | Assert( pSearch != NULL ); |
| 65 | |
| 66 | if ( pStr == NULL || pSearch == NULL ) |
| 67 | return 0; |
| 68 | |
| 69 | char const* pLetter = pStr; |
| 70 | |
| 71 | // Check the entire string |
| 72 | while ( *pLetter != 0 ) |
| 73 | { |
| 74 | if ( n <= 0 ) |
| 75 | return 0; |
| 76 | |
| 77 | // Skip over non-matches |
| 78 | if ( tolower( *pLetter ) == tolower( *pSearch ) ) |
| 79 | { |
| 80 | int n1 = n - 1; |
| 81 | |
| 82 | // Check for match |
| 83 | char const* pMatch = pLetter + 1; |
| 84 | char const* pTest = pSearch + 1; |
| 85 | while (*pTest != 0) |
| 86 | { |
| 87 | if ( n1 <= 0 ) |
| 88 | return 0; |
| 89 | |
| 90 | // We've run off the end; don't bother. |
| 91 | if (*pMatch == 0) |
| 92 | return 0; |
| 93 | |
| 94 | if ( tolower( *pMatch ) != tolower( *pTest ) ) |
| 95 | break; |
| 96 | |
| 97 | ++pMatch; |
| 98 | ++pTest; |
| 99 | --n1; |
| 100 | } |
| 101 | |
| 102 | // Found a match! |
| 103 | if (*pTest == 0) |
| 104 | return pLetter; |
| 105 | } |
| 106 | |
| 107 | ++pLetter; |
| 108 | --n; |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | const char* V_strnchr( const char* pStr, char c, int n ) |
| 115 | { |
no outgoing calls
no test coverage detected