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);
30 string seconds_tostring(float sec)
32 float minutes = floor(sec / 60);
34 return sprintf("%d:%02d", minutes, sec);
37 string format_time(float seconds)
39 seconds = floor(seconds + 0.5);
40 float days = floor(seconds / 864000);
41 seconds -= days * 864000;
42 float hours = floor(seconds / 36000);
43 seconds -= hours * 36000;
44 float minutes = floor(seconds / 600);
45 seconds -= minutes * 600;
46 if (days > 0) return sprintf(_("%d days, %02d:%02d:%02d"), days, hours, minutes, seconds);
47 else return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds);
50 string mmsss(float tenths)
52 tenths = floor(tenths + 0.5);
53 float minutes = floor(tenths / 600);
54 tenths -= minutes * 600;
55 string s = ftos(1000 + tenths);
56 return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
59 string mmssss(float hundredths)
61 hundredths = floor(hundredths + 0.5);
62 float minutes = floor(hundredths / 6000);
63 hundredths -= minutes * 6000;
64 string s = ftos(10000 + hundredths);
65 return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2));
68 int ColorTranslateMode;
70 string ColorTranslateRGB(string s)
72 return (ColorTranslateMode & 1) ? strdecolorize(s) : s;
76 // color code replace, place inside of sprintf and parse the string... defaults described as constants
77 // foreground/normal colors
78 string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green // primary priority (important names, etc)
79 string autocvar_hud_colorset_foreground_2 = "3"; // F2 - Yellow // secondary priority (items, locations, numbers, etc)
80 string autocvar_hud_colorset_foreground_3 = "4"; // F3 - Blue // tertiary priority or relatively inconsequential text
81 string autocvar_hud_colorset_foreground_4 = "1"; // F4 - Red // notice/attention grabbing texting
83 string autocvar_hud_colorset_kill_1 = "1"; // K1 - Red // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
84 string autocvar_hud_colorset_kill_2 = "3"; // K2 - Yellow // similar to above, but less important... OR, a highlight out of above message type
85 string autocvar_hud_colorset_kill_3 = "4"; // K3 - Blue // "good" or "beneficial" text (you fragging someone, etc)
87 string autocvar_hud_colorset_background = "7"; // BG - White // neutral/unimportant text
89 /** color code replace, place inside of sprintf and parse the string */
90 string CCR(string input)
92 // See the autocvar declarations in util.qh for default values
94 // foreground/normal colors
95 input = strreplace("^F1", strcat("^", autocvar_hud_colorset_foreground_1), input);
96 input = strreplace("^F2", strcat("^", autocvar_hud_colorset_foreground_2), input);
97 input = strreplace("^F3", strcat("^", autocvar_hud_colorset_foreground_3), input);
98 input = strreplace("^F4", strcat("^", autocvar_hud_colorset_foreground_4), input);
101 input = strreplace("^K1", strcat("^", autocvar_hud_colorset_kill_1), input);
102 input = strreplace("^K2", strcat("^", autocvar_hud_colorset_kill_2), input);
103 input = strreplace("^K3", strcat("^", autocvar_hud_colorset_kill_3), input);
106 input = strreplace("^BG", strcat("^", autocvar_hud_colorset_background), input);
107 input = strreplace("^N", "^7", input); // "none"-- reset to white...
112 #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0)
114 bool startsWithNocase(string haystack, string needle)
116 return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
119 noref string _endsWith_suffix;
120 #define endsWith(this, suffix) (_endsWith_suffix = suffix, substring(this, -strlen(_endsWith_suffix), -1) == _endsWith_suffix)
122 /** unzone the string, and return it as tempstring. Safe to be called on string_null */
123 string fstrunzone(string s)
126 string sc = strcat(s, "");
131 /** returns first word */
134 int o = strstrofs(s, " ", 0);
136 return substring(s, 0, o);
139 /** returns all but first word */
142 int o = strstrofs(s, " ", 0);
143 if (o < 0) return string_null;
144 return substring(s, o + 1, strlen(s) - (o + 1));
147 string cons(string a, string b)
149 if (a == "") return b;
150 if (b == "") return a;
151 return strcat(a, " ", b);
154 string substring_range(string s, float b, float e)
156 return substring(s, b, e - b);
159 string swapwords(string str, float i, float j)
162 string s1, s2, s3, s4, s5;
163 float si, ei, sj, ej, s0, en;
164 n = tokenizebyseparator(str, " "); // must match g_maplist processing in ShuffleMaplist and "shuffle"
165 si = argv_start_index(i);
166 sj = argv_start_index(j);
167 ei = argv_end_index(i);
168 ej = argv_end_index(j);
169 s0 = argv_start_index(0);
170 en = argv_end_index(n - 1);
171 s1 = substring_range(str, s0, si);
172 s2 = substring_range(str, si, ei);
173 s3 = substring_range(str, ei, sj);
174 s4 = substring_range(str, sj, ej);
175 s5 = substring_range(str, ej, en);
176 return strcat(s1, s4, s3, s2, s5);
179 string _shufflewords_str;
180 void _shufflewords_swapfunc(float i, float j, entity pass)
182 _shufflewords_str = swapwords(_shufflewords_str, i, j);
184 string shufflewords(string str)
186 _shufflewords_str = str;
187 int n = tokenizebyseparator(str, " ");
188 shuffle(n, _shufflewords_swapfunc, NULL);
189 str = _shufflewords_str;
190 _shufflewords_str = string_null;
194 string unescape(string in)
196 in = strzone(in); // but it doesn't seem to be necessary in my tests at least
198 int len = strlen(in);
200 for (int i = 0; i < len; ++i)
202 string s = substring(in, i, 1);
205 s = substring(in, i + 1, 1);
206 if (s == "n") str = strcat(str, "\n");
207 else if (s == "\\") str = strcat(str, "\\");
208 else str = strcat(str, substring(in, i, 2));
212 str = strcat(str, s);
218 string strwords(string s, int w)
221 for ( ; w && endpos >= 0; --w)
222 endpos = strstrofs(s, " ", endpos + 1);
223 if (endpos < 0) return s;
224 return substring(s, 0, endpos);
227 #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0)
229 int u8_strsize(string s)
232 for (int i = 0, c; (c = str2chr(s, i)) > 0; ++i, ++l)
241 bool isInvisibleString(string s)
243 s = strdecolorize(s);
244 bool utf8 = cvar("utf8_enable");
245 for (int i = 0, n = strlen(s); i < n; ++i)
247 int c = str2chr(s, i);
253 case 192: // charmap space
256 case 160: // space in unicode fonts
257 case 0xE000 + 192: // utf8 charmap space
266 // Multiline text file buffers
268 int buf_load(string pFilename)
270 int buf = buf_create();
271 if (buf < 0) return -1;
272 int fh = fopen(pFilename, FILE_READ);
279 for (int i = 0; (l = fgets(fh)); ++i)
280 bufstr_set(buf, i, l);
285 void buf_save(float buf, string pFilename)
287 int fh = fopen(pFilename, FILE_WRITE);
288 if (fh < 0) error(strcat("Can't write buf to ", pFilename));
289 int n = buf_getsize(buf);
290 for (int i = 0; i < n; ++i)
291 fputs(fh, strcat(bufstr_get(buf, i), "\n"));
296 * converts a number to a string with the indicated number of decimals
298 string ftos_decimals(float number, int decimals)
300 // inhibit stupid negative zero
301 if (number == 0) number = 0;
302 return sprintf("%.*f", decimals, number);
306 * converts a number to a string with the minimum number of decimals
308 string ftos_mindecimals(float number)
310 // inhibit stupid negative zero
311 if (number == 0) number = 0;
312 return sprintf("%.7g", number);
315 int vercmp_recursive(string v1, string v2)
317 int dot1 = strstrofs(v1, ".", 0);
318 int dot2 = strstrofs(v2, ".", 0);
319 string s1 = (dot1 == -1) ? v1 : substring(v1, 0, dot1);
320 string s2 = (dot2 == -1) ? v2 : substring(v2, 0, dot2);
323 r = stof(s1) - stof(s2);
324 if (r != 0) return r;
326 r = strcasecmp(s1, s2);
327 if (r != 0) return r;
329 if (dot1 == -1) return (dot2 == -1) ? 0 : -1;
330 else return (dot2 == -1) ? 1 : vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
333 int vercmp(string v1, string v2)
335 if (strcasecmp(v1, v2) == 0) return 0; // early out check
338 if (v1 == "git") return 1;
339 if (v2 == "git") return -1;
341 return vercmp_recursive(v1, v2);
344 const string HEXDIGITS_MINSET = "0123456789ABCDEFabcdef";
345 const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
346 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
347 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
348 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS_MINSET, (d), 1))
349 #define IS_HEXDIGIT(d) (strstrofs(HEXDIGITS_MINSET, (d), 0) >= 0)
351 const string DIGITS = "0123456789";
352 #define IS_DIGIT(d) (strstrofs(DIGITS, (d), 0) >= 0)