diff --git a/AppKit/fifo.c b/AppKit/fifo.c index a24e210..47b721b 100644 --- a/AppKit/fifo.c +++ b/AppKit/fifo.c @@ -35,8 +35,7 @@ os_err_t fifo_get(fifo_t* self, uint8_t* data){ *data = self->buffer[read_idx]; } - os_size_t next_read_idx = fifo_next(self, self->read_idx); - self->read_idx = next_read_idx; + self->read_idx = fifo_next(self, self->read_idx); return OS_ERR_OK; } diff --git a/AppKit/str_util.c b/AppKit/str_util.c new file mode 100644 index 0000000..f51a433 --- /dev/null +++ b/AppKit/str_util.c @@ -0,0 +1,33 @@ +#include + + +const char* str_util_strnstr(const char* haystack, const char* needle, os_size_t needle_len) { + if (needle_len == 0) return (char*)haystack; + if (!haystack || !needle || haystack[0]=='\0') return NULL; + + os_size_t haystack_len = strlen(haystack); + if (haystack_len < needle_len) return NULL; + + for (os_size_t i = 0; i <= haystack_len - needle_len; i++) { + if (memcmp(haystack + i, needle, needle_len) == 0) { + return haystack + i; + } + } + return NULL; +} + +const char* str_util_strstr(const char* haystack, const char* needle) { + if (!haystack || !needle || haystack[0]=='\0' || needle[0]=='\0') return NULL; + + os_size_t needle_len = strlen(needle); + os_size_t haystack_len = strlen(haystack); + if (haystack_len < needle_len) return NULL; + + for (os_size_t i = 0; i <= haystack_len - needle_len; i++) { + if (memcmp(haystack + i, needle, needle_len) == 0) { + return haystack + i; + } + } + return NULL; +} + diff --git a/AppKit/str_util.h b/AppKit/str_util.h new file mode 100644 index 0000000..3a3fb81 --- /dev/null +++ b/AppKit/str_util.h @@ -0,0 +1,28 @@ +#ifndef INCLUDED_STR_UTIL_H +#define INCLUDED_STR_UTIL_H + +#ifndef INCLUDED_OS_TYPES_H +#include +#endif /*INCLUDED_OS_TYPES_H*/ + +/* ------------------------------------------------------------------------------------------------------------------ */ +/* */ + +/** + * + * @param haystack + * @param needle + * @return + */ +const char* str_util_strstr(const char* haystack, const char* needle); + +/** + * + * @param haystack + * @param needle + * @param needle_len + * @return + */ +const char* str_util_strnstr(const char* haystack, const char* needle, os_size_t needle_len); + +#endif /*INCLUDED_STR_UTIL_H*/