]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/string.qh
Merge branch 'master' into Lyberta/KillSound
[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 ERASEABLE
31 string seconds_tostring(float sec)
32 {
33         float minutes = floor(sec / 60);
34         sec -= minutes * 60;
35         return sprintf("%d:%02d", minutes, sec);
36 }
37
38 ERASEABLE
39 string format_time(float seconds)
40 {
41         seconds = floor(seconds + 0.5);
42         float days = floor(seconds / 864000);
43         seconds -= days * 864000;
44         float hours = floor(seconds / 36000);
45         seconds -= hours * 36000;
46         float minutes = floor(seconds / 600);
47         seconds -= minutes * 600;
48         if (days > 0) return sprintf(_("%d days, %02d:%02d:%02d"), days, hours, minutes, seconds);
49         else return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds);
50 }
51
52 ERASEABLE
53 string mmsss(float tenths)
54 {
55         tenths = floor(tenths + 0.5);
56         float minutes = floor(tenths / 600);
57         tenths -= minutes * 600;
58         string s = ftos(1000 + tenths);
59         return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
60 }
61
62 ERASEABLE
63 string mmssss(float hundredths)
64 {
65         hundredths = floor(hundredths + 0.5);
66         float minutes = floor(hundredths / 6000);
67         hundredths -= minutes * 6000;
68         string s = ftos(10000 + hundredths);
69         return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2));
70 }
71
72 int ColorTranslateMode;
73
74 ERASEABLE
75 string ColorTranslateRGB(string s)
76 {
77         return (ColorTranslateMode & 1) ? strdecolorize(s) : s;
78 }
79
80 #ifdef GAMEQC
81 // color code replace, place inside of sprintf and parse the string... defaults described as constants
82 // foreground/normal colors
83 string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green  // primary priority (important names, etc)
84 string autocvar_hud_colorset_foreground_2 = "3"; // F2 - Yellow // secondary priority (items, locations, numbers, etc)
85 string autocvar_hud_colorset_foreground_3 = "4"; // F3 - Blue   // tertiary priority or relatively inconsequential text
86 string autocvar_hud_colorset_foreground_4 = "1"; // F4 - Red    // notice/attention grabbing texting
87 // "kill" colors
88 string autocvar_hud_colorset_kill_1 = "1";       // K1 - Red    // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
89 string autocvar_hud_colorset_kill_2 = "3";       // K2 - Yellow // similar to above, but less important... OR, a highlight out of above message type
90 string autocvar_hud_colorset_kill_3 = "4";       // K3 - Blue   // "good" or "beneficial" text (you fragging someone, etc)
91 // background color
92 string autocvar_hud_colorset_background = "7";   // BG - White // neutral/unimportant text
93
94 /** color code replace, place inside of sprintf and parse the string */
95 string CCR(string input)
96 {
97         // See the autocvar declarations in util.qh for default values
98
99         // foreground/normal colors
100         input = strreplace("^F1", strcat("^", autocvar_hud_colorset_foreground_1), input);
101         input = strreplace("^F2", strcat("^", autocvar_hud_colorset_foreground_2), input);
102         input = strreplace("^F3", strcat("^", autocvar_hud_colorset_foreground_3), input);
103         input = strreplace("^F4", strcat("^", autocvar_hud_colorset_foreground_4), input);
104
105         // "kill" colors
106         input = strreplace("^K1", strcat("^", autocvar_hud_colorset_kill_1), input);
107         input = strreplace("^K2", strcat("^", autocvar_hud_colorset_kill_2), input);
108         input = strreplace("^K3", strcat("^", autocvar_hud_colorset_kill_3), input);
109
110         // background colors
111         input = strreplace("^BG", strcat("^", autocvar_hud_colorset_background), input);
112         input = strreplace("^N", "^7", input);  // "none"-- reset to white...
113         return input;
114 }
115 #endif
116
117 #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0)
118
119 ERASEABLE
120 bool startsWithNocase(string haystack, string needle)
121 {
122         return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
123 }
124
125 noref string _endsWith_suffix;
126 #define endsWith(this, suffix) (_endsWith_suffix = suffix, substring(this, -strlen(_endsWith_suffix), -1) == _endsWith_suffix)
127
128 /** unzone the string, and return it as tempstring. Safe to be called on string_null */
129 ERASEABLE
130 string fstrunzone(string s)
131 {
132         if (!s) return s;
133         string sc = strcat(s, "");
134         strunzone(s);
135         return sc;
136 }
137
138 /** returns first word */
139 ERASEABLE
140 string car(string s)
141 {
142         int o = strstrofs(s, " ", 0);
143         if (o < 0) return s;
144         return substring(s, 0, o);
145 }
146
147 /** returns all but first word */
148 ERASEABLE
149 string cdr(string s)
150 {
151         int o = strstrofs(s, " ", 0);
152         if (o < 0) return string_null;
153         return substring(s, o + 1, strlen(s) - (o + 1));
154 }
155
156 ERASEABLE
157 string cons(string a, string b)
158 {
159         if (a == "") return b;
160         if (b == "") return a;
161         return strcat(a, " ", b);
162 }
163
164 ERASEABLE
165 string cons_mid(string a, string mid, string b)
166 {
167         if (a == "") return b;
168         if (b == "") return a;
169         return strcat(a, mid, b);
170 }
171
172 ERASEABLE
173 string substring_range(string s, float b, float e)
174 {
175         return substring(s, b, e - b);
176 }
177
178 ERASEABLE
179 string swapwords(string str, float i, float j)
180 {
181         float n;
182         string s1, s2, s3, s4, s5;
183         float si, ei, sj, ej, s0, en;
184         n = tokenizebyseparator(str, " ");  // must match g_maplist processing in ShuffleMaplist and "shuffle"
185         si = argv_start_index(i);
186         sj = argv_start_index(j);
187         ei = argv_end_index(i);
188         ej = argv_end_index(j);
189         s0 = argv_start_index(0);
190         en = argv_end_index(n - 1);
191         s1 = substring_range(str, s0, si);
192         s2 = substring_range(str, si, ei);
193         s3 = substring_range(str, ei, sj);
194         s4 = substring_range(str, sj, ej);
195         s5 = substring_range(str, ej, en);
196         return strcat(s1, s4, s3, s2, s5);
197 }
198
199 string _shufflewords_str;
200 ERASEABLE
201 void _shufflewords_swapfunc(float i, float j, entity pass)
202 {
203         _shufflewords_str = swapwords(_shufflewords_str, i, j);
204 }
205
206 ERASEABLE
207 string shufflewords(string str)
208 {
209         _shufflewords_str = str;
210         int n = tokenizebyseparator(str, " ");
211         shuffle(n, _shufflewords_swapfunc, NULL);
212         str = _shufflewords_str;
213         _shufflewords_str = string_null;
214         return str;
215 }
216
217 ERASEABLE
218 string unescape(string in)
219 {
220         in = strzone(in);  // but it doesn't seem to be necessary in my tests at least
221
222         int len = strlen(in);
223         string str = "";
224         for (int i = 0; i < len; ++i)
225         {
226                 string s = substring(in, i, 1);
227                 if (s == "\\")
228                 {
229                         s = substring(in, i + 1, 1);
230                         if (s == "n") str = strcat(str, "\n");
231                         else if (s == "\\") str = strcat(str, "\\");
232                         else str = strcat(str, substring(in, i, 2));
233                         ++i;
234                         continue;
235                 }
236                 str = strcat(str, s);
237         }
238         strunzone(in);
239         return str;
240 }
241
242 ERASEABLE
243 string strwords(string s, int w)
244 {
245         int endpos = 0;
246         for ( ; w && endpos >= 0; --w)
247                 endpos = strstrofs(s, " ", endpos + 1);
248         if (endpos < 0) return s;
249         return substring(s, 0, endpos);
250 }
251
252 #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0)
253
254 ERASEABLE
255 int u8_strsize(string s)
256 {
257         int l = 0;
258         for (int i = 0, c; (c = str2chr(s, i)) > 0; ++i, ++l)
259         {
260                 l += (c >= 0x80);
261                 l += (c >= 0x800);
262                 l += (c >= 0x10000);
263         }
264         return l;
265 }
266
267 //List of Unicode spaces: https://www.cs.tut.fi/~jkorpela/chars/spaces.html
268 ERASEABLE
269 bool isInvisibleString(string s)
270 {
271         s = strdecolorize(s);
272         bool utf8 = cvar("utf8_enable");
273         for (int i = 0, n = strlen(s); i < n; ++i)
274         {
275                 int c = str2chr(s, i);
276                 switch (c)
277                 {
278                         case 0:
279                         case 32:           // space
280                                 break;
281                         case 192:          // charmap space
282                                 if (!utf8) break;
283                                 return false;
284                         case 0xE000 + 192: // utf8 charmap space
285                         case 0x00A0: // NO-BREAK SPACE
286                         //case 0x1680: // OGHAM SPACE MARK
287                         case 0x180E: // MONGOLIAN VOWEL SEPARATOR
288                         case 0x2000: // EN QUAD
289                         case 0x2001: // EM QUAD
290                         case 0x2002: // EN SPACE
291                         case 0x2003: // EM SPACE
292                         case 0x2004: // THREE-PER-EM SPACE
293                         case 0x2005: // FOUR-PER-EM SPACE
294                         case 0x2006: // SIX-PER-EM SPACE
295                         case 0x2007: // FIGURE SPACE
296                         case 0x2008: // PUNCTUATION SPACE
297                         case 0x2009: // THIN SPACE
298                         case 0x200A: // HAIR SPACE
299                         case 0x200B: // ZERO WIDTH SPACE
300                         case 0x202F: // NARROW NO-BREAK SPACE
301                         case 0x205F: // MEDIUM MATHEMATICAL SPACE
302                         case 0x3000: // IDEOGRAPHIC SPACE
303                         case 0xFEFF: // ZERO WIDTH NO-BREAK SPACE
304                                 if (utf8) break;
305                         default:
306                                 return false;
307                 }
308         }
309         return true;
310 }
311
312 // Multiline text file buffers
313
314 ERASEABLE
315 int buf_load(string pFilename)
316 {
317         int buf = buf_create();
318         if (buf < 0) return -1;
319         int fh = fopen(pFilename, FILE_READ);
320         if (fh < 0)
321         {
322                 buf_del(buf);
323                 return -1;
324         }
325         string l;
326         for (int i = 0; (l = fgets(fh)); ++i)
327                 bufstr_set(buf, i, l);
328         fclose(fh);
329         return buf;
330 }
331
332 ERASEABLE
333 void buf_save(float buf, string pFilename)
334 {
335         int fh = fopen(pFilename, FILE_WRITE);
336         if (fh < 0) error(strcat("Can't write buf to ", pFilename));
337         int n = buf_getsize(buf);
338         for (int i = 0; i < n; ++i)
339                 fputs(fh, strcat(bufstr_get(buf, i), "\n"));
340         fclose(fh);
341 }
342
343 /**
344  * converts a number to a string with the indicated number of decimals
345  */
346 ERASEABLE
347 string ftos_decimals(float number, int decimals)
348 {
349         // inhibit stupid negative zero
350         if (number == 0) number = 0;
351         return sprintf("%.*f", decimals, number);
352 }
353
354 /**
355  * converts a number to a string with the minimum number of decimals
356  */
357 ERASEABLE
358 string ftos_mindecimals(float number)
359 {
360         // inhibit stupid negative zero
361         if (number == 0) number = 0;
362         return sprintf("%.7g", number);
363 }
364
365 ERASEABLE
366 int vercmp_recursive(string v1, string v2)
367 {
368         int dot1 = strstrofs(v1, ".", 0);
369         int dot2 = strstrofs(v2, ".", 0);
370         string s1 = (dot1 == -1) ? v1 : substring(v1, 0, dot1);
371         string s2 = (dot2 == -1) ? v2 : substring(v2, 0, dot2);
372
373         float r;
374         r = stof(s1) - stof(s2);
375         if (r != 0) return r;
376
377         r = strcasecmp(s1, s2);
378         if (r != 0) return r;
379
380         if (dot1 == -1) return (dot2 == -1) ? 0 : -1;
381         else return (dot2 == -1) ? 1 : vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
382 }
383
384 ERASEABLE
385 int vercmp(string v1, string v2)
386 {
387         if (strcasecmp(v1, v2) == 0) return 0;  // early out check
388
389         // "git" beats all
390         if (v1 == "git") return 1;
391         if (v2 == "git") return -1;
392
393         return vercmp_recursive(v1, v2);
394 }
395
396 const string HEXDIGITS_MINSET = "0123456789ABCDEFabcdef";
397 const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
398 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
399 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
400 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS_MINSET, (d), 1))
401 #define IS_HEXDIGIT(d) (strstrofs(HEXDIGITS_MINSET, (d), 0) >= 0)
402
403 const string DIGITS = "0123456789";
404 #define IS_DIGIT(d) (strstrofs(DIGITS, (d), 0) >= 0)