|
|
@@ -0,0 +1,160 @@
|
|
|
+#include "string.h"
|
|
|
+#include "../kernel/debug.h"
|
|
|
+#include "../kernel/global.h"
|
|
|
+
|
|
|
+// 函数: _memset
|
|
|
+// 作用: 将dst_起始的size个字节设置为value
|
|
|
+// 参数: dst_ 为目标地址, value 为设置的值, size 为设置的字节数
|
|
|
+// 返回: 无
|
|
|
+void _memset(void *dst_, uint8_t value, uint32_t size)
|
|
|
+{
|
|
|
+ ASSERT(dst_ != NULL);
|
|
|
+ uint8_t *dst = (uint8_t *)dst_;
|
|
|
+ while (size--)
|
|
|
+ {
|
|
|
+ *dst++ = value;
|
|
|
+ }
|
|
|
+}
|
|
|
+// 函数: _memcpy
|
|
|
+// 作用: 将src_起始的size个字节复制到dst_起始地址
|
|
|
+// 参数: dst_ 为目标地址, src_ 为源地址, size 为复制的字节数
|
|
|
+// 返回: 无
|
|
|
+void _memcpy(void *dst_, const void *src_, uint32_t size)
|
|
|
+{
|
|
|
+ ASSERT(dst_ != NULL && src_ != NULL);
|
|
|
+ uint8_t *dst = (uint8_t *)dst_;
|
|
|
+ const uint8_t *src = (const uint8_t *)src_;
|
|
|
+ while (size--)
|
|
|
+ {
|
|
|
+ *dst++ = *src++;
|
|
|
+ }
|
|
|
+}
|
|
|
+// 函数: _memcmp
|
|
|
+// 作用: 比较两个内存区域的内容
|
|
|
+// 参数: a_ 为比较的地址1, b_ 为比较的地址2, size 为比较的字节数
|
|
|
+// 返回: 相等返回0, a_大于b_返回1, a_小于b_返回-1
|
|
|
+int8_t _memcmp(const void *a_, const void *b_, uint32_t size)
|
|
|
+{
|
|
|
+ const char *a = a_;
|
|
|
+ const char *b = b_;
|
|
|
+ ASSERT(a != NULL && b != NULL);
|
|
|
+ while (size--)
|
|
|
+ {
|
|
|
+ if (*a != *b)
|
|
|
+ {
|
|
|
+ return *a > *b ? 1 : -1;
|
|
|
+ }
|
|
|
+ a++;
|
|
|
+ b++;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+// 函数: _strcpy
|
|
|
+// 作用: 将字符串从src_复制到dst_
|
|
|
+// 参数: dst_ 为目标地址, src_ 为源地址
|
|
|
+// 返回: 目标地址
|
|
|
+char *_strcpy(char *dst_, const char *src_)
|
|
|
+{
|
|
|
+ ASSERT(dst_ != NULL && src_ != NULL);
|
|
|
+ char *r = dst_; // 用来返回目标地址
|
|
|
+ while ((*dst_++ = *src_++))
|
|
|
+ ;
|
|
|
+ return r;
|
|
|
+}
|
|
|
+// 函数: _strlen
|
|
|
+// 作用: 返回字符串的长度
|
|
|
+// 参数: str 为字符串
|
|
|
+// 返回: 字符串的长度
|
|
|
+uint32_t _strlen(const char *str)
|
|
|
+{
|
|
|
+ ASSERT(str != NULL);
|
|
|
+ const char *p = str;
|
|
|
+ while (*p++)
|
|
|
+ ;
|
|
|
+ return (uint32_t)(p - str - 1);
|
|
|
+}
|
|
|
+// 函数: _strcmp
|
|
|
+// 作用: 比较两个字符串
|
|
|
+// 参数: a_ 为比较的字符串1, b_ 为比较的字符串2
|
|
|
+// 返回: 相等返回0, a_大于b_返回1, a_小于b_返回-1
|
|
|
+int8_t _strcmp(const char *a_, const char *b_)
|
|
|
+{
|
|
|
+ ASSERT(a_ != NULL && b_ != NULL);
|
|
|
+ const char *a = a_;
|
|
|
+ const char *b = b_;
|
|
|
+ while (*a != 0 && *a == *b)
|
|
|
+ {
|
|
|
+ a++;
|
|
|
+ b++;
|
|
|
+ }
|
|
|
+ return *a < *b ? -1 : *a > *b;
|
|
|
+}
|
|
|
+// 函数: _strchr
|
|
|
+// 作用: 在字符串中查找字符
|
|
|
+// 参数: str 为字符串, ch 为要查找的字符
|
|
|
+// 返回: 找到返回地址, 未找到返回NULL
|
|
|
+char *_strchr(const char *str, const uint8_t ch)
|
|
|
+{
|
|
|
+ ASSERT(str != NULL);
|
|
|
+ while (*str != 0)
|
|
|
+ {
|
|
|
+ if (*str == ch)
|
|
|
+ {
|
|
|
+ return (char *)str;
|
|
|
+ }
|
|
|
+ str++;
|
|
|
+ }
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+// 函数: _strrchr
|
|
|
+// 作用: 在字符串中查找字符(从后往前)
|
|
|
+// 参数: str 为字符串, ch 为要查找的字符
|
|
|
+// 返回: 找到返回地址, 未找到返回NULL
|
|
|
+char *_strrchr(const char *str, const uint8_t ch)
|
|
|
+{
|
|
|
+ ASSERT(str != NULL);
|
|
|
+ const char *last_char = NULL;
|
|
|
+ // 从前往后查找字符,若存在 ch 字符,则将其地址赋值给 last_char,last_char 为最后一个 ch 字符的地址
|
|
|
+ while (*str != 0)
|
|
|
+ {
|
|
|
+ if (*str == ch)
|
|
|
+ {
|
|
|
+ last_char = str;
|
|
|
+ }
|
|
|
+ str++;
|
|
|
+ }
|
|
|
+ return (char *)last_char;
|
|
|
+}
|
|
|
+// 函数: _strcat
|
|
|
+// 作用: 将字符串src_连接到dst_后
|
|
|
+// 参数: dst_ 为目标地址, src_ 为源地址
|
|
|
+// 返回: 目标地址(连接后的字符串)
|
|
|
+char *_strcat(char *dst_, const char *src_)
|
|
|
+{
|
|
|
+ ASSERT(dst_ != NULL && src_ != NULL);
|
|
|
+ char *str = dst_;
|
|
|
+ while (*str++)
|
|
|
+ ;
|
|
|
+ --str;
|
|
|
+ while ((*str++ = *src_++))
|
|
|
+ ;
|
|
|
+ return dst_;
|
|
|
+}
|
|
|
+// 函数: _strchrs
|
|
|
+// 作用: 在字符串中查找字符出现的次数
|
|
|
+// 参数: str 为字符串, ch 为要查找的字符
|
|
|
+// 返回: 字符出现的次数
|
|
|
+uint32_t _strchrs(const char *str, uint8_t ch)
|
|
|
+{
|
|
|
+ ASSERT(str != NULL);
|
|
|
+ uint32_t ch_cnt = 0;
|
|
|
+ while (*str != 0)
|
|
|
+ {
|
|
|
+ if (*str == ch)
|
|
|
+ {
|
|
|
+ ch_cnt++;
|
|
|
+ }
|
|
|
+ str++;
|
|
|
+ }
|
|
|
+ return ch_cnt;
|
|
|
+}
|