/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
+ * Copyright (C) 2018 Han-Wen Nienhuys <hanwen@xs4all.nl>
*
* This file is part of Mes.
*
return 0;
}
+/** locate a substring. #memmem# finds the first occurrence of
+ #needle# in #haystack#. This is not ANSI-C.
+
+ The prototype is not in accordance with the Linux Programmer's
+ Manual v1.15, but it is with /usr/include/string.h */
+
+unsigned char *
+_memmem (unsigned char const *haystack, int haystack_len,
+ unsigned char const *needle, int needle_len)
+{
+ unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
+ unsigned char const *end_needle = needle + needle_len;
+
+ /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
+ is the spice of life */
+ while (haystack < end_haystack)
+ {
+ unsigned char const *subneedle = needle;
+ unsigned char const *subhaystack = haystack;
+ while (subneedle < end_needle)
+ if (*subneedle++ != *subhaystack++)
+ goto next;
+
+ /* Completed the needle. Gotcha. */
+ return (unsigned char *) haystack;
+ next:
+ haystack++;
+ }
+ return 0;
+}
+
+void *
+memmem (void const *haystack, int haystack_len,
+ void const *needle, int needle_len)
+{
+ unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
+ unsigned char const *needle_byte_c = (unsigned char const *)needle;
+ return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
+}
+
char *
strstr (char const *haystack, char const *needle)
{
- eputs ("strstr stub\n");
- return 0;
+ return memmem (haystack, strlen (haystack), needle, strlen (needle));
}
double