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