diff --git a/string/memmem.c b/string/memmem.c
index c17e1cf6a63c0709df0c1c7a45410c5178b2876e..43efaa3fb718e7a22c3b4122c278720768644d0f 100644
--- a/string/memmem.c
+++ b/string/memmem.c
@@ -31,6 +31,7 @@
Â 
Â #define RETURN_TYPE void *
Â #define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
+#define FASTSEARCH(S,C,N) (void*) memchr ((void *)(S), (C), (N))
Â #include "str-two-way.h"
Â 
Â #undef memmem
diff --git a/string/str-two-way.h b/string/str-two-way.h
index cd2605857d9b5c432c3c2b1c5e35f55d8e9285b6..6ce6798ea4d595bb4c95335233eb92595f447a28 100644
--- a/string/str-two-way.h
+++ b/string/str-two-way.h
@@ -281,50 +281,50 @@ two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
Â Â Â Â  }
Â Â  else
Â Â Â Â  {
-Â Â Â Â Â  const unsigned char *phaystack = &haystack[suffix];
+Â Â Â Â Â  const unsigned char *phaystack;
Â Â Â Â Â Â  /* The comparison always starts from needle[suffix], so cache it
Â Â Â Â Â Â Â Â Â  and use an optimized first-character loop.Â  */
Â Â Â Â Â Â  unsigned char needle_suffix = CANON_ELEMENT (needle[suffix]);
Â 
-#if CHECK_EOL
-Â Â Â Â Â  /* We start matching from the SUFFIX'th element, so make sure we
-Â Â Â Â Â Â Â  don't hit '\0' before that.Â  */
-Â Â Â Â Â  if (haystack_len < suffix + 1
-Â Â Â Â Â Â Â Â  && !AVAILABLE (haystack, haystack_len, 0, suffix + 1))
-Â Â Â Â Â Â  return NULL;
-#endif
-
Â Â Â Â Â Â  /* The two halves of needle are distinct; no extra memory is
Â Â Â Â Â Â Â Â Â  required, and any mismatch results in a maximal shift.Â  */
Â Â Â Â Â Â  period = MAX (suffix, needle_len - suffix) + 1;
Â Â Â Â Â Â  j = 0;
-Â Â Â Â Â  while (1
-#if !CHECK_EOL
-Â Â Â Â Â Â Â Â Â Â Â  && AVAILABLE (haystack, haystack_len, j, needle_len)
-#endif
-Â Â Â Â Â Â Â Â Â Â Â  )
+Â Â Â Â Â  while (AVAILABLE (haystack, haystack_len, j, needle_len))
Â Â Â Â Â Â Â Â  {
Â Â Â Â Â Â Â Â Â Â  unsigned char haystack_char;
Â Â Â Â Â Â Â Â Â Â  const unsigned char *pneedle;
Â 
-Â Â Â Â Â Â Â Â  /* TODO: The first-character loop can be sped up by adapting
-Â Â Â Â Â Â Â Â Â Â Â  longword-at-a-time implementation of memchr/strchr.Â  */
-Â Â Â Â Â Â Â Â  if (needle_suffix
+Â Â Â Â Â Â Â Â  phaystack = &haystack[suffix + j];
+
+#ifdef FASTSEARCH
+Â Â Â Â Â Â Â Â  if (*phaystack++ != needle_suffix)
+Â Â Â Â Â Â Â Â Â Â  {
+Â Â Â Â Â Â Â Â Â Â Â Â  phaystack = FASTSEARCH (phaystack, needle_suffix,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  haystack_len - needle_len - j);
+Â Â Â Â Â Â Â Â Â Â Â Â  if (phaystack == NULL)
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â  goto ret0;
+Â Â Â Â Â Â Â Â Â Â Â Â  j = phaystack - &haystack[suffix];
+Â Â Â Â Â Â Â Â Â Â Â Â  phaystack++;
+Â Â Â Â Â Â Â Â Â Â  }
+#else
+Â Â Â Â Â Â Â Â  while (needle_suffix
Â Â Â Â Â Â Â Â Â Â Â Â Â Â  != (haystack_char = CANON_ELEMENT (*phaystack++)))
Â Â Â Â Â Â Â Â Â Â Â Â  {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â  RET0_IF_0 (haystack_char);
-#if !CHECK_EOL
+# if !CHECK_EOL
Â Â Â Â Â Â Â Â Â Â Â Â Â Â  ++j;
-#endif
-Â Â Â Â Â Â Â Â Â Â Â Â  continue;
+Â Â Â Â Â Â Â Â Â Â Â Â  if (!AVAILABLE (haystack, haystack_len, j, needle_len))
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â  goto ret0;
+# endif
Â Â Â Â Â Â Â Â Â Â Â Â  }
Â 
-#if CHECK_EOL
+# if CHECK_EOL
Â Â Â Â Â Â Â Â Â Â  /* Calculate J if it wasn't kept up-to-date in the first-character
Â Â Â Â Â Â Â Â Â Â Â Â Â  loop.Â  */
Â Â Â Â Â Â Â Â Â Â  j = phaystack - &haystack[suffix] - 1;
+# endif
Â #endif
-
Â Â Â Â Â Â Â Â Â Â  /* Scan for matches in right half.Â  */
Â Â Â Â Â Â Â Â Â Â  i = suffix + 1;
Â Â Â Â Â Â Â Â Â Â  pneedle = &needle[i];
@@ -338,6 +338,11 @@ two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }
Â Â Â Â Â Â Â Â Â Â Â Â Â Â  ++i;
Â Â Â Â Â Â Â Â Â Â Â Â  }
+#if CHECK_EOL
+Â Â Â Â Â Â Â Â  /* Update minimal length of haystack.Â  */
+Â Â Â Â Â Â Â Â  if (phaystack > haystack + haystack_len)
+Â Â Â Â Â Â Â Â Â Â  haystack_len = phaystack - haystack;
+#endif
Â Â Â Â Â Â Â Â Â Â  if (needle_len <= i)
Â Â Â Â Â Â Â Â Â Â Â Â  {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â  /* Scan for matches in left half.Â  */
@@ -360,13 +365,6 @@ two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
Â Â Â Â Â Â Â Â Â Â Â Â  }
Â Â Â Â Â Â Â Â Â Â  else
Â Â Â Â Â Â Â Â Â Â Â Â  j += i - suffix + 1;
-
-#if CHECK_EOL
-Â Â Â Â Â Â Â Â  if (!AVAILABLE (haystack, haystack_len, j, needle_len))
-Â Â Â Â Â Â Â Â Â Â  break;
-#endif
-
-Â Â Â Â Â Â Â Â  phaystack = &haystack[suffix + j];
Â Â Â Â Â Â Â Â  }
Â Â Â Â  }
Â  ret0: __attribute__ ((unused))
diff --git a/string/strcasestr.c b/string/strcasestr.c
index 90ba189790481a58eb7627eb8c396530f988f985..d54c4b56853735fb8f5ebd59419b222ca8827651 100644
--- a/string/strcasestr.c
+++ b/string/strcasestr.c
@@ -37,8 +37,8 @@
Â /* Two-Way algorithm.Â  */
Â #define RETURN_TYPE char *
Â #define AVAILABLE(h, h_l, j, n_l)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  \
-Â  (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l))Â Â Â  \
-Â Â  && ((h_l) = (j) + (n_l)))
+Â  (((j) + (n_l) <= (h_l)) || ((h_l) += strnlen ((void*)((h) + (h_l)), 512), \
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  (j) + (n_l) <= (h_l)))
Â #define CHECK_EOL (1)
Â #define RET0_IF_0(a) if (!a) goto ret0
Â #define CANON_ELEMENT(c) TOLOWER (c)
diff --git a/string/strstr.c b/string/strstr.c
index b3b5deb673758510616d32849bcf2fc15ce941cd..346f303c7423c95de0fbcdeb1a9f31293b303372 100644
--- a/string/strstr.c
+++ b/string/strstr.c
@@ -33,10 +33,11 @@
Â 
Â #define RETURN_TYPE char *
Â #define AVAILABLE(h, h_l, j, n_l)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  \
-Â  (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l))Â Â Â  \
-Â Â  && ((h_l) = (j) + (n_l)))
+Â  (((j) + (n_l) <= (h_l)) || ((h_l) += strnlen ((void*)((h) + (h_l)), 512), \
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  (j) + (n_l) <= (h_l)))
Â #define CHECK_EOL (1)
Â #define RET0_IF_0(a) if (!a) goto ret0
+#define FASTSEARCH(S,C,N) (void*) strchr ((void*)(S), (C))
Â #include "str-two-way.h"
Â 
Â #undef strstr
