This repository has been archived by the owner on Mar 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
string.c
76 lines (63 loc) · 1.63 KB
/
string.c
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
#include <system.h>
#include <string.h>
#include <main.h>
/* String length function */
int strlen(const unsigned char* str)
{
/* This loops through character array 'str', returning how
* many characters it needs to check before it finds a 0.
* In simple words, it returns the length in bytes of a string */
// strlen implementation from FreeBSD 6.2
const unsigned char* s;
for (s = str; *s; ++s);
return (s - str);
}
/* String copy function */
int strcpy(unsigned char* dest, const unsigned char* src, unsigned int size)
{
memcpy(dest, src, size);
}
/* String compare function */
int strcmp(const unsigned char* a, const unsigned char* b)
{
int i = 0;
if (strlen(a) > strlen(b)) return 1;
if (strlen(a) < strlen(b)) return -1;
for (i = 0; i < strlen(a); i++)
if (a[i] != b[i])
return (a[i] > b[i]) ? 1 : -1;
return 0;
}
/* String reverse function
* Sourced from: http://www.daniweb.com/software-development/c/threads/11049 */
unsigned char* strrev(unsigned char* str)
{
unsigned char* p1;
unsigned char* p2;
if (!str || !*str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
/* Integer to string function
* Sourced from: http://www.daniweb.com/software-development/c/threads/11049 */
unsigned char* itoa(unsigned long num, unsigned char* str, int base)
{
static unsigned char digits[] = "0123456789abcdefghigjklmnopqrstuvwxyz";
unsigned long i = 0, sign;
if ((sign = num) < 0)
num = -num;
do
{
str[i++] = digits[num % base];
} while ((num /= base) > 0);
if (sign < 0)
str[i++] = '-';
str[i] = '\0';
return strrev(str);
}