aboutsummaryrefslogtreecommitdiff
path: root/src/lib/strings.c
blob: 674af1944ad21a08cd44ca2d35724d0949620f08 (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
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
#include <lib/kmem.h>
#include <lib/strings.h>

unsigned long strlen(string_t s)
{
	unsigned long len = 0;
	while (s[len] != 0) {
		len += 1;
	}
	return len;
}

void strcpy(string_t src, string_t dest)
{
	unsigned long idx = 0;
	while (src[idx] != 0) {
		dest[idx] = src[idx];
		idx++;
	}
	dest[idx] = src[idx];
}

unsigned char strcmp(string_t a, string_t b)
{
	unsigned long idx = 0;
	while (a[idx] != 0 && b[idx] != 0) {
		if (a[idx] != b[idx]) {
			return 0;
		}
		idx += 1;
	}
	return a[idx] == b[idx];
}

unsigned char strcmpn(string_t a, string_t b, unsigned int n)
{
	unsigned long idx = 0;
	while (a[idx] != 0 && b[idx] != 0 && idx+1 < n) {
		if (a[idx] != b[idx]) {
			return 0;
		}
		idx += 1;
	}
	return a[idx] == b[idx];
}

char* zhex32_to_str(unsigned long value)
{
	static char data[10];
	char tmp = 0;
	char isz = -1;
	for (int i = 0; i < 8; i++) {
		tmp = (value >> 4*(8-i-1))&0xF;
		if (isz == 0xFF && tmp != 0)
			isz = i;
		if(tmp > 0x9)
			tmp += 7;
		tmp += 0x30;
		data[i] = tmp;
	}
	return data+isz;
}

char* hex32_to_str(unsigned long value)
{
	static char data[10];
	char tmp = 0;
	for (int i = 0; i < 8; i++) {
		tmp = (value >> 4*(8-i-1))&0xF;
		if(tmp > 0x9)
			tmp += 7;
		tmp += 0x30;
		data[i] = tmp;
	}
	return data;
}

char* u32_to_str(unsigned long value)
{
	unsigned long t = value;
	unsigned long c;
	static char data[12];
	char* dptr = data + 9;
	for (int i = 0; i <= 10; i++) {
		c = t%10;
		*dptr = 0x30 + (c&0xF);
		t /= 10;
		if (t==0)
			break;
		dptr -= 1;
	}
	return dptr;
}

char* s32_to_str(unsigned long value)
{
	long t = value;
	unsigned long c;
	char is_neg = 0;
	if (t < 0) {
		t = -t;
		is_neg = 1;
	}
	static char data[13];
	char* dptr = data + 10;
	for (int i = 0; i <= 10; i++) {
		c = t%10;
		*dptr = 0x30 + (c&0xF);
		t /= 10;
		if (t==0)
			break;
		dptr -= 1;
	}
	if (is_neg) {
		dptr -= 1;
		*dptr = '-';
	}
	return dptr;
}