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