summaryrefslogtreecommitdiff
path: root/src/string/strstr.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-02-08 18:42:39 -0500
committerJakob Kaivo <jkk@ung.org>2019-02-08 18:42:39 -0500
commit7ef8a7379f7f7d09e71ccae2a0b688c3cd80423f (patch)
tree092ab0aed1769117fd7b28b8592f6f96b0e0d5af /src/string/strstr.c
parent6acf19370e8adff79cd83b257d3f04aeaf2a59dd (diff)
merge sources into single tree
Diffstat (limited to 'src/string/strstr.c')
-rw-r--r--src/string/strstr.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/string/strstr.c b/src/string/strstr.c
new file mode 100644
index 00000000..898db1be
--- /dev/null
+++ b/src/string/strstr.c
@@ -0,0 +1,43 @@
+#include <string.h>
+#include "nonstd/assert.h"
+
+/** search for substring **/
+char * strstr(const char *s1, const char *s2)
+{
+ size_t l1 = 0;
+ size_t l2 = 0;
+ char *p = (char*)s1;
+
+ ASSERT_NONNULL(s1);
+ ASSERT_NONNULL(s2);
+
+ l2 = strlen(s2);
+ if (l2 == 0) {
+ return p;
+ }
+
+ l1 = strlen(s1);
+
+ do {
+ p = memchr(p, *s2, l1);
+ if (p == NULL || strcmp(p + 1, s2 + 1) == 0) {
+ break;
+ }
+ p++;
+ } while (p < s1 + l2);
+
+ /*
+ RETURN_FAILURE(CONSTANT(NULL));
+ RETURN_SUCCESS(a pointer to the located string);
+ */
+ return p;
+}
+
+/***
+finds the first occurrence of the string ARGUMENT(s2) in the string
+ARGUMENT(s1). Specifying the empty string for ARGUMENT(s2) matches the first
+character of ARGUMENT(s1).
+***/
+/*
+STDC(1)
+*/