10 // == "": is equal to ""
11 // is "": has the same string index as the string constant ""
12 // strunzone: can be strunzoned
14 // | | true | == "" | is "" | strunzone |
15 // | :----------: | :--: | :---: | :---: | :-------: |
16 // | nil | | yes | | |
17 // | strcat(nil) | yes | yes | | |
18 // | strzone(nil) | yes | yes | | yes |
19 // | "" | yes | yes | yes | |
20 // | strcat("") | yes | yes | | |
21 // | strzone("") | yes | yes | | yes |
22 // | "s" | yes | | | |
23 // | strcat("s") | yes | | | |
24 // | strzone("s") | yes | | | yes |
27 float stringwidth_colors(string s, vector theSize)
29 return stringwidth_builtin(s, true, theSize);
32 float stringwidth_nocolors(string s, vector theSize)
34 return stringwidth_builtin(s, false, theSize);
38 float stringwidth_colors(string s, vector theSize)
40 return stringwidth(s, true, theSize);
43 float stringwidth_nocolors(string s, vector theSize)
45 return stringwidth(s, false, theSize);
49 #define strcpy(this, s) MACRO_BEGIN \
56 #define strfree(this) MACRO_BEGIN \
64 string seconds_tostring(float sec)
66 float minutes = floor(sec / 60);
68 return sprintf("%d:%02d", minutes, sec);
72 string format_time(float seconds)
74 seconds = floor(seconds + 0.5);
75 float days = floor(seconds / 864000);
76 seconds -= days * 864000;
77 float hours = floor(seconds / 36000);
78 seconds -= hours * 36000;
79 float minutes = floor(seconds / 600);
80 seconds -= minutes * 600;
81 if (days > 0) return sprintf(_("%d days, %02d:%02d:%02d"), days, hours, minutes, seconds);
82 else return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds);
86 string mmsss(float tenths)
88 tenths = floor(tenths + 0.5);
89 float minutes = floor(tenths / 600);
90 tenths -= minutes * 600;
91 string s = ftos(1000 + tenths);
92 return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
96 string mmssss(float hundredths)
98 hundredths = floor(hundredths + 0.5);
99 float minutes = floor(hundredths / 6000);
100 hundredths -= minutes * 6000;
101 string s = ftos(10000 + hundredths);
102 return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2));
105 int ColorTranslateMode;
108 string ColorTranslateRGB(string s)
110 return (ColorTranslateMode & 1) ? strdecolorize(s) : s;
114 // color code replace, place inside of sprintf and parse the string... defaults described as constants
115 // foreground/normal colors
116 string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green // primary priority (important names, etc)
117 string autocvar_hud_colorset_foreground_2 = "3"; // F2 - Yellow // secondary priority (items, locations, numbers, etc)
118 string autocvar_hud_colorset_foreground_3 = "4"; // F3 - Blue // tertiary priority or relatively inconsequential text
119 string autocvar_hud_colorset_foreground_4 = "1"; // F4 - Red // notice/attention grabbing texting
121 string autocvar_hud_colorset_kill_1 = "1"; // K1 - Red // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
122 string autocvar_hud_colorset_kill_2 = "3"; // K2 - Yellow // similar to above, but less important... OR, a highlight out of above message type
123 string autocvar_hud_colorset_kill_3 = "4"; // K3 - Blue // "good" or "beneficial" text (you fragging someone, etc)
125 string autocvar_hud_colorset_background = "7"; // BG - White // neutral/unimportant text
127 /** color code replace, place inside of sprintf and parse the string */
128 string CCR(string input)
130 // foreground/normal colors
131 input = strreplace("^F1", strcat("^", autocvar_hud_colorset_foreground_1), input);
132 input = strreplace("^F2", strcat("^", autocvar_hud_colorset_foreground_2), input);
133 input = strreplace("^F3", strcat("^", autocvar_hud_colorset_foreground_3), input);
134 input = strreplace("^F4", strcat("^", autocvar_hud_colorset_foreground_4), input);
137 input = strreplace("^K1", strcat("^", autocvar_hud_colorset_kill_1), input);
138 input = strreplace("^K2", strcat("^", autocvar_hud_colorset_kill_2), input);
139 input = strreplace("^K3", strcat("^", autocvar_hud_colorset_kill_3), input);
142 input = strreplace("^BG", strcat("^", autocvar_hud_colorset_background), input);
143 input = strreplace("^N", "^7", input); // "none"-- reset to white...
148 #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0)
151 bool startsWithNocase(string haystack, string needle)
153 return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
156 noref string _endsWith_suffix;
157 #define endsWith(this, suffix) (_endsWith_suffix = suffix, substring(this, -strlen(_endsWith_suffix), -1) == _endsWith_suffix)
159 /** unzone the string, and return it as tempstring. Safe to be called on string_null */
161 string fstrunzone(string s)
164 string sc = strcat(s, "");
169 /** returns first word */
173 int o = strstrofs(s, " ", 0);
175 return substring(s, 0, o);
178 /** returns all but first word */
182 int o = strstrofs(s, " ", 0);
183 if (o < 0) return string_null;
184 return substring(s, o + 1, strlen(s) - (o + 1));
188 string cons(string a, string b)
190 if (a == "") return b;
191 if (b == "") return a;
192 return strcat(a, " ", b);
196 string cons_mid(string a, string mid, string b)
198 if (a == "") return b;
199 if (b == "") return a;
200 return strcat(a, mid, b);
204 string substring_range(string s, float b, float e)
206 return substring(s, b, e - b);
210 string swapwords(string str, float i, float j)
213 string s1, s2, s3, s4, s5;
214 float si, ei, sj, ej, s0, en;
215 n = tokenizebyseparator(str, " "); // must match g_maplist processing in ShuffleMaplist and "shuffle"
216 si = argv_start_index(i);
217 sj = argv_start_index(j);
218 ei = argv_end_index(i);
219 ej = argv_end_index(j);
220 s0 = argv_start_index(0);
221 en = argv_end_index(n - 1);
222 s1 = substring_range(str, s0, si);
223 s2 = substring_range(str, si, ei);
224 s3 = substring_range(str, ei, sj);
225 s4 = substring_range(str, sj, ej);
226 s5 = substring_range(str, ej, en);
227 return strcat(s1, s4, s3, s2, s5);
230 string _shufflewords_str;
232 void _shufflewords_swapfunc(float i, float j, entity pass)
234 _shufflewords_str = swapwords(_shufflewords_str, i, j);
238 string shufflewords(string str)
240 _shufflewords_str = str;
241 int n = tokenizebyseparator(str, " ");
242 shuffle(n, _shufflewords_swapfunc, NULL);
243 str = _shufflewords_str;
244 _shufflewords_str = string_null;
249 string unescape(string in)
251 in = strzone(in); // but it doesn't seem to be necessary in my tests at least
253 int len = strlen(in);
255 for (int i = 0; i < len; ++i)
257 string s = substring(in, i, 1);
260 s = substring(in, i + 1, 1);
261 if (s == "n") str = strcat(str, "\n");
262 else if (s == "\\") str = strcat(str, "\\");
263 else str = strcat(str, substring(in, i, 2));
267 str = strcat(str, s);
274 string strwords(string s, int w)
277 for ( ; w && endpos >= 0; --w)
278 endpos = strstrofs(s, " ", endpos + 1);
279 if (endpos < 0) return s;
280 return substring(s, 0, endpos);
283 #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0)
286 int u8_strsize(string s)
289 for (int i = 0, c; (c = str2chr(s, i)) > 0; ++i, ++l)
298 // List of Unicode spaces: http://jkorpela.fi/chars/spaces.html
300 bool isInvisibleString(string s)
302 s = strdecolorize(s);
303 bool utf8 = cvar("utf8_enable");
304 for (int i = 0, n = strlen(s); i < n; ++i)
306 int c = str2chr(s, i);
312 case 192: // charmap space
315 case 0xE000 + 192: // utf8 charmap space
316 case 0x00A0: // NO-BREAK SPACE
317 //case 0x1680: // OGHAM SPACE MARK
318 case 0x180E: // MONGOLIAN VOWEL SEPARATOR
319 case 0x2000: // EN QUAD
320 case 0x2001: // EM QUAD
321 case 0x2002: // EN SPACE
322 case 0x2003: // EM SPACE
323 case 0x2004: // THREE-PER-EM SPACE
324 case 0x2005: // FOUR-PER-EM SPACE
325 case 0x2006: // SIX-PER-EM SPACE
326 case 0x2007: // FIGURE SPACE
327 case 0x2008: // PUNCTUATION SPACE
328 case 0x2009: // THIN SPACE
329 case 0x200A: // HAIR SPACE
330 case 0x200B: // ZERO WIDTH SPACE
331 case 0x202F: // NARROW NO-BREAK SPACE
332 case 0x205F: // MEDIUM MATHEMATICAL SPACE
333 case 0x3000: // IDEOGRAPHIC SPACE
334 case 0xFEFF: // ZERO WIDTH NO-BREAK SPACE
343 // Multiline text file buffers
346 int buf_load(string pFilename)
348 int buf = buf_create();
349 if (buf < 0) return -1;
350 int fh = fopen(pFilename, FILE_READ);
357 for (int i = 0; (l = fgets(fh)); ++i)
358 bufstr_set(buf, i, l);
364 void buf_save(float buf, string pFilename)
366 int fh = fopen(pFilename, FILE_WRITE);
367 if (fh < 0) error(strcat("Can't write buf to ", pFilename));
368 int n = buf_getsize(buf);
369 for (int i = 0; i < n; ++i)
370 fputs(fh, strcat(bufstr_get(buf, i), "\n"));
375 * converts a number to a string with the indicated number of decimals
378 string ftos_decimals(float number, int decimals)
380 // inhibit stupid negative zero
381 if (number == 0) number = 0;
382 return sprintf("%.*f", decimals, number);
386 * converts a number to a string with the minimum number of decimals
389 string ftos_mindecimals(float number)
391 // inhibit stupid negative zero
392 if (number == 0) number = 0;
393 return sprintf("%.7g", number);
397 int vercmp_recursive(string v1, string v2)
399 int dot1 = strstrofs(v1, ".", 0);
400 int dot2 = strstrofs(v2, ".", 0);
401 string s1 = (dot1 == -1) ? v1 : substring(v1, 0, dot1);
402 string s2 = (dot2 == -1) ? v2 : substring(v2, 0, dot2);
405 r = stof(s1) - stof(s2);
406 if (r != 0) return r;
408 r = strcasecmp(s1, s2);
409 if (r != 0) return r;
411 if (dot1 == -1) return (dot2 == -1) ? 0 : -1;
412 else return (dot2 == -1) ? 1 : vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
416 int vercmp(string v1, string v2)
418 if (strcasecmp(v1, v2) == 0) return 0; // early out check
421 if (v1 == "git") return 1;
422 if (v2 == "git") return -1;
424 return vercmp_recursive(v1, v2);
427 const string HEXDIGITS_MINSET = "0123456789ABCDEFabcdef";
428 const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
429 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
430 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
431 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS_MINSET, (d), 1))
432 #define IS_HEXDIGIT(d) (strstrofs(HEXDIGITS_MINSET, (d), 0) >= 0)
434 const string DIGITS = "0123456789";
435 #define IS_DIGIT(d) (strstrofs(DIGITS, (d), 0) >= 0)