SimpleKernel 1.17.0
Loading...
Searching...
No Matches
sk_string.c
Go to the documentation of this file.
1
5#include "sk_string.h"
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11// 复制内存块
12void* memcpy(void* dest, const void* src, size_t n) {
13 char* d = (char*)dest;
14 const char* s = (const char*)src;
15 while (n--) {
16 *d++ = *s++;
17 }
18 return dest;
19}
20
21// 复制内存块,可以处理重叠区域。
22void* memmove(void* dest, const void* src, size_t n) {
23 char* d = (char*)dest;
24 const char* s = (const char*)src;
25 if (d < s) {
26 while (n--) {
27 *d++ = *s++;
28 }
29 } else {
30 const char* lasts = s + (n - 1);
31 char* lastd = d + (n - 1);
32 while (n--) {
33 *lastd-- = *lasts--;
34 }
35 }
36 return dest;
37}
38
39// 设置内存块
40void* memset(void* dest, int val, size_t n) {
41 unsigned char* ptr = (unsigned char*)dest;
42 while (n-- > 0) {
43 *ptr++ = val;
44 }
45 return dest;
46}
47
48// 比较内存块
49int memcmp(const void* str1, const void* str2, size_t n) {
50 const unsigned char* s1 = (const unsigned char*)str1;
51 const unsigned char* s2 = (const unsigned char*)str2;
52
53 while (n-- > 0) {
54 if (*s1++ != *s2++) {
55 return s1[-1] < s2[-1] ? -1 : 1;
56 }
57 }
58 return 0;
59}
60
61// 在内存块中查找字符
62const void* memchr(const void* str, int c, size_t n) {
63 const unsigned char* src = (const unsigned char*)str;
64 unsigned char uc = (unsigned char)c;
65
66 while (n-- > 0) {
67 if (*src == uc) {
68 return (void*)src;
69 }
70 src++;
71 }
72 return NULL;
73}
74
75// 复制字符串
76char* strcpy(char* dest, const char* src) {
77 char* address = dest;
78 while ((*dest++ = *src++) != '\0') {
79 ;
80 }
81 return address;
82}
83
84// 复制指定长度的字符串
85char* strncpy(char* dest, const char* src, size_t n) {
86 size_t size = strnlen(src, n);
87 if (size != n) {
88 memset(dest + size, '\0', n - size);
89 }
90 return (char*)memcpy(dest, src, size);
91}
92
93// 连接字符串
94char* strcat(char* dest, const char* src) {
95 char* add_d = dest;
96 if (dest != 0 && src != 0) {
97 while (*add_d) {
98 add_d++;
99 }
100 while (*src) {
101 *add_d++ = *src++;
102 }
103 *add_d = '\0';
104 }
105 return dest;
106}
107
108// 比较字符串
109int strcmp(const char* s1, const char* s2) {
110 while (*s1 && (*s1 == *s2)) {
111 s1++;
112 s2++;
113 }
114 return *(const unsigned char*)s1 - *(const unsigned char*)s2;
115}
116
117// 比较指定长度的字符串
118int strncmp(const char* s1, const char* s2, size_t n) {
119 if (n == 0) {
120 return 0;
121 }
122 do {
123 if (*s1 != *s2++) {
124 return (*(const unsigned char*)s1 - *(const unsigned char*)(s2 - 1));
125 }
126 if (*s1++ == '\0') {
127 break;
128 }
129 } while (--n != 0);
130 return 0;
131}
132
133// 获取字符串长度
134size_t strlen(const char* s) {
135 size_t len = 0;
136 while (s[len]) {
137 len++;
138 }
139 return len;
140}
141
142// 获取指定字符串长度
143size_t strnlen(const char* s, size_t n) {
144 const char* p = s;
145 while (n-- > 0 && *p) {
146 p++;
147 }
148 return p - s;
149}
150
151// 查找字符在字符串中的首次出现
152const char* strchr(const char* s, int c) {
153 char ch = (char)c;
154 do {
155 if (*s == ch) {
156 return (char*)s;
157 }
158 } while (*s++);
159 return NULL;
160}
161
162// 反向查找字符在字符串中的首次出现
163const char* strrchr(const char* s, int c) {
164 char* rtnval = 0;
165 char ch = (char)c;
166
167 do {
168 if (*s == ch) {
169 rtnval = (char*)s;
170 }
171 } while (*s++);
172 return (rtnval);
173}
174
175#ifdef __cplusplus
176}
177#endif
#define memcpy
#define strcpy
#define strlen
#define strncpy
#define strcmp
#define strnlen
#define memmove
#define strncmp
#define memcmp
#define strchr
#define memset
#define strrchr
#define strcat
#define memchr