strings.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <assert.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <strings.h>
  10. extern "C" {
  11. void bzero(void* dest, size_t n)
  12. {
  13. memset(dest, 0, n);
  14. }
  15. void bcopy(const void* src, void* dest, size_t n)
  16. {
  17. memmove(dest, src, n);
  18. }
  19. static char foldcase(char ch)
  20. {
  21. if (isalpha(ch))
  22. return tolower(ch);
  23. return ch;
  24. }
  25. int strcasecmp(const char* s1, const char* s2)
  26. {
  27. for (; foldcase(*s1) == foldcase(*s2); ++s1, ++s2) {
  28. if (*s1 == 0)
  29. return 0;
  30. }
  31. return foldcase(*(const unsigned char*)s1) < foldcase(*(const unsigned char*)s2) ? -1 : 1;
  32. }
  33. int strncasecmp(const char* s1, const char* s2, size_t n)
  34. {
  35. if (!n)
  36. return 0;
  37. do {
  38. if (foldcase(*s1) != foldcase(*s2++))
  39. return foldcase(*(const unsigned char*)s1) - foldcase(*(const unsigned char*)--s2);
  40. if (*s1++ == 0)
  41. break;
  42. } while (--n);
  43. return 0;
  44. }
  45. }