test-strcspn.c 2.1 KB

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