test-strcspn.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Test strcspn functions.
  2. Copyright (C) 1999-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #define STRPBRK_RESULT(s, pos) (pos)
  16. #define RES_TYPE size_t
  17. #define TEST_MAIN
  18. #ifndef WIDE
  19. # define TEST_NAME "strcspn"
  20. #else
  21. # define TEST_NAME "wcscspn"
  22. #endif /* WIDE */
  23. #include "test-string.h"
  24. #ifndef WIDE
  25. # define STRCSPN strcspn
  26. # define CHAR char
  27. # define SIMPLE_STRCSPN simple_strcspn
  28. # define STUPID_STRCSPN stupid_strcspn
  29. # define STRLEN strlen
  30. #else
  31. # include <wchar.h>
  32. # define STRCSPN wcscspn
  33. # define CHAR wchar_t
  34. # define SIMPLE_STRCSPN simple_wcscspn
  35. # define STUPID_STRCSPN stupid_wcscspn
  36. # define STRLEN wcslen
  37. #endif /* WIDE */
  38. typedef size_t (*proto_t) (const CHAR *, const CHAR *);
  39. size_t SIMPLE_STRCSPN (const CHAR *, const CHAR *);
  40. size_t STUPID_STRCSPN (const CHAR *, const CHAR *);
  41. IMPL (STUPID_STRCSPN, 0)
  42. IMPL (SIMPLE_STRCSPN, 0)
  43. IMPL (STRCSPN, 1)
  44. size_t
  45. SIMPLE_STRCSPN (const CHAR *s, const CHAR *rej)
  46. {
  47. const CHAR *r, *str = s;
  48. CHAR c;
  49. while ((c = *s++) != '\0')
  50. for (r = rej; *r != '\0'; ++r)
  51. if (*r == c)
  52. return s - str - 1;
  53. return s - str - 1;
  54. }
  55. size_t
  56. STUPID_STRCSPN (const CHAR *s, const CHAR *rej)
  57. {
  58. size_t ns = STRLEN (s), nrej = STRLEN (rej);
  59. size_t i, j;
  60. for (i = 0; i < ns; ++i)
  61. for (j = 0; j < nrej; ++j)
  62. if (s[i] == rej[j])
  63. return i;
  64. return i;
  65. }
  66. #undef CHAR
  67. #undef STRLEN
  68. #include "test-strpbrk.c"