memmem.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2011 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, 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 memmem. */
  20. #include <string.h>
  21. #ifndef _LIBC
  22. # define __builtin_expect(expr, val) (expr)
  23. #endif
  24. #define RETURN_TYPE void *
  25. #define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
  26. #include "str-two-way.h"
  27. /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
  28. if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in
  29. HAYSTACK. */
  30. void *
  31. memmem (const void *haystack_start, size_t haystack_len,
  32. const void *needle_start, size_t needle_len)
  33. {
  34. /* Abstract memory is considered to be an array of 'unsigned char' values,
  35. not an array of 'char' values. See ISO C 99 section 6.2.6.1. */
  36. const unsigned char *haystack = (const unsigned char *) haystack_start;
  37. const unsigned char *needle = (const unsigned char *) needle_start;
  38. if (needle_len == 0)
  39. /* The first occurrence of the empty string is deemed to occur at
  40. the beginning of the string. */
  41. return (void *) haystack;
  42. /* Sanity check, otherwise the loop might search through the whole
  43. memory. */
  44. if (__builtin_expect (haystack_len < needle_len, 0))
  45. return NULL;
  46. /* Use optimizations in memchr when possible, to reduce the search
  47. size of haystack using a linear algorithm with a smaller
  48. coefficient. However, avoid memchr for long needles, since we
  49. can often achieve sublinear performance. */
  50. if (needle_len < LONG_NEEDLE_THRESHOLD)
  51. {
  52. haystack = memchr (haystack, *needle, haystack_len);
  53. if (!haystack || __builtin_expect (needle_len == 1, 0))
  54. return (void *) haystack;
  55. haystack_len -= haystack - (const unsigned char *) haystack_start;
  56. if (haystack_len < needle_len)
  57. return NULL;
  58. return two_way_short_needle (haystack, haystack_len, needle, needle_len);
  59. }
  60. else
  61. return two_way_long_needle (haystack, haystack_len, needle, needle_len);
  62. }
  63. #undef LONG_NEEDLE_THRESHOLD