-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.c
More file actions
93 lines (80 loc) · 1.25 KB
/
lib.c
File metadata and controls
93 lines (80 loc) · 1.25 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
#include "lib.h"
char *strchr(const char *s, char c)
{
while (*s)
{
if (*s == c)
return (char *)s;
s++;
}
if (c == 0)
return (char *)s;
return (char *)0;
}
char strcmp(const char *s1, const char *s2)
{
while (*s1 && *s1 == *s2)
{
s1++;
s2++;
}
return *s2 - *s1;
}
int strlen(const char *s)
{
int i = 0;
while (s[i])
i++;
return i;
}
void memset(void *buf, int c, usize n)
{
usize i = 0;
while (i < n)
((char *)buf)[i++] = c;
}
void bzero(void *buf, usize n)
{
memset(buf, 0, n);
}
void memmove(void *dst, void *src, usize n)
{
if (src < dst)
{
usize i = n;
while (i-- > 0)
((u8 *)dst)[i] = ((u8 *)src)[i];
if (n)
*dst = *src;
}
else
{
usize i = 0;
while (i < n)
{
((u8 *)dst)[i] = ((u8 *)src)[i];
i++;
}
}
}
#include "tests.h"
TESTS()
{
ensure(strlen("hello") == 5);
ensure(strlen("") == 0);
ensure(strcmp("hello", "hello") == 0);
ensure(strcmp("hello", "hellp") == 1);
ensure(strcmp(strchr("hello world", 'w'), "world") == 0);
{
char dst[] = "hello brigitte";
char src[] = "world";
memmove(dst + 6, src, sizeof(src));
ensure(strcmp(dst, "hello world") == 0);
}
{
char dst[] = "world hello";
char *src = dst + 6;
memmove(dst, src, 5);
ensure(strcmp(dst, "hello hello") == 0);
}
}