c-strcasecmp.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* c-strcasecmp.c -- case insensitive string comparator in C locale
  2. Copyright (C) 1998-1999, 2005-2006, 2009-2012 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "c-strcase.h"
  17. #include <limits.h>
  18. #include "c-ctype.h"
  19. int
  20. c_strcasecmp (const char *s1, const char *s2)
  21. {
  22. register const unsigned char *p1 = (const unsigned char *) s1;
  23. register const unsigned char *p2 = (const unsigned char *) s2;
  24. unsigned char c1, c2;
  25. if (p1 == p2)
  26. return 0;
  27. do
  28. {
  29. c1 = c_tolower (*p1);
  30. c2 = c_tolower (*p2);
  31. if (c1 == '\0')
  32. break;
  33. ++p1;
  34. ++p2;
  35. }
  36. while (c1 == c2);
  37. if (UCHAR_MAX <= INT_MAX)
  38. return c1 - c2;
  39. else
  40. /* On machines where 'char' and 'int' are types of the same size, the
  41. difference of two 'unsigned char' values - including the sign bit -
  42. doesn't fit in an 'int'. */
  43. return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
  44. }