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