4
0

strstr.c 3.1 KB

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