memmem.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright (C) 1991-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <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 memmem. */
  19. #include <string.h>
  20. #ifndef _LIBC
  21. # define __builtin_expect(expr, val) (expr)
  22. # define __memmem memmem
  23. #endif
  24. #define RETURN_TYPE void *
  25. #define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
  26. #define FASTSEARCH(S,C,N) (void*) memchr ((void *)(S), (C), (N))
  27. #include "str-two-way.h"
  28. #undef memmem
  29. /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
  30. if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in
  31. HAYSTACK. */
  32. void *
  33. __memmem (const void *haystack_start, size_t haystack_len,
  34. const void *needle_start, size_t needle_len)
  35. {
  36. /* Abstract memory is considered to be an array of 'unsigned char' values,
  37. not an array of 'char' values. See ISO C 99 section 6.2.6.1. */
  38. const unsigned char *haystack = (const unsigned char *) haystack_start;
  39. const unsigned char *needle = (const unsigned char *) needle_start;
  40. if (needle_len == 0)
  41. /* The first occurrence of the empty string is deemed to occur at
  42. the beginning of the string. */
  43. return (void *) haystack;
  44. /* Sanity check, otherwise the loop might search through the whole
  45. memory. */
  46. if (__glibc_unlikely (haystack_len < needle_len))
  47. return NULL;
  48. /* Use optimizations in memchr when possible, to reduce the search
  49. size of haystack using a linear algorithm with a smaller
  50. coefficient. However, avoid memchr for long needles, since we
  51. can often achieve sublinear performance. */
  52. if (needle_len < LONG_NEEDLE_THRESHOLD)
  53. {
  54. haystack = memchr (haystack, *needle, haystack_len);
  55. if (!haystack || __builtin_expect (needle_len == 1, 0))
  56. return (void *) haystack;
  57. haystack_len -= haystack - (const unsigned char *) haystack_start;
  58. if (haystack_len < needle_len)
  59. return NULL;
  60. /* Check whether we have a match. This improves performance since we
  61. avoid the initialization overhead of the two-way algorithm. */
  62. if (memcmp (haystack, needle, needle_len) == 0)
  63. return (void *) haystack;
  64. return two_way_short_needle (haystack, haystack_len, needle, needle_len);
  65. }
  66. else
  67. return two_way_long_needle (haystack, haystack_len, needle, needle_len);
  68. }
  69. libc_hidden_def (__memmem)
  70. weak_alias (__memmem, memmem)
  71. libc_hidden_weak (memmem)
  72. #undef LONG_NEEDLE_THRESHOLD