-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstring
More file actions
108 lines (75 loc) · 1.85 KB
/
cstring
File metadata and controls
108 lines (75 loc) · 1.85 KB
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
#include <cstddef>
#include <cstdlib>
#include <cstdint>
#ifndef _cstring_
#define _cstring_
// TODO: refactor all the counter variable based methods to increment the pointers (see: strcat)
void memcpy(void* destination, const void* source, size_t num) {
for (size_t i = 0; i < num; i++) {
((uint8_t*)destination)[i] = ((uint8_t*)source)[i];
}
}
void memmove(void* destination, const void* source, size_t num) {
void* buffer = malloc(num);
memcpy(buffer, source, num);
memcpy(destination, buffer, num);
free(buffer);
}
char* strcpy(char* destination, const char* source) {
for (size_t i = 0; source[i]; i++) {
((uint8_t*)destination)[i] = ((uint8_t*)source)[i];
}
return destination;
}
char* strncpy(char* destination, const char* source, size_t num) {
for (size_t i = 0; source[i] && i < num; i++) {
((uint8_t*)destination)[i] = ((uint8_t*)source)[i];
}
for (size_t i = 0; i < num; i++) {
((uint8_t*)destination)[i] = 0;
}
return destination;
}
char* strcat(char* destination, const char* source) {
// Skip to end of destination
while (*destination) {
destination++;
}
while (*source) {
*destination = *source;
source++;
destination++;
}
*destination = 0;
return destination;
}
// TODO: strncat
// TODO: memcmp
int strcmp(const char* str1, const char* str2) {
while (*str1 && *str2 && *str1 == *str2) {
str1++;
str2++;
}
return *str1 - *str2;
}
// TODO: strcoll
// TODO: strncmp
// TODO: strxfrm
// TODO: memchr
// TODO: strchr
// TODO: strcspn
// TODO: strpbrk
// TODO: strrchr
// TODO: strspn
// TODO: strstr
// TODO: strtok
// TODO: memset
// TODO: strerror
size_t strlen(const char* str) {
size_t len = 0;
while (str[len]) {
len++;
}
return len;
}
#endif