strcspn.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (C) 1991-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library 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 GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <string.h>
  15. #include <stdint.h>
  16. #include <libc-pointer-arith.h>
  17. #undef strcspn
  18. #ifndef STRCSPN
  19. # define STRCSPN strcspn
  20. #endif
  21. /* Return the length of the maximum initial segment of S
  22. which contains no characters from REJECT. */
  23. size_t
  24. STRCSPN (const char *str, const char *reject)
  25. {
  26. if (__glibc_unlikely (reject[0] == '\0') ||
  27. __glibc_unlikely (reject[1] == '\0'))
  28. return __strchrnul (str, reject [0]) - str;
  29. /* Use multiple small memsets to enable inlining on most targets. */
  30. unsigned char table[256];
  31. unsigned char *p = memset (table, 0, 64);
  32. memset (p + 64, 0, 64);
  33. memset (p + 128, 0, 64);
  34. memset (p + 192, 0, 64);
  35. unsigned char *s = (unsigned char*) reject;
  36. unsigned char tmp;
  37. do
  38. p[tmp = *s++] = 1;
  39. while (tmp);
  40. s = (unsigned char*) str;
  41. if (p[s[0]]) return 0;
  42. if (p[s[1]]) return 1;
  43. if (p[s[2]]) return 2;
  44. if (p[s[3]]) return 3;
  45. s = (unsigned char *) PTR_ALIGN_DOWN (s, 4);
  46. unsigned int c0, c1, c2, c3;
  47. do
  48. {
  49. s += 4;
  50. c0 = p[s[0]];
  51. c1 = p[s[1]];
  52. c2 = p[s[2]];
  53. c3 = p[s[3]];
  54. }
  55. while ((c0 | c1 | c2 | c3) == 0);
  56. size_t count = s - (unsigned char *) str;
  57. return (c0 | c1) != 0 ? count - c0 + 1 : count - c2 + 3;
  58. }
  59. libc_hidden_builtin_def (strcspn)