MCPcopy Create free account
hub / github.com/apache/nuttx / strstr

Function strstr

libs/libc/string/lib_strstr.c:57–286  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55#undef strstr
56no_builtin("strstr")
57FAR 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

Callers 15

check_funcptrFunction · 0.85
get_compilerFunction · 0.85
parse_kconfigfileFunction · 0.85
save_original_configFunction · 0.85
get_compilerFunction · 0.85
white_file_listFunction · 0.85
white_content_listFunction · 0.85
mainFunction · 0.85
lwm2mstub_get_handlerFunction · 0.85
select_bssFunction · 0.85
fakesensor_read_gnssFunction · 0.85
goldfish_camera_get_listFunction · 0.85

Calls 4

strchrFunction · 0.70
memcmpFunction · 0.70
strlenFunction · 0.70
strncmpFunction · 0.70

Tested by

no test coverage detected