memmem.c 2.7 KB

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