| 55 | #undef strstr |
| 56 | no_builtin("strstr") |
| 57 | FAR char *strstr(FAR const char *haystack, FAR const char *needle) |
| 58 | { |
| 59 | #ifdef CONFIG_ALLOW_MIT_COMPONENTS |
| 60 | FAR const unsigned char *needle_cmp_end; |
| 61 | FAR const unsigned char *i_haystack; |
| 62 | const char needle_first = *needle; |
| 63 | FAR const unsigned char *i_needle; |
| 64 | unsigned long last_haystack_chars; |
| 65 | unsigned long last_needle_chars; |
| 66 | FAR const char *sub_start; |
| 67 | size_t needle_cmp_len; |
| 68 | bool identical = true; |
| 69 | unsigned long mask; |
| 70 | size_t compare_len; |
| 71 | size_t needle_len; |
| 72 | |
| 73 | if (!*needle) |
| 74 | { |
| 75 | return (FAR char *)haystack; |
| 76 | } |
| 77 | |
| 78 | /* Runs strchr() on the first section of the haystack as it has a lower |
| 79 | * algorithmic complexity for discarding the first non-matching characters. |
| 80 | */ |
| 81 | |
| 82 | haystack = strchr(haystack, needle_first); |
| 83 | if (!haystack) /* First character of needle is not in the haystack. */ |
| 84 | { |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | /* First characters of haystack and needle are the same now. Both are |
| 89 | * guaranteed to be at least one character long. |
| 90 | * Now computes the sum of the first needle_len characters of haystack |
| 91 | * minus the sum of characters values of needle. |
| 92 | */ |
| 93 | |
| 94 | i_haystack = (FAR const unsigned char *)haystack + 1; |
| 95 | i_needle = (FAR const unsigned char *)needle + 1; |
| 96 | |
| 97 | while (*i_haystack && *i_needle) |
| 98 | { |
| 99 | identical &= *i_haystack++ == *i_needle++; |
| 100 | } |
| 101 | |
| 102 | /* i_haystack now references the (needle_len + 1)-th character. */ |
| 103 | |
| 104 | if (*i_needle) /* haystack is smaller than needle. */ |
| 105 | { |
| 106 | return NULL; |
| 107 | } |
| 108 | else if (identical) |
| 109 | { |
| 110 | return (FAR char *)haystack; |
| 111 | } |
| 112 | |
| 113 | needle_len = i_needle - (FAR const unsigned char *)needle; |
| 114 |
no test coverage detected