]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/string.qh
Workaround for admins who change g_maxplayers mid match (count the actual players...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / string.qh
1 #pragma once
2
3 #include "nil.qh"
4 #include "sort.qh"
5 #include "oo.qh"
6
7 #ifdef CSQC
8         float stringwidth_colors(string s, vector theSize)
9         {
10                 return stringwidth_builtin(s, true, theSize);
11         }
12
13         float stringwidth_nocolors(string s, vector theSize)
14         {
15                 return stringwidth_builtin(s, false, theSize);
16         }
17 #endif
18 #ifdef MENUQC
19         float stringwidth_colors(string s, vector theSize)
20         {
21                 return stringwidth(s, true, theSize);
22         }
23
24         float stringwidth_nocolors(string s, vector theSize)
25         {
26                 return stringwidth(s, false, theSize);
27         }
28 #endif
29
30 string seconds_tostring(float sec)
31 {
32         float minutes = floor(sec / 60);
33         sec -= minutes * 60;
34         return sprintf("%d:%02d", minutes, sec);
35 }
36
37 string format_time(float seconds)
38 {
39         seconds = floor(seconds + 0.5);
40         float days = floor(seconds / 864000);
41         seconds -= days * 864000;
42         float hours = floor(seconds / 36000);
43         seconds -= hours * 36000;
44         float minutes = floor(seconds / 600);
45         seconds -= minutes * 600;
46         if (days > 0) return sprintf(_("%d days, %02d:%02d:%02d"), days, hours, minutes, seconds);
47         else return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds);
48 }
49
50 string mmsss(float tenths)
51 {
52         tenths = floor(tenths + 0.5);
53         float minutes = floor(tenths / 600);
54         tenths -= minutes * 600;
55         string s = ftos(1000 + tenths);
56         return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
57 }
58
59 string mmssss(float hundredths)
60 {
61         hundredths = floor(hundredths + 0.5);
62         float minutes = floor(hundredths / 6000);
63         hundredths -= minutes * 6000;
64         string s = ftos(10000 + hundredths);
65         return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2));
66 }
67
68 int ColorTranslateMode;
69
70 string ColorTranslateRGB(string s)
71 {
72         return (ColorTranslateMode & 1) ? strdecolorize(s) : s;
73 }
74
75 #ifdef GAMEQC
76 // color code replace, place inside of sprintf and parse the string... defaults described as constants
77 // foreground/normal colors
78 string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green  // primary priority (important names, etc)
79 string autocvar_hud_colorset_foreground_2 = "3"; // F2 - Yellow // secondary priority (items, locations, numbers, etc)
80 string autocvar_hud_colorset_foreground_3 = "4"; // F3 - Blue   // tertiary priority or relatively inconsequential text
81 string autocvar_hud_colorset_foreground_4 = "1"; // F4 - Red    // notice/attention grabbing texting
82 // "kill" colors
83 string autocvar_hud_colorset_kill_1 = "1";       // K1 - Red    // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
84 string autocvar_hud_colorset_kill_2 = "3";       // K2 - Yellow // similar to above, but less important... OR, a highlight out of above message type
85 string autocvar_hud_colorset_kill_3 = "4";       // K3 - Blue   // "good" or "beneficial" text (you fragging someone, etc)
86 // background color
87 string autocvar_hud_colorset_background = "7";   // BG - White // neutral/unimportant text
88
89 /** color code replace, place inside of sprintf and parse the string */
90 string CCR(string input)
91 {
92         // See the autocvar declarations in util.qh for default values
93
94         // foreground/normal colors
95         input = strreplace("^F1", strcat("^", autocvar_hud_colorset_foreground_1), input);
96         input = strreplace("^F2", strcat("^", autocvar_hud_colorset_foreground_2), input);
97         input = strreplace("^F3", strcat("^", autocvar_hud_colorset_foreground_3), input);
98         input = strreplace("^F4", strcat("^", autocvar_hud_colorset_foreground_4), input);
99
100         // "kill" colors
101         input = strreplace("^K1", strcat("^", autocvar_hud_colorset_kill_1), input);
102         input = strreplace("^K2", strcat("^", autocvar_hud_colorset_kill_2), input);
103         input = strreplace("^K3", strcat("^", autocvar_hud_colorset_kill_3), input);
104
105         // background colors
106         input = strreplace("^BG", strcat("^", autocvar_hud_colorset_background), input);
107         input = strreplace("^N", "^7", input);  // "none"-- reset to white...
108         return input;
109 }
110 #endif
111
112 #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0)
113
114 bool startsWithNocase(string haystack, string needle)
115 {
116         return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
117 }
118
119 noref string _endsWith_suffix;
120 #define endsWith(this, suffix) (_endsWith_suffix = suffix, substring(this, -strlen(_endsWith_suffix), -1) == _endsWith_suffix)
121
122 /** unzone the string, and return it as tempstring. Safe to be called on string_null */
123 string fstrunzone(string s)
124 {
125         if (!s) return s;
126         string sc = strcat(s, "");
127         strunzone(s);
128         return sc;
129 }
130
131 /** returns first word */
132 string car(string s)
133 {
134         int o = strstrofs(s, " ", 0);
135         if (o < 0) return s;
136         return substring(s, 0, o);
137 }
138
139 /** returns all but first word */
140 string cdr(string s)
141 {
142         int o = strstrofs(s, " ", 0);
143         if (o < 0) return string_null;
144         return substring(s, o + 1, strlen(s) - (o + 1));
145 }
146
147 string cons(string a, string b)
148 {
149         if (a == "") return b;
150         if (b == "") return a;
151         return strcat(a, " ", b);
152 }
153
154 string substring_range(string s, float b, float e)
155 {
156         return substring(s, b, e - b);
157 }
158
159 string swapwords(string str, float i, float j)
160 {
161         float n;
162         string s1, s2, s3, s4, s5;
163         float si, ei, sj, ej, s0, en;
164         n = tokenizebyseparator(str, " ");  // must match g_maplist processing in ShuffleMaplist and "shuffle"
165         si = argv_start_index(i);
166         sj = argv_start_index(j);
167         ei = argv_end_index(i);
168         ej = argv_end_index(j);
169         s0 = argv_start_index(0);
170         en = argv_end_index(n - 1);
171         s1 = substring_range(str, s0, si);
172         s2 = substring_range(str, si, ei);
173         s3 = substring_range(str, ei, sj);
174         s4 = substring_range(str, sj, ej);
175         s5 = substring_range(str, ej, en);
176         return strcat(s1, s4, s3, s2, s5);
177 }
178
179 string _shufflewords_str;
180 void _shufflewords_swapfunc(float i, float j, entity pass)
181 {
182         _shufflewords_str = swapwords(_shufflewords_str, i, j);
183 }
184 string shufflewords(string str)
185 {
186         _shufflewords_str = str;
187         int n = tokenizebyseparator(str, " ");
188         shuffle(n, _shufflewords_swapfunc, NULL);
189         str = _shufflewords_str;
190         _shufflewords_str = string_null;
191         return str;
192 }
193
194 string unescape(string in)
195 {
196         in = strzone(in);  // but it doesn't seem to be necessary in my tests at least
197
198         int len = strlen(in);
199         string str = "";
200         for (int i = 0; i < len; ++i)
201         {
202                 string s = substring(in, i, 1);
203                 if (s == "\\")
204                 {
205                         s = substring(in, i + 1, 1);
206                         if (s == "n") str = strcat(str, "\n");
207                         else if (s == "\\") str = strcat(str, "\\");
208                         else str = strcat(str, substring(in, i, 2));
209                         ++i;
210                         continue;
211                 }
212                 str = strcat(str, s);
213         }
214         strunzone(in);
215         return str;
216 }
217
218 string strwords(string s, int w)
219 {
220         int endpos = 0;
221         for ( ; w && endpos >= 0; --w)
222                 endpos = strstrofs(s, " ", endpos + 1);
223         if (endpos < 0) return s;
224         return substring(s, 0, endpos);
225 }
226
227 #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0)
228
229 int u8_strsize(string s)
230 {
231         int l = 0;
232         for (int i = 0, c; (c = str2chr(s, i)) > 0; ++i, ++l)
233         {
234                 l += (c >= 0x80);
235                 l += (c >= 0x800);
236                 l += (c >= 0x10000);
237         }
238         return l;
239 }
240
241 bool isInvisibleString(string s)
242 {
243         s = strdecolorize(s);
244         bool utf8 = cvar("utf8_enable");
245         for (int i = 0, n = strlen(s); i < n; ++i)
246         {
247                 int c = str2chr(s, i);
248                 switch (c)
249                 {
250                         case 0:
251                         case 32:           // space
252                                 break;
253                         case 192:          // charmap space
254                                 if (!utf8) break;
255                                 return false;
256                         case 160:          // space in unicode fonts
257                         case 0xE000 + 192: // utf8 charmap space
258                                 if (utf8) break;
259                         default:
260                                 return false;
261                 }
262         }
263         return true;
264 }
265
266 // Multiline text file buffers
267
268 int buf_load(string pFilename)
269 {
270         int buf = buf_create();
271         if (buf < 0) return -1;
272         int fh = fopen(pFilename, FILE_READ);
273         if (fh < 0)
274         {
275                 buf_del(buf);
276                 return -1;
277         }
278         string l;
279         for (int i = 0; (l = fgets(fh)); ++i)
280                 bufstr_set(buf, i, l);
281         fclose(fh);
282         return buf;
283 }
284
285 void buf_save(float buf, string pFilename)
286 {
287         int fh = fopen(pFilename, FILE_WRITE);
288         if (fh < 0) error(strcat("Can't write buf to ", pFilename));
289         int n = buf_getsize(buf);
290         for (int i = 0; i < n; ++i)
291                 fputs(fh, strcat(bufstr_get(buf, i), "\n"));
292         fclose(fh);
293 }
294
295 /**
296  * converts a number to a string with the indicated number of decimals
297  */
298 string ftos_decimals(float number, int decimals)
299 {
300         // inhibit stupid negative zero
301         if (number == 0) number = 0;
302         return sprintf("%.*f", decimals, number);
303 }
304
305 /**
306  * converts a number to a string with the minimum number of decimals
307  */
308 string ftos_mindecimals(float number)
309 {
310         // inhibit stupid negative zero
311         if (number == 0) number = 0;
312         return sprintf("%.7g", number);
313 }
314
315 int vercmp_recursive(string v1, string v2)
316 {
317         int dot1 = strstrofs(v1, ".", 0);
318         int dot2 = strstrofs(v2, ".", 0);
319         string s1 = (dot1 == -1) ? v1 : substring(v1, 0, dot1);
320         string s2 = (dot2 == -1) ? v2 : substring(v2, 0, dot2);
321
322         float r;
323         r = stof(s1) - stof(s2);
324         if (r != 0) return r;
325
326         r = strcasecmp(s1, s2);
327         if (r != 0) return r;
328
329         if (dot1 == -1) return (dot2 == -1) ? 0 : -1;
330         else return (dot2 == -1) ? 1 : vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
331 }
332
333 int vercmp(string v1, string v2)
334 {
335         if (strcasecmp(v1, v2) == 0) return 0;  // early out check
336
337         // "git" beats all
338         if (v1 == "git") return 1;
339         if (v2 == "git") return -1;
340
341         return vercmp_recursive(v1, v2);
342 }
343
344 const string HEXDIGITS_MINSET = "0123456789ABCDEFabcdef";
345 const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
346 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
347 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
348 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS_MINSET, (d), 1))
349 #define IS_HEXDIGIT(d) (strstrofs(HEXDIGITS_MINSET, (d), 0) >= 0)
350
351 const string DIGITS = "0123456789";
352 #define IS_DIGIT(d) (strstrofs(DIGITS, (d), 0) >= 0)