CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Nov 2001
    Posts
    251

    case insensitive strstr for TCHARs

    Apparently, the runtime library doesn't have a case insensitive strstr function, so I had to write one myself.

    I took the old one and just replaced everything with TCHARs.
    Is there anything inheritly wrong with it?

    I don't think it matters if I use _totlower or _totupper?

    The SDK docs says:
    "In order for tolower to give the expected results, __isascii and isupper must both return nonzero."

    I tested it with some non-ASCII characters, and tolower returned the same character, so I hope that's ok.

    Code:
    TCHAR *_tcsistr( const TCHAR * str1,
                     const TCHAR * str2 ) {
    
     TCHAR *cp = (TCHAR *)str1;
     TCHAR *s1, *s2;
    
     if (!*str2) {
        return (TCHAR *)str1;
     }
    
     while (*cp) {
        s1 = cp;
        s2 = (TCHAR *)str2;
    
        // Matching characters will return zero if they are subtracted
    
        while ( *s1 && *s2 && !(_totlower(*s1) - _totlower(*s2)) ) {
           s1++, s2++;
        }
    
        if (!*s2) {
           return cp;
        }
    
        cp++;
     }
    
     return NULL;
    }

    Original runtime function:

    Code:
    char * __cdecl strstr (
            const char * str1,
            const char * str2
            )
    {
            char *cp = (char *) str1;
            char *s1, *s2;
    
            if ( !*str2 )
                return((char *)str1);
    
            while (*cp)
            {
                    s1 = cp;
                    s2 = (char *) str2;
    
                    while ( *s1 && *s2 && !(*s1-*s2) )
                            s1++, s2++;
    
                    if (!*s2)
                            return(cp);
    
                    cp++;
            }
    
            return(NULL);
    
    }
    Last edited by Syslock; May 23rd, 2010 at 04:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured