Diff: strcasecmp.c 1.2 -> strcasecmp.c 1.3 (fwd)


Subject: Diff: strcasecmp.c 1.2 -> strcasecmp.c 1.3 (fwd)
From: Aaron Lehmann (aaronl@vitelus.com)
Date: Mon Jun 12 2000 - 00:08:10 CDT


---------- Forwarded message ----------
Date: 11 Jun 2000 23:11:32 -0400
From: Jordan Katz <webmaster@underlevel.net>
To: aaronl@vitelus.com
Subject: Diff: strcasecmp.c 1.2 -> strcasecmp.c 1.3

Hi,

 This is a minor change. I hope I can contribute more meaningful
stuff later. The strcasecmp.c 1.2 invoked undefined behavior because
of not casting its argument to an unsigned type. It also didn't
comply with the standard behavior of strcasecmp, which is:

>> From the man page:
  Upon completion, the strcasecmp() function returns an integer whose value
  is greater than, equal to, or less than 0 (zero), according to whether the
  s1 string, ignoring case, is greater than, equal to, or less than the s2
  string.

Also, I used pointer arithmetic which can speed it up on certain systems.

*** strcasecmp-1.2.c Sun Jun 11 23:07:03 2000
--- strcasecmp-1.3.c Sun Jun 11 23:06:08 2000
***************
*** 1,16 ****
! #if !defined(__GLIBC__) || (__GLIBC__ < 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 2)
  
  #include <ctype.h>
  #include <string.h>
  
  int strcasecmp(const char *s1, const char *s2)
! {
! int i;
! if (strlen(s1) != strlen(s1))
! return(1);
! for (i=0;i<strlen(s1);i++)
! if ( toupper(s1[i]) != toupper(s2[i]) )
! return(1);
! return(0);
! }
  #endif
--- 1,24 ----
! #if !defined(__GLIBC__) || (__GLIBC__ < 2) || \
! (__GLIBC__ == 2 && __GLIBC_MINOR__ < 2)
  
  #include <ctype.h>
  #include <string.h>
  
+ /*
+ * Return less than 0 if s1 is smaller than
+ * s2, greater than 0 if s1 is bigger than
+ * s2, and 0 if equal.
  int strcasecmp(const char *s1, const char *s2)
! {
! const unsigned char *ps1 = (const unsigned *)s1,
! *ps2 = (const unsigned *)s2;
!
! while (tolower(*ps1) && tolower(*ps2)) {
! if (*ps1 != *ps2)
! return tolower(*ps1) - tolower(*ps2);
! ++ps1, ++ps2;
! }
!
! return 0;
! }
  #endif

-- 
Jordan Katz <webmaster@underlevel.net>



This archive was generated by hypermail 2b25 : Mon Jun 12 2000 - 00:08:14 CDT