aboutsummaryrefslogtreecommitdiff
path: root/string/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'string/common.c')
-rw-r--r--string/common.c33
1 files changed, 27 insertions, 6 deletions
diff --git a/string/common.c b/string/common.c
index b9adc3e..5e9f8b1 100644
--- a/string/common.c
+++ b/string/common.c
@@ -60,15 +60,36 @@ uint32_t atoi(const char *string)
//strcmp()
// UNFINISHED
-/*bool string_contains(char *haystack, char *needle)
+
+// HAYSTACK:
+// foo bar hello woo
+
+// NEEDLE:
+// hello
+bool string_contains(char *haystack, char *needle)
{
- return FALSE;
uint32_t i;
- for (i = strlen(haystack)-(strlen(needle)+1); i >= 0; i--)
+ uint32_t j;
+ uint32_t hlen = strlen(haystack);
+ uint32_t nlen = strlen(needle);
+
+ // If needle is longer than haystack, it can't be in there!
+ if (strlen(needle) > strlen(haystack))
+ return FALSE;
+
+ for (i = 0; i < hlen; i++)
{
- // if ()
+ for (j = 0; j < nlen; j++)
+ {
+ if (haystack[j+i] != needle[j])
+ break;
+ }
+ // Reached end of haystack without mismatch, we've found the needle!
+ if (j == nlen)
+ return TRUE;
}
-}*/
+ return FALSE;
+}
void memcpy(void *to, const void *from, uint32_t size)
{
@@ -90,4 +111,4 @@ void memset(void *to, uint8_t value, uint32_t size)
t[i] = value;
}
-#endif
+#endif \ No newline at end of file