strncasecmp.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) 2002 Manuel Novoa III
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include "_string.h"
  8. #include <ctype.h>
  9. #include <locale.h>
  10. #ifdef __UCLIBC_HAS_XLOCALE__
  11. extern int __strncasecmp_l (__const char *__s1, __const char *__s2,
  12. size_t __n, __locale_t __loc)
  13. __THROW __attribute_pure__ __nonnull ((1, 2, 4)) attribute_hidden;
  14. extern int __wcsncasecmp_l (__const wchar_t *__s1, __const wchar_t *__s2,
  15. size_t __n, __locale_t __loc) __THROW attribute_hidden;
  16. #endif
  17. #ifdef WANT_WIDE
  18. # define strncasecmp wcsncasecmp
  19. # define __strncasecmp __wcsncasecmp
  20. # define strncasecmp_l wcsncasecmp_l
  21. # define __strncasecmp_l __wcsncasecmp_l
  22. # ifdef __UCLIBC_DO_XLOCALE
  23. # define TOLOWER(C) __towlower_l((C), locale_arg)
  24. extern wint_t __towlower_l (wint_t __wc, __locale_t __locale) __THROW attribute_hidden;
  25. # else
  26. # define TOLOWER(C) __towlower((C))
  27. # endif
  28. #else
  29. # ifdef __UCLIBC_DO_XLOCALE
  30. # define TOLOWER(C) __tolower_l((C), locale_arg)
  31. # else
  32. # define TOLOWER(C) __tolower((C))
  33. # endif
  34. #endif
  35. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  36. int attribute_hidden __strncasecmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  37. {
  38. return __strncasecmp_l(s1, s2, n, __UCLIBC_CURLOCALE);
  39. }
  40. strong_alias(__strncasecmp,strncasecmp)
  41. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  42. int attribute_hidden __UCXL(strncasecmp)(register const Wchar *s1, register const Wchar *s2,
  43. size_t n __LOCALE_PARAM )
  44. {
  45. #ifdef WANT_WIDE
  46. while (n && ((*s1 == *s2) || (TOLOWER(*s1) == TOLOWER(*s2)))) {
  47. if (!*s1++) {
  48. return 0;
  49. }
  50. ++s2;
  51. --n;
  52. }
  53. return (n == 0)
  54. ? 0
  55. : ((((Wuchar)TOLOWER(*s1)) < ((Wuchar)TOLOWER(*s2))) ? -1 : 1);
  56. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  57. #else
  58. int r = 0;
  59. while ( n
  60. && ((s1 == s2) ||
  61. !(r = ((int)( TOLOWER(*((unsigned char *)s1))))
  62. - TOLOWER(*((unsigned char *)s2))))
  63. && (--n, ++s2, *s1++));
  64. return r;
  65. #endif
  66. }
  67. __UCXL_ALIAS(strncasecmp)
  68. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */