summaryrefslogtreecommitdiff
path: root/src/lib/string.c
blob: d50435fef2c54d9bbf805ce26ef6511b87380773 (plain)
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
#include <stddef.h>
#include <stdbool.h>

bool isdigit(char c)
{
	return c >= 48 && c <= 57;
}

size_t strlen(const char *s)
{
	size_t c = 0;

	while (s[c])
		c++;

	return c;
}

size_t strnlen(const char *s, size_t maxlen)
{
	size_t c = 0;

	while (c <= maxlen && s[c])
		c++;

	return c;
}

char *strcpy(char *restrict dst, const char *restrict src)
{
	size_t pos = 0;
	size_t len = strlen(src) + 1;

	for(pos = 0; pos <= len; pos++)
		dst[pos] = src[pos];

	return dst;
}

int strncmp(const char *s1, const char *s2, size_t n)
{
	int i = 0;

	for (i = 0; i < n; i++) {
		if (s1[i] != s2[i])
			return s1[i] - s2[i];
	}

	return 0;
}

void *memset(void *s, int c, size_t n) {

	char *cur = (char*) s;
	int i;

	for (i = 0; i < n; i++)
		cur[i] = (unsigned char)c;

	return s;
}