diff --git a/global.c b/global.c index a2304fb..24ba7b7 100644 --- a/global.c +++ b/global.c @@ -29,9 +29,38 @@ #include // external interrupt defines #include "global.h" // common methods +#include "string.h" // memory utilities volatile bool button_flag = false; +void * memmem(const void *l, size_t l_len, const void *s, size_t s_len) +{ + register char *cur, *last; + const char *cl = (const char *)l; + const char *cs = (const char *)s; + + /* we need something to compare */ + if (l_len == 0 || s_len == 0) + return NULL; + + /* "s" must be smaller or equal to "l" */ + if (l_len < s_len) + return NULL; + + /* special case where s_len == 1 */ + if (s_len == 1) + return memchr(l, (int)*cs, l_len); + + /* the last position where its possible to find "s" in "l" */ + last = (char *)cl + l_len - s_len; + + for (cur = (char *)cl; cur <= last; cur++) + if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0) + return cur; + + return NULL; +} + char* b2s(uint64_t binary, uint8_t rjust) { static char string[64+1] = {0}; // the string representation to return diff --git a/global.h b/global.h index 184504f..61976ac 100644 --- a/global.h +++ b/global.h @@ -127,6 +127,17 @@ extern volatile bool button_flag; /**< flag set when board user button has been /** default printf output */ int _write(int file, char *ptr, int len); + +/** find the first occurrence of the byte string s in byte string l + * @copyright 2005 Pascal Gloor + * @param[in] l byte string to find byte substring in + * @param[in] l_len length of l byte string + * @param[in] s byte byte substring to find in byte string + * @param[in] s_len length of s byte string + * @return pointer to the beginning of the substring, or NULL if the substring is not found + */ +void * memmem(const void *l, size_t l_len, const void *s, size_t s_len); + /** get binary representation of a number * @param[in] binary number to represent in binary * @param[in] rjust justify representation with leading zeros