优化
This commit is contained in:
+1
-2
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <str_util.h>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef INCLUDED_STR_UTIL_H
|
||||
#define INCLUDED_STR_UTIL_H
|
||||
|
||||
#ifndef INCLUDED_OS_TYPES_H
|
||||
#include <os_types.h>
|
||||
#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*/
|
||||
Reference in New Issue
Block a user