strstr.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2013 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. #ifndef _LIBC
  22. # define __builtin_expect(expr, val) (expr)
  23. #endif
  24. #define RETURN_TYPE char *
  25. #define AVAILABLE(h, h_l, j, n_l) \
  26. (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
  27. && ((h_l) = (j) + (n_l)))
  28. #include "str-two-way.h"
  29. /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
  30. if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
  31. HAYSTACK. */
  32. char *
  33. strstr (const char *haystack_start, const char *needle_start)
  34. {
  35. const char *haystack = haystack_start;
  36. const char *needle = needle_start;
  37. size_t needle_len; /* Length of NEEDLE. */
  38. size_t haystack_len; /* Known minimum length of HAYSTACK. */
  39. bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
  40. /* Determine length of NEEDLE, and in the process, make sure
  41. HAYSTACK is at least as long (no point processing all of a long
  42. NEEDLE if HAYSTACK is too short). */
  43. while (*haystack && *needle)
  44. ok &= *haystack++ == *needle++;
  45. if (*needle)
  46. return NULL;
  47. if (ok)
  48. return (char *) haystack_start;
  49. /* Reduce the size of haystack using strchr, since it has a smaller
  50. linear coefficient than the Two-Way algorithm. */
  51. needle_len = needle - needle_start;
  52. haystack = strchr (haystack_start + 1, *needle_start);
  53. if (!haystack || __builtin_expect (needle_len == 1, 0))
  54. return (char *) haystack;
  55. needle -= needle_len;
  56. haystack_len = (haystack > haystack_start + needle_len ? 1
  57. : needle_len + haystack_start - haystack);
  58. /* Perform the search. Abstract memory is considered to be an array
  59. of 'unsigned char' values, not an array of 'char' values. See
  60. ISO C 99 section 6.2.6.1. */
  61. if (needle_len < LONG_NEEDLE_THRESHOLD)
  62. return two_way_short_needle ((const unsigned char *) haystack,
  63. haystack_len,
  64. (const unsigned char *) needle, needle_len);
  65. return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
  66. (const unsigned char *) needle, needle_len);
  67. }
  68. #undef LONG_NEEDLE_THRESHOLD