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
|
// MIT License, Copyright (c) 2020 Marvin Borner
#include <def.h>
#include <mem.h>
#include <str.h>
u32 strlen(const char *s)
{
const char *ss = s;
while (*ss)
ss++;
return ss - s;
}
char *strcpy(char *dst, const char *src)
{
char *q = dst;
const char *p = src;
char ch;
do {
*q++ = ch = *p++;
} while (ch);
return dst;
}
char *strncpy(char *dst, const char *src, u32 n)
{
char *q = dst;
while (n-- && (*dst++ = *src++))
;
return q;
}
int strcmp(const char *s1, const char *s2)
{
const u8 *c1 = (const u8 *)s1;
const u8 *c2 = (const u8 *)s2;
u8 ch;
int d = 0;
while (1) {
d = (int)(ch = *c1++) - (int)*c2++;
if (d || !ch)
break;
}
return d;
}
int strncmp(const char *s1, const char *s2, u32 n)
{
const u8 *c1 = (const u8 *)s1;
const u8 *c2 = (const u8 *)s2;
u8 ch;
int d = 0;
while (n--) {
d = (int)(ch = *c1++) - (int)*c2++;
if (d || !ch)
break;
}
return d;
}
char *strchr(char *s, int c)
{
while (*s != (char)c) {
if (!*s)
return NULL;
s++;
}
return s;
}
char *strrchr(char *s, int c)
{
char *ret = 0;
do {
if (*s == c)
ret = s;
} while (*s++);
return ret;
}
char *strcat(char *dst, const char *src)
{
strcpy(strchr(dst, '\0'), src);
return dst;
}
char *strncat(char *dst, const char *src, u32 n)
{
strncpy(strchr(dst, '\0'), src, n);
return dst;
}
char *strinv(char *s)
{
u32 s_str = strlen(s);
int iterations = (int)s_str / 2;
for (int i = 0; i < iterations; i++) {
char aux = s[i];
s[i] = s[(s_str - i) - 1];
s[(s_str - i) - 1] = aux;
}
return s;
}
char *strdup(const char *s)
{
int l = strlen(s) + 1;
char *d = malloc(l);
memcpy(d, s, l);
return d;
}
|