strcasestr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Return the offset of one string within another.
  2. Copyright (C) 1994-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. /*
  16. * My personal strstr() implementation that beats most other algorithms.
  17. * Until someone tells me otherwise, I assume that this is the
  18. * fastest implementation of strstr() in C.
  19. * I deliberately chose not to comment it. You should have at least
  20. * as much fun trying to understand it, as I had to write it :-).
  21. *
  22. * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
  23. /* Specification. */
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <stdbool.h>
  27. #include <strings.h>
  28. #define TOLOWER(Ch) tolower (Ch)
  29. /* Two-Way algorithm. */
  30. #define RETURN_TYPE char *
  31. #define AVAILABLE(h, h_l, j, n_l) \
  32. (((j) + (n_l) <= (h_l)) \
  33. || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
  34. (j) + (n_l) <= (h_l)))
  35. #define CHECK_EOL (1)
  36. #define RET0_IF_0(a) if (!a) goto ret0
  37. #define CANON_ELEMENT(c) TOLOWER (c)
  38. #define CMP_FUNC(p1, p2, l) \
  39. __strncasecmp ((const char *) (p1), (const char *) (p2), l)
  40. #include "str-two-way.h"
  41. #undef strcasestr
  42. #undef __strcasestr
  43. #ifndef STRCASESTR
  44. #define STRCASESTR __strcasestr
  45. #endif
  46. /* Find the first occurrence of NEEDLE in HAYSTACK, using
  47. case-insensitive comparison. This function gives unspecified
  48. results in multibyte locales. */
  49. char *
  50. STRCASESTR (const char *haystack, const char *needle)
  51. {
  52. size_t needle_len; /* Length of NEEDLE. */
  53. size_t haystack_len; /* Known minimum length of HAYSTACK. */
  54. /* Handle empty NEEDLE special case. */
  55. if (needle[0] == '\0')
  56. return (char *) haystack;
  57. /* Ensure HAYSTACK length is at least as long as NEEDLE length.
  58. Since a match may occur early on in a huge HAYSTACK, use strnlen
  59. and read ahead a few cachelines for improved performance. */
  60. needle_len = strlen (needle);
  61. haystack_len = __strnlen (haystack, needle_len + 256);
  62. if (haystack_len < needle_len)
  63. return NULL;
  64. /* Perform the search. Abstract memory is considered to be an array
  65. of 'unsigned char' values, not an array of 'char' values. See
  66. ISO C 99 section 6.2.6.1. */
  67. if (needle_len < LONG_NEEDLE_THRESHOLD)
  68. return two_way_short_needle ((const unsigned char *) haystack,
  69. haystack_len,
  70. (const unsigned char *) needle,
  71. needle_len);
  72. return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
  73. (const unsigned char *) needle,
  74. needle_len);
  75. }
  76. #undef LONG_NEEDLE_THRESHOLD
  77. #ifndef NO_ALIAS
  78. weak_alias (__strcasestr, strcasestr)
  79. #endif