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 \
63 // Returns the number of days since 0000-03-01 (March 1, year 0)
64 // Starting counting from March, as the 1st month of the year, February becomes the 12th and last month,
65 // so its variable duration does not affect, given that the 29th is the last day of the period
67 int days_up_to_date(int Y, int M, int D)
69 int years = (M <= 2) ? Y - 1 : Y;
71 int leap_days = floor(years / 4) - floor(years / 100) + floor(years / 400);
73 // using these 2 formulas to save 2 arrays or switches (performance isn't important here)
74 int months = (M <= 2) ? (M + 9) : (M - 3); // 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
75 int leftover_days = (M <= 2) ? (M + 5) : floor(0.58 * M - 1.1); // 6, 7, 0, 1, 1, 2, 2, 3, 4, 4, 5, 5
77 int month_days = 30 * months + leftover_days;
79 return 365 * years + month_days + D + leap_days;
82 #define DAYS_UP_TO_EPOCH 719469 // days_up_to_date(1970, 1, 1);
84 // Returns the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
85 // This function exists only as a replacement for strftime(false, "%s") which doesn't work
86 // on Windows (%s is not supported) and at least in some linux systems doesn't return the
88 // NOTE: at the current date, the number (string) returned by both strftime(false, "%s") and
89 // strftime_s() is so high that can't be converted to int (with ftos) without precision loss
93 string date = strftime(false, "%Y-%m-%d %H:%M:%S");
95 i =0; int Y = stof(substring(date, i, 4)); // years
96 i+=5; int M = stof(substring(date, i, 2)); // months
97 i+=3; int D = stof(substring(date, i, 2)); // days
99 i+=3; seconds += stof(substring(date, i, 2)) * 60 * 60; // hours
100 i+=3; seconds += stof(substring(date, i, 2)) * 60; // minutes
101 i+=3; seconds += stof(substring(date, i, 2)); // seconds
103 // doing so we loose precision
104 //seconds += (days_up_to_date(Y, M, D) - DAYS_UP_TO_EPOCH) * 24 * 60 * 60;
105 //return ftos(seconds);
107 int days_since_epoch = days_up_to_date(Y, M, D) - DAYS_UP_TO_EPOCH;
108 // use hundreds of seconds as unit to avoid precision loss
109 int hundreds_of_seconds = days_since_epoch * 24 * 6 * 6;
110 hundreds_of_seconds += floor(seconds / 100);
112 // tens of seconds and seconds
113 string seconds_str = ftos(seconds % 100);
114 if ((seconds % 100) < 10)
115 seconds_str = strcat("0", seconds_str);
117 return strcat(ftos(hundreds_of_seconds), seconds_str);
120 /// \param[in] seconds number of seconds, can be negative too
121 /// \return time as "m:ss" string (floored)
123 string seconds_tostring(float seconds)
125 bool negative = false;
130 if (floor(seconds) != seconds)
131 seconds += 1; // make floor work in the other direction
133 int minutes = floor(seconds / 60);
134 seconds -= minutes * 60;
136 return sprintf("-%d:%02d", minutes, seconds);
137 return sprintf("%d:%02d", minutes, seconds);
141 string format_time(float seconds)
143 seconds = floor(seconds + 0.5);
144 float days = floor(seconds / 864000);
145 seconds -= days * 864000;
146 float hours = floor(seconds / 36000);
147 seconds -= hours * 36000;
148 float minutes = floor(seconds / 600);
149 seconds -= minutes * 600;
150 if (days > 0) return sprintf(_("%d days, %02d:%02d:%02d"), days, hours, minutes, seconds);
151 else return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds);
155 string mmsss(float tenths)
157 tenths = floor(tenths + 0.5);
158 float minutes = floor(tenths / 600);
159 tenths -= minutes * 600;
160 string s = ftos(1000 + tenths);
161 return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
165 string mmssss(float hundredths)
167 hundredths = floor(hundredths + 0.5);
168 float minutes = floor(hundredths / 6000);
169 hundredths -= minutes * 6000;
170 string s = ftos(10000 + hundredths);
171 return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2));
174 int ColorTranslateMode;
177 string ColorTranslateRGB(string s)
179 return (ColorTranslateMode & 1) ? strdecolorize(s) : s;
183 // color code replace, place inside of sprintf and parse the string... defaults described as constants
184 // foreground/normal colors
185 string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green // primary priority (important names, etc)
186 string autocvar_hud_colorset_foreground_2 = "3"; // F2 - Yellow // secondary priority (items, locations, numbers, etc)
187 string autocvar_hud_colorset_foreground_3 = "4"; // F3 - Blue // tertiary priority or relatively inconsequential text
188 string autocvar_hud_colorset_foreground_4 = "1"; // F4 - Red // notice/attention grabbing texting
190 string autocvar_hud_colorset_kill_1 = "1"; // K1 - Red // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
191 string autocvar_hud_colorset_kill_2 = "3"; // K2 - Yellow // similar to above, but less important... OR, a highlight out of above message type
192 string autocvar_hud_colorset_kill_3 = "4"; // K3 - Blue // "good" or "beneficial" text (you fragging someone, etc)
194 string autocvar_hud_colorset_background = "7"; // BG - White // neutral/unimportant text
196 /** color code replace, place inside of sprintf and parse the string */
197 string CCR(string input)
199 // foreground/normal colors
200 input = strreplace("^F1", strcat("^", autocvar_hud_colorset_foreground_1), input);
201 input = strreplace("^F2", strcat("^", autocvar_hud_colorset_foreground_2), input);
202 input = strreplace("^F3", strcat("^", autocvar_hud_colorset_foreground_3), input);
203 input = strreplace("^F4", strcat("^", autocvar_hud_colorset_foreground_4), input);
206 input = strreplace("^K1", strcat("^", autocvar_hud_colorset_kill_1), input);
207 input = strreplace("^K2", strcat("^", autocvar_hud_colorset_kill_2), input);
208 input = strreplace("^K3", strcat("^", autocvar_hud_colorset_kill_3), input);
211 input = strreplace("^BG", strcat("^", autocvar_hud_colorset_background), input);
212 input = strreplace("^N", "^7", input); // "none"-- reset to white...
217 #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0)
220 bool startsWithNocase(string haystack, string needle)
222 return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
225 noref string _endsWith_suffix;
226 #define endsWith(this, suffix) (_endsWith_suffix = suffix, substring(this, -strlen(_endsWith_suffix), -1) == _endsWith_suffix)
228 /** unzone the string, and return it as tempstring. Safe to be called on string_null */
230 string fstrunzone(string s)
233 string sc = strcat(s, "");
238 /** returns first word */
242 int o = strstrofs(s, " ", 0);
244 return substring(s, 0, o);
247 /** returns all but first word */
251 int o = strstrofs(s, " ", 0);
252 if (o < 0) return string_null;
253 return substring(s, o + 1, strlen(s) - (o + 1));
257 string cons(string a, string b)
259 if (a == "") return b;
260 if (b == "") return a;
261 return strcat(a, " ", b);
265 string cons_mid(string a, string mid, string b)
267 if (a == "") return b;
268 if (b == "") return a;
269 return strcat(a, mid, b);
273 string substring_range(string s, float b, float e)
275 return substring(s, b, e - b);
279 string swapwords(string str, float i, float j)
282 string s1, s2, s3, s4, s5;
283 float si, ei, sj, ej, s0, en;
284 n = tokenizebyseparator(str, " "); // must match g_maplist processing in ShuffleMaplist and "shuffle"
285 si = argv_start_index(i);
286 sj = argv_start_index(j);
287 ei = argv_end_index(i);
288 ej = argv_end_index(j);
289 s0 = argv_start_index(0);
290 en = argv_end_index(n - 1);
291 s1 = substring_range(str, s0, si);
292 s2 = substring_range(str, si, ei);
293 s3 = substring_range(str, ei, sj);
294 s4 = substring_range(str, sj, ej);
295 s5 = substring_range(str, ej, en);
296 return strcat(s1, s4, s3, s2, s5);
299 string _shufflewords_str;
301 void _shufflewords_swapfunc(float i, float j, entity pass)
303 _shufflewords_str = swapwords(_shufflewords_str, i, j);
307 string shufflewords(string str)
309 _shufflewords_str = str;
310 int n = tokenizebyseparator(str, " ");
311 shuffle(n, _shufflewords_swapfunc, NULL);
312 str = _shufflewords_str;
313 _shufflewords_str = string_null;
318 string unescape(string in)
320 in = strzone(in); // but it doesn't seem to be necessary in my tests at least
322 int len = strlen(in);
324 for (int i = 0; i < len; ++i)
326 string s = substring(in, i, 1);
329 s = substring(in, i + 1, 1);
330 if (s == "n") str = strcat(str, "\n");
331 else if (s == "\\") str = strcat(str, "\\");
332 else str = strcat(str, substring(in, i, 2));
336 str = strcat(str, s);
343 string strwords(string s, int w)
346 for ( ; w && endpos >= 0; --w)
347 endpos = strstrofs(s, " ", endpos + 1);
348 if (endpos < 0) return s;
349 return substring(s, 0, endpos);
352 #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0)
355 int u8_strsize(string s)
358 for (int i = 0, c; (c = str2chr(s, i)) > 0; ++i, ++l)
367 // List of Unicode spaces: http://jkorpela.fi/chars/spaces.html
369 bool isInvisibleString(string s)
371 s = strdecolorize(s);
372 bool utf8 = cvar("utf8_enable");
373 for (int i = 0, n = strlen(s); i < n; ++i)
375 int c = str2chr(s, i);
381 case 192: // charmap space
384 case 0xE000: // invisible char of the utf8 quake charmap
385 case 0xE00A: // invisible char of the utf8 quake charmap
386 case 0xE0A0: // invisible char of the utf8 quake charmap
387 case 0xE020: // invisible char of the utf8 quake charmap
388 case 0x00A0: // NO-BREAK SPACE
389 //case 0x1680: // OGHAM SPACE MARK
390 case 0x180E: // MONGOLIAN VOWEL SEPARATOR
391 case 0x2000: // EN QUAD
392 case 0x2001: // EM QUAD
393 case 0x2002: // EN SPACE
394 case 0x2003: // EM SPACE
395 case 0x2004: // THREE-PER-EM SPACE
396 case 0x2005: // FOUR-PER-EM SPACE
397 case 0x2006: // SIX-PER-EM SPACE
398 case 0x2007: // FIGURE SPACE
399 case 0x2008: // PUNCTUATION SPACE
400 case 0x2009: // THIN SPACE
401 case 0x200A: // HAIR SPACE
402 case 0x200B: // ZERO WIDTH SPACE
403 case 0x202F: // NARROW NO-BREAK SPACE
404 case 0x205F: // MEDIUM MATHEMATICAL SPACE
405 case 0x3000: // IDEOGRAPHIC SPACE
406 case 0xFEFF: // ZERO WIDTH NO-BREAK SPACE
415 // Multiline text file buffers
418 int buf_load(string pFilename)
420 int buf = buf_create();
421 if (buf < 0) return -1;
422 int fh = fopen(pFilename, FILE_READ);
429 for (int i = 0; (l = fgets(fh)); ++i)
430 bufstr_set(buf, i, l);
436 void buf_save(float buf, string pFilename)
438 int fh = fopen(pFilename, FILE_WRITE);
439 if (fh < 0) error(strcat("Can't write buf to ", pFilename));
440 int n = buf_getsize(buf);
441 for (int i = 0; i < n; ++i)
442 fputs(fh, strcat(bufstr_get(buf, i), "\n"));
447 * converts a number to a string with the indicated number of decimals
450 string ftos_decimals(float number, int decimals)
452 // inhibit stupid negative zero
453 if (number == 0) number = 0;
454 return sprintf("%.*f", decimals, number);
458 * converts a number to a string with the minimum number of decimals
461 string ftos_mindecimals(float number)
463 // inhibit stupid negative zero
464 if (number == 0) number = 0;
465 return sprintf("%.7g", number);
469 int vercmp_recursive(string v1, string v2)
471 int dot1 = strstrofs(v1, ".", 0);
472 int dot2 = strstrofs(v2, ".", 0);
473 string s1 = (dot1 == -1) ? v1 : substring(v1, 0, dot1);
474 string s2 = (dot2 == -1) ? v2 : substring(v2, 0, dot2);
477 r = stof(s1) - stof(s2);
478 if (r != 0) return r;
480 r = strcasecmp(s1, s2);
481 if (r != 0) return r;
483 if (dot1 == -1) return (dot2 == -1) ? 0 : -1;
484 else return (dot2 == -1) ? 1 : vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
488 int vercmp(string v1, string v2)
490 if (strcasecmp(v1, v2) == 0) return 0; // early out check
493 if (v1 == "git") return 1;
494 if (v2 == "git") return -1;
496 return vercmp_recursive(v1, v2);
499 const string HEXDIGITS_MINSET = "0123456789ABCDEFabcdef";
500 const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
501 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
502 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
503 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS_MINSET, (d), 1))
504 #define IS_HEXDIGIT(d) (strstrofs(HEXDIGITS_MINSET, (d), 0) >= 0)
506 const string DIGITS = "0123456789";
507 #define IS_DIGIT(d) (strstrofs(DIGITS, (d), 0) >= 0)
509 // returns true if the caret at position pos is escaped
511 bool isCaretEscaped(string theText, float pos)
513 // count all the previous carets
515 while(pos - carets >= 1 && substring(theText, pos - carets - 1, 1) == "^")
517 // if number of previous carets is odd then this carets is escaped
522 bool isValidColorCodeValue(string theText, int cc_len, int tag_start)
525 return IS_DIGIT(substring(theText, tag_start + 1, 1));
527 return (IS_HEXDIGIT(substring(theText, tag_start + 2, 1))
528 && IS_HEXDIGIT(substring(theText, tag_start + 3, 1))
529 && IS_HEXDIGIT(substring(theText, tag_start + 4, 1)));
533 // it returns 0 if pos is NOT in the middle or at the end of a color code
534 // otherwise it returns a vector with color code length as the first component
535 // and the offset from '^' position to pos as the second component
537 // "j^2kl" | returns 0 if pos == 0 or 1 or 4
538 // ^^ | returns '2 1' or '2 2' if pos == 2 or 3
540 vector checkColorCode(string theText, int text_len, int pos, bool check_at_the_end)
543 text_len = strlen(theText);
544 string tag_type = "^";
548 LABEL(check_color_tag)
551 if (!check_at_the_end)
553 for (; ofs >= 1; ofs--)
555 if (!(pos >= ofs && text_len >= pos + (cc_len - ofs)))
557 if(substring(theText, pos - ofs, tag_len) == tag_type)
559 if (!isCaretEscaped(theText, pos - ofs) && isValidColorCodeValue(theText, cc_len, pos - ofs))
560 return eX * cc_len + eY * ofs;
568 goto check_color_tag;