-
Notifications
You must be signed in to change notification settings - Fork 0
/
q_stl.c
118 lines (105 loc) · 2.36 KB
/
q_stl.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
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
#include "quakedefs.h"
// Copy src string to dst
void Q_strcpy(char *dst, const char *src)
{
while (*src)
{
*dst++ = *src++;
}
// Ensures destination is null terminated
*dst = 0;
}
// Copy a number of characters from src string to dst padding with zeroes if needed
void Q_strncpy(char *dst, const char *src, int32_t count)
{
if (count < 0)
{
return;
}
while (*src && count)
{
*dst++ = *src++;
--count;
}
while (count > 0)
{
*dst = 0;
--count;
}
}
int32_t Q_strlen(const char *str)
{
int32_t count = 0;
while (str[count])
{
++count;
}
return count;
}
// String comparison (0 if equal, -1 if s1 < s2, 1 if s1 > s2)
int32_t Q_strcmp(const char *s1, const char *s2)
{
// Skip part where values are equal
while (*s1 == *s2)
{
// Both strings ended at terminator so they are equal
if (*s1 == 0)
{
return 0;
}
++s1;
++s2;
}
// Reached two chars that are different. Find out which comes first.
return ((*s1 < *s2) ? -1 : 1);
}
// Convert a string with a decimal or hexadecimal signed/unsigned number to an integer value
int32_t Q_atoi(const char *str)
{
int32_t sign = 1;
int32_t val = 0;
if (*str == '-')
{
sign = -1;
str++;
}
// hexadecimal (prefixed by 0x or 0X)
if ((str[0] == '0') && ((str[1] == 'x') || (str[1] == 'X')))
{
str += 2;
for (;;)
{
const char c = *str;
str++;
// Reached a non-numerical char, so return the value found so far
if ((c >= '0') && (c <= '9'))
{
val = (val * 16) + (c - '0');
}
else if ((c >= 'a') && (c <= 'f'))
{
val = (val * 16) + (c - 'a' + 10);
}
else if ((c >= 'A') && (c <= 'F'))
{
val = (val * 16) + (c - 'F' + 10);
}
else
{
return sign * val;
}
}
}
// decimal
for (;;)
{
const char c = *str;
str++;
// Reached a non-numerical char, so return the value found so far
if ((c < '0') || (c > '9'))
{
return sign * val;
}
val = (val * 10) + (c - '0');
}
}