c-strcasecmp.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* c-strcasecmp.c -- case insensitive string comparator in C locale
  2. Copyright (C) 1998-1999, 2005-2006, 2009-2024 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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, see <https://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include "c-strcase.h"
  16. #include <limits.h>
  17. #include "c-ctype.h"
  18. int
  19. c_strcasecmp (const char *s1, const char *s2)
  20. {
  21. register const unsigned char *p1 = (const unsigned char *) s1;
  22. register const unsigned char *p2 = (const unsigned char *) s2;
  23. unsigned char c1, c2;
  24. if (p1 == p2)
  25. return 0;
  26. do
  27. {
  28. c1 = c_tolower (*p1);
  29. c2 = c_tolower (*p2);
  30. if (c1 == '\0')
  31. break;
  32. ++p1;
  33. ++p2;
  34. }
  35. while (c1 == c2);
  36. if (UCHAR_MAX <= INT_MAX)
  37. return c1 - c2;
  38. else
  39. /* On machines where 'char' and 'int' are types of the same size, the
  40. difference of two 'unsigned char' values - including the sign bit -
  41. doesn't fit in an 'int'. */
  42. return _GL_CMP (c1, c2);
  43. }