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