- mbui_init (rneedle, needle);
- if (!mbui_avail (rneedle))
- abort ();
- mbui_advance (rneedle);
-
- for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
- {
- if (!mbui_avail (rneedle))
- /* Found a match. */
- return (char *) haystack;
- if (!mbui_avail (rhaystack))
- /* No match. */
- return NULL;
- if (!mb_equal (mbui_cur (rhaystack), mbui_cur (rneedle)))
- /* Nothing in this round. */
- break;
- }
- }
- }
- }
- else
- return (char *) haystack;
- }
- else
-#endif
- {
- if (*needle != '\0')
- {
- /* Speed up the following searches of needle by caching its first
- character. */
- char b = *needle++;
-
- for (;; haystack++)
- {
- if (*haystack == '\0')
- /* No match. */
- return NULL;
- if (*haystack == b)
- /* The first character matches. */
- {
- const char *rhaystack = haystack + 1;
- const char *rneedle = needle;
-
- for (;; rhaystack++, rneedle++)
- {
- if (*rneedle == '\0')
- /* Found a match. */
- return (char *) haystack;
- if (*rhaystack == '\0')
- /* No match. */
- return NULL;
- if (*rhaystack != *rneedle)
- /* Nothing in this round. */
- break;
- }
- }
- }
- }
- else
- return (char *) haystack;
- }
+ /* Perform the search. Abstract memory is considered to be an array
+ of 'unsigned char' values, not an array of 'char' values. See
+ ISO C 99 section 6.2.6.1. */
+ if (needle_len < LONG_NEEDLE_THRESHOLD)
+ return two_way_short_needle ((const unsigned char *) haystack,
+ haystack_len,
+ (const unsigned char *) needle, needle_len);
+ return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
+ (const unsigned char *) needle, needle_len);