-
Notifications
You must be signed in to change notification settings - Fork 2
/
string.qc
160 lines (134 loc) · 2.92 KB
/
string.qc
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const int MAX_QPATH = 64; // max length of a quake game pathname
// this is a special boolean that an engine must support
// that changes how string functions/opcodes work.
// this changes:
// == and != become case-insensitive if false
// strcmp will call stricmp internally if false
// strncmp will call strnicmp internally if false
API_FUNC var bool strcasesensitive = true;
inline int(string str, int offset) strat =
{
return str[offset];
}
int(string) stoi = #0;
float(string) stof = #0;
int(string, string) strcmp = #0;
int(string, string, int) strncmp = #0;
int(string) strlen = #0;
string(string, ...) va = #0;
int(string, string) strstr = #0;
int(string, int) strchr = #0;
string(string, int start, optional int length) substr = #0;
string(string...) strconcat = #0;
int(int) chrlwr = #0;
int(int) chrupr = #0;
string(string) strlwr = #0;
string(string) strupr = #0;
INLINE bool(string a, string b) stricmp =
{
strcasesensitive = false;
bool result = strcmp(a, b);
strcasesensitive = true;
return result;
}
INLINE bool(string a, string b, int len) strnicmp =
{
strcasesensitive = false;
bool result = strncmp(a, b, len);
strcasesensitive = true;
return result;
}
INLINE bool(string a, string b) striequals =
{
strcasesensitive = false;
bool result = (a == b) != 0;
strcasesensitive = true;
return result;
}
#define strempty(s) (!(s))
inline string(int v) itos =
{
return va("%i", v);
};
inline string(float v) ftos =
{
return va("%f", v);
};
inline string(vector v) vtos =
{
return va("%f %f %f", v[0], v[1], v[2]);
};
/*
==============
strtok
Parse a token out of a string.
Handles C and C++ comments.
==============
*/
API_FUNC string(string data, inout int start) strtok =
{
int token_start = start, token_end;
if (!strat(data, 0))
{
start = -1;
return "";
}
// skip whitespace
int c;
skipwhite:
while ((c = strat(data, token_start)) <= ' ')
{
if (c == 0)
{
start = -1;
return "";
}
token_start++;
}
// skip // comments
if (c == '/' && strat(data, token_start + 1) == '/')
{
token_start += 2;
while ((c = strat(data, token_start)) && c != '\n')
token_start++;
goto skipwhite;
}
// skip /* */ comments
if (c == '/' && strat(data, token_start + 1) == '*')
{
token_start += 2;
while ((c = strat(data, token_start)))
{
if (c == '*' && strat(data, token_start + 1) == '/')
{
token_start += 2;
break;
}
token_start++;
}
goto skipwhite;
}
token_end = token_start;
// handle quoted strings specially
if (c == '\"')
{
token_end++;
while ((c = strat(data, token_end++)) && c != '\"') ;
if (!strat(data, token_end))
start = -1;
else
start = token_end;
return substr(data, token_start + 1, token_end - token_start - 2);
}
// parse a regular word
do
{
token_end++;
c = strat(data, token_end);
} while (c > 32);
if (!strat(data, token_end))
start = -1;
else
start = token_end;
return substr(data, token_start, token_end - token_start);
};