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