8 float stringwidth_colors(string s, vector theSize)
10 return stringwidth_builtin(s, true, theSize);
13 float stringwidth_nocolors(string s, vector theSize)
15 return stringwidth_builtin(s, false, theSize);
19 float stringwidth_colors(string s, vector theSize)
21 return stringwidth(s, true, theSize);
24 float stringwidth_nocolors(string s, vector theSize)
26 return stringwidth(s, false, theSize);
31 string seconds_tostring(float sec)
33 float minutes = floor(sec / 60);
35 return sprintf("%d:%02d", minutes, sec);
39 string format_time(float seconds)
41 seconds = floor(seconds + 0.5);
42 float days = floor(seconds / 864000);
43 seconds -= days * 864000;
44 float hours = floor(seconds / 36000);
45 seconds -= hours * 36000;
46 float minutes = floor(seconds / 600);
47 seconds -= minutes * 600;
48 if (days > 0) return sprintf(_("%d days, %02d:%02d:%02d"), days, hours, minutes, seconds);
49 else return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds);
53 string mmsss(float tenths)
55 tenths = floor(tenths + 0.5);
56 float minutes = floor(tenths / 600);
57 tenths -= minutes * 600;
58 string s = ftos(1000 + tenths);
59 return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
63 string mmssss(float hundredths)
65 hundredths = floor(hundredths + 0.5);
66 float minutes = floor(hundredths / 6000);
67 hundredths -= minutes * 6000;
68 string s = ftos(10000 + hundredths);
69 return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2));
72 int ColorTranslateMode;
75 string ColorTranslateRGB(string s)
77 return (ColorTranslateMode & 1) ? strdecolorize(s) : s;
81 // color code replace, place inside of sprintf and parse the string... defaults described as constants
82 // foreground/normal colors
83 string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green // primary priority (important names, etc)
84 string autocvar_hud_colorset_foreground_2 = "3"; // F2 - Yellow // secondary priority (items, locations, numbers, etc)
85 string autocvar_hud_colorset_foreground_3 = "4"; // F3 - Blue // tertiary priority or relatively inconsequential text
86 string autocvar_hud_colorset_foreground_4 = "1"; // F4 - Red // notice/attention grabbing texting
88 string autocvar_hud_colorset_kill_1 = "1"; // K1 - Red // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
89 string autocvar_hud_colorset_kill_2 = "3"; // K2 - Yellow // similar to above, but less important... OR, a highlight out of above message type
90 string autocvar_hud_colorset_kill_3 = "4"; // K3 - Blue // "good" or "beneficial" text (you fragging someone, etc)
92 string autocvar_hud_colorset_background = "7"; // BG - White // neutral/unimportant text
94 /** color code replace, place inside of sprintf and parse the string */
95 string CCR(string input)
97 // See the autocvar declarations in util.qh for default values
99 // foreground/normal colors
100 input = strreplace("^F1", strcat("^", autocvar_hud_colorset_foreground_1), input);
101 input = strreplace("^F2", strcat("^", autocvar_hud_colorset_foreground_2), input);
102 input = strreplace("^F3", strcat("^", autocvar_hud_colorset_foreground_3), input);
103 input = strreplace("^F4", strcat("^", autocvar_hud_colorset_foreground_4), input);
106 input = strreplace("^K1", strcat("^", autocvar_hud_colorset_kill_1), input);
107 input = strreplace("^K2", strcat("^", autocvar_hud_colorset_kill_2), input);
108 input = strreplace("^K3", strcat("^", autocvar_hud_colorset_kill_3), input);
111 input = strreplace("^BG", strcat("^", autocvar_hud_colorset_background), input);
112 input = strreplace("^N", "^7", input); // "none"-- reset to white...
117 #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0)
120 bool startsWithNocase(string haystack, string needle)
122 return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
125 noref string _endsWith_suffix;
126 #define endsWith(this, suffix) (_endsWith_suffix = suffix, substring(this, -strlen(_endsWith_suffix), -1) == _endsWith_suffix)
128 /** unzone the string, and return it as tempstring. Safe to be called on string_null */
130 string fstrunzone(string s)
133 string sc = strcat(s, "");
138 /** returns first word */
142 int o = strstrofs(s, " ", 0);
144 return substring(s, 0, o);
147 /** returns all but first word */
151 int o = strstrofs(s, " ", 0);
152 if (o < 0) return string_null;
153 return substring(s, o + 1, strlen(s) - (o + 1));
157 string cons(string a, string b)
159 if (a == "") return b;
160 if (b == "") return a;
161 return strcat(a, " ", b);
165 string substring_range(string s, float b, float e)
167 return substring(s, b, e - b);
171 string swapwords(string str, float i, float j)
174 string s1, s2, s3, s4, s5;
175 float si, ei, sj, ej, s0, en;
176 n = tokenizebyseparator(str, " "); // must match g_maplist processing in ShuffleMaplist and "shuffle"
177 si = argv_start_index(i);
178 sj = argv_start_index(j);
179 ei = argv_end_index(i);
180 ej = argv_end_index(j);
181 s0 = argv_start_index(0);
182 en = argv_end_index(n - 1);
183 s1 = substring_range(str, s0, si);
184 s2 = substring_range(str, si, ei);
185 s3 = substring_range(str, ei, sj);
186 s4 = substring_range(str, sj, ej);
187 s5 = substring_range(str, ej, en);
188 return strcat(s1, s4, s3, s2, s5);
191 string _shufflewords_str;
193 void _shufflewords_swapfunc(float i, float j, entity pass)
195 _shufflewords_str = swapwords(_shufflewords_str, i, j);
199 string shufflewords(string str)
201 _shufflewords_str = str;
202 int n = tokenizebyseparator(str, " ");
203 shuffle(n, _shufflewords_swapfunc, NULL);
204 str = _shufflewords_str;
205 _shufflewords_str = string_null;
210 string unescape(string in)
212 in = strzone(in); // but it doesn't seem to be necessary in my tests at least
214 int len = strlen(in);
216 for (int i = 0; i < len; ++i)
218 string s = substring(in, i, 1);
221 s = substring(in, i + 1, 1);
222 if (s == "n") str = strcat(str, "\n");
223 else if (s == "\\") str = strcat(str, "\\");
224 else str = strcat(str, substring(in, i, 2));
228 str = strcat(str, s);
235 string strwords(string s, int w)
238 for ( ; w && endpos >= 0; --w)
239 endpos = strstrofs(s, " ", endpos + 1);
240 if (endpos < 0) return s;
241 return substring(s, 0, endpos);
244 #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0)
247 int u8_strsize(string s)
250 for (int i = 0, c; (c = str2chr(s, i)) > 0; ++i, ++l)
260 bool isInvisibleString(string s)
262 s = strdecolorize(s);
263 bool utf8 = cvar("utf8_enable");
264 for (int i = 0, n = strlen(s); i < n; ++i)
266 int c = str2chr(s, i);
272 case 192: // charmap space
275 case 160: // space in unicode fonts
276 case 0xE000 + 192: // utf8 charmap space
285 // Multiline text file buffers
288 int buf_load(string pFilename)
290 int buf = buf_create();
291 if (buf < 0) return -1;
292 int fh = fopen(pFilename, FILE_READ);
299 for (int i = 0; (l = fgets(fh)); ++i)
300 bufstr_set(buf, i, l);
306 void buf_save(float buf, string pFilename)
308 int fh = fopen(pFilename, FILE_WRITE);
309 if (fh < 0) error(strcat("Can't write buf to ", pFilename));
310 int n = buf_getsize(buf);
311 for (int i = 0; i < n; ++i)
312 fputs(fh, strcat(bufstr_get(buf, i), "\n"));
317 * converts a number to a string with the indicated number of decimals
320 string ftos_decimals(float number, int decimals)
322 // inhibit stupid negative zero
323 if (number == 0) number = 0;
324 return sprintf("%.*f", decimals, number);
328 * converts a number to a string with the minimum number of decimals
331 string ftos_mindecimals(float number)
333 // inhibit stupid negative zero
334 if (number == 0) number = 0;
335 return sprintf("%.7g", number);
339 int vercmp_recursive(string v1, string v2)
341 int dot1 = strstrofs(v1, ".", 0);
342 int dot2 = strstrofs(v2, ".", 0);
343 string s1 = (dot1 == -1) ? v1 : substring(v1, 0, dot1);
344 string s2 = (dot2 == -1) ? v2 : substring(v2, 0, dot2);
347 r = stof(s1) - stof(s2);
348 if (r != 0) return r;
350 r = strcasecmp(s1, s2);
351 if (r != 0) return r;
353 if (dot1 == -1) return (dot2 == -1) ? 0 : -1;
354 else return (dot2 == -1) ? 1 : vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
358 int vercmp(string v1, string v2)
360 if (strcasecmp(v1, v2) == 0) return 0; // early out check
363 if (v1 == "git") return 1;
364 if (v2 == "git") return -1;
366 return vercmp_recursive(v1, v2);
369 const string HEXDIGITS_MINSET = "0123456789ABCDEFabcdef";
370 const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
371 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
372 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
373 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS_MINSET, (d), 1))
374 #define IS_HEXDIGIT(d) (strstrofs(HEXDIGITS_MINSET, (d), 0) >= 0)
376 const string DIGITS = "0123456789";
377 #define IS_DIGIT(d) (strstrofs(DIGITS, (d), 0) >= 0)