c-strncasecmp.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* c-strncasecmp.c -- case insensitive string comparator in C locale
  2. Copyright (C) 1998-1999, 2005-2006, 2009-2013 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 General Public License as published by
  5. the Free Software Foundation; either version 3, 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://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_strncasecmp (const char *s1, const char *s2, size_t n)
  20. {
  21. const unsigned char *p1 = (const unsigned char *) s1;
  22. const unsigned char *p2 = (const unsigned char *) s2;
  23. unsigned char c1, c2;
  24. if (p1 == p2 || n == 0)
  25. return 0;
  26. do
  27. {
  28. c1 = c_tolower (*p1);
  29. c2 = c_tolower (*p2);
  30. if (--n == 0 || 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 (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
  43. }