strstr.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2017 Free Software
  2. Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, see <http://www.gnu.org/licenses/>. */
  14. /* This particular implementation was written by Eric Blake, 2008. */
  15. #ifndef _LIBC
  16. # include <config.h>
  17. #endif
  18. /* Specification of strstr. */
  19. #include <string.h>
  20. #include <stdbool.h>
  21. #define RETURN_TYPE char *
  22. #define AVAILABLE(h, h_l, j, n_l) \
  23. (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
  24. && ((h_l) = (j) + (n_l)))
  25. #include "str-two-way.h"
  26. /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
  27. if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
  28. HAYSTACK. */
  29. char *
  30. strstr (const char *haystack_start, const char *needle_start)
  31. {
  32. const char *haystack = haystack_start;
  33. const char *needle = needle_start;
  34. size_t needle_len; /* Length of NEEDLE. */
  35. size_t haystack_len; /* Known minimum length of HAYSTACK. */
  36. bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
  37. /* Determine length of NEEDLE, and in the process, make sure
  38. HAYSTACK is at least as long (no point processing all of a long
  39. NEEDLE if HAYSTACK is too short). */
  40. while (*haystack && *needle)
  41. ok &= *haystack++ == *needle++;
  42. if (*needle)
  43. return NULL;
  44. if (ok)
  45. return (char *) haystack_start;
  46. /* Reduce the size of haystack using strchr, since it has a smaller
  47. linear coefficient than the Two-Way algorithm. */
  48. needle_len = needle - needle_start;
  49. haystack = strchr (haystack_start + 1, *needle_start);
  50. if (!haystack || __builtin_expect (needle_len == 1, 0))
  51. return (char *) haystack;
  52. needle -= needle_len;
  53. haystack_len = (haystack > haystack_start + needle_len ? 1
  54. : needle_len + haystack_start - haystack);
  55. /* Perform the search. Abstract memory is considered to be an array
  56. of 'unsigned char' values, not an array of 'char' values. See
  57. ISO C 99 section 6.2.6.1. */
  58. if (needle_len < LONG_NEEDLE_THRESHOLD)
  59. return two_way_short_needle ((const unsigned char *) haystack,
  60. haystack_len,
  61. (const unsigned char *) needle, needle_len);
  62. return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
  63. (const unsigned char *) needle, needle_len);
  64. }
  65. #undef LONG_NEEDLE_THRESHOLD