From 009a998876458f1cbab925c56c95b90d48d72157 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 27 Mar 2014 20:03:33 +1300 Subject: Added a bunch of things. This is the epitome of a bad commit message --- string/common.c | 33 +++++++++++++++++++++++++++------ string/common.h | 1 + 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'string') 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 diff --git a/string/common.h b/string/common.h index 82ad4b1..1ca441b 100644 --- a/string/common.h +++ b/string/common.h @@ -22,6 +22,7 @@ uint64_t strlen(const char *string); bool itoa(uint32_t num, char *buffer, uint8_t base); uint32_t atoi(const char *string); +bool string_contains(char *haystack, char *needle); void memcpy(void *to, const void *from, uint32_t size); void memset(void *to, uint8_t value, uint32_t size); -- cgit v1.1