-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkstring.c
259 lines (212 loc) · 4.97 KB
/
kstring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "kstring.h"
#include <stddef.h>
#include "kvarargs.h"
void* k_memcpy(void* destination, const void* source, size_t num) {
char* d = destination;
const char * s = source;
for (int i = 0; i < num; i++) { d[i] = s[i]; }
return destination;
}
void* k_memmove(void* destination, const void* source, size_t num) {
char *d = destination;
const char *s = source;
if (source <= destination) { // copy in ascent order
for (size_t i = 0; i < num; i++) d[i] = s[i];
}
else { // copy in descent order
for (size_t i = num; i > 0; i--) d[num - 1] = s[num - 1];
}
return destination;
}
void* k_memset(void* ptr, int value, size_t num) {
// slow version, shall use stosb/stosw/stosd instead
char* d = ptr;
for (size_t i = 0; i < num; i++) d[i] = value;
return ptr;
}
int k_memcmp(const void* ptr1, const void* ptr2, size_t num) {
const char* d = ptr1, * s = ptr2;
for (size_t i = 0; i < num; i++) {
int diff = d[i] - s[i];
if (diff != 0) return diff;
}
return 0;
}
const void* k_memchr(const void* ptr, int value, size_t num) {
const char* d = ptr;
for (size_t i = 0; i < num; i++) if (d[i] == value) return d + i;
return 0;
}
char* k_strcat(char* destination, const char* source) {
char* d = destination;
while (*d != 0) d++;
while (*source != 0) { *d = *source; d++, source++; }
*d = 0;
return destination;
}
const char* k_strchr(const char* str, int character) {
while (*str != 0) {
if (*str == character) return str;
str++;
}
return 0;
}
int k_strcmp(const char* str1, const char* str2) {
while (*str1 != 0 && *str2 != 0) {
int diff = *str1 - *str2;
if (diff != 0) return diff;
str1++, str2++;
}
return *str1 - *str2;
}
int k_strncmp(const char* str1, const char* str2, size_t n) {
int i = 0;
while (*str1 != 0 && *str2 != 0 && i < n) {
int diff = *str1 - *str2;
if (diff != 0) return diff;
str1++, str2++;
}
return i == n ? 0 : *str1 - *str2;
}
size_t k_strlen(const char* str) {
const char *t = str;
while (*t != 0) t++;
return t - str;
}
char* k_strcpy(char* destination, const char* source) {
char* d = destination;
while ((*d++ = *source++) != 0);
return destination;
}
char* k_strrev(char *str) {
size_t l = k_strlen(str);
k_memrev(str, l);
return str;
}
void k_rev8(char* pbyte1, char* pbyte2) {
char t = *pbyte1;
*pbyte1 = *pbyte2;
*pbyte2 = t;
}
void k_rev16(short* pword1, short* pword2) {
short t = *pword1;
*pword1 = *pword2;
*pword2 = t;
}
void k_rev32(int* pdword1, int* pdword2) {
int t = *pdword1;
*pdword1 = *pdword2;
*pdword2 = t;
}
void k_rev64(long long* pqword1, long long* pqword2) {
long long t = *pqword1;
*pqword1 = *pqword2;
*pqword2 = t;
}
void * k_memrev(void *ptr, size_t num) {
char *ph = ptr, *pt = (ph + num) - 1;
while (ph < pt) {
k_rev8(ph, pt);
ph++, pt--;
}
return ptr;
}
static const char NUM_MAP[] = "0123456789ABCDEF";
void *k_utoa(unsigned num, char* str, int base) {
if (NULL == str) return NULL;
if (base > 16) base = 16;
size_t i = 0;
if (num == 0) {
str[i++] = NUM_MAP[num];
str[i] = NULL;
return str;
}
while (num != 0) {
int r = num % base;
str[i++] = NUM_MAP[r];
num /= base;
}
str[i] = NULL;
k_memrev(str, i);
return str;
}
void *k_itoa(int num, char* str, int base){
if (NULL == str) return NULL;
if (base > 16) base = 16;
size_t i = 0;
int is_neg = 0;
if (num == 0) {
str[i++] = '0';
str[i] = 0x0;
return str;
}
if (num < 0 && base == 10) {
is_neg = 1;
num = -num;
}
while (num != 0) {
int r = num % base;
str[i++] = NUM_MAP[r];
num /= base;
}
if (is_neg) str[i++] = '-';
str[i] = NULL;
k_memrev(str, i);
return str;
}
int k_sprintf(char *str, const char *fmt, ...) {
va_list va;
va_start(va, fmt);
int int_temp;
unsigned int uint_temp;
char char_temp;
char *string_temp;
double double_temp;
char ch;
int l = 0;
char buffer[256];
while ( ch = *fmt++ ) {
if ('%' == ch) {
switch (ch = *fmt++) {
case '%':
str[l++] = ch;
break;
case 's':
string_temp = va_arg(va, char *);
str[l] = NULL;
k_strcat(str, string_temp);
l += k_strlen(string_temp);
break;
case 'd':
int_temp = va_arg(va, int);
k_itoa(int_temp, buffer, 10);
str[l] = NULL;
k_strcat(str, buffer);
l += k_strlen(buffer);
break;
case 'x':
uint_temp = va_arg(va, unsigned int *);
k_utoa(uint_temp, buffer, 16);
str[l] = NULL;
k_strcat(str, buffer);
l += k_strlen(buffer);
break;
case 0: // Reach out to the end of the format string
goto end;
}
}
else {
str[l++] = ch;
}
}
end:
va_end(va);
str[l] = 0x0;
return l;
}
inline char k_toupper(char c) {
return c >= 'a' && c <= 'z' ? (c + ('A' - 'a')) : c;
}
inline char k_tolower(char c) {
return c >= 'A' && c <= 'Z' ? (c + ('a' - 'A')) : c;
}