]> git.sesse.net Git - vlc/commitdiff
* src/extras/libc.c: fixed vlc_strncasecmp().
authorGildas Bazin <gbazin@videolan.org>
Wed, 16 Feb 2005 15:34:53 +0000 (15:34 +0000)
committerGildas Bazin <gbazin@videolan.org>
Wed, 16 Feb 2005 15:34:53 +0000 (15:34 +0000)
src/extras/libc.c

index b96ba93e059adf047233bcd1a97ce6f544747df7..dd964c7fd0c0eeeaaf5adb6e57342fdd2ad100bd 100644 (file)
@@ -86,29 +86,20 @@ char *vlc_strndup( const char *string, size_t n )
 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
 int vlc_strcasecmp( const char *s1, const char *s2 )
 {
-    int i_delta = 0;
+    int c1, c2;
     if( !s1 || !s2 ) return  -1;
 
-    while( !i_delta && *s1 && *s2 )
+    while( *s1 && *s2 )
     {
-        i_delta = *s1 - *s2;
-
-        if( *s1 >= 'A' && *s1 <= 'Z' )
-        {
-            i_delta -= ('A' - 'a');
-        }
-
-        if( *s2 >= 'A' && *s2 <= 'Z' )
-        {
-            i_delta += ('A' - 'a');
-        }
+        c1 = tolower(*s1);
+        c2 = tolower(*s2);
 
+        if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
         s1++; s2++;
     }
 
-    if( !i_delta && (*s1 || *s2) ) i_delta = *s1 ? 1 : -1;
-
-    return i_delta;
+    if( !*s1 && !*s2 ) return 0;
+    else return (*s1 ? 1 : -1);
 }
 #endif
 
@@ -118,29 +109,20 @@ int vlc_strcasecmp( const char *s1, const char *s2 )
 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
 {
-    int i_delta = 0;
+    int c1, c2;
     if( !s1 || !s2 ) return  -1;
 
-    while( n-- && !i_delta && *s1 && *s2 )
+    while( n > 0 && *s1 && *s2 )
     {
-        i_delta = *s1 - *s2;
+        c1 = tolower(*s1);
+        c2 = tolower(*s2);
 
-        if( *s1 >= 'A' && *s1 <= 'Z' )
-        {
-            i_delta -= 'A' - 'a';
-        }
-
-        if( *s2 >= 'A' && *s2 <= 'Z' )
-        {
-            i_delta += 'A' - 'a';
-        }
-
-        s1++; s2++;
+        if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
+        s1++; s2++; n--;
     }
 
-    if( !n && !i_delta && (*s1 || *s2) ) i_delta = *s1 ? 1 : -1;
-
-    return i_delta;
+    if( !n || (!*s1 && !*s2) ) return 0;
+    else return (*s1 ? 1 : -1);
 }
 #endif