9 float stringwidth_colors(string s, vector theSize)
11 return stringwidth(s, true, theSize);
14 float stringwidth_nocolors(string s, vector theSize)
16 return stringwidth(s, false, theSize);
21 string seconds_tostring(float sec)
23 float minutes = floor(sec / 60);
25 return sprintf("%d:%02d", minutes, sec);
28 string format_time(float seconds)
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);
41 string mmsss(float tenths)
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));
50 string mmssss(float hundredths)
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));
59 int ColorTranslateMode;
61 string ColorTranslateRGB(string s)
63 return (ColorTranslateMode & 1) ? strdecolorize(s) : s;
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
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)
77 string autocvar_hud_colorset_background = "7"; // BG - White // neutral/unimportant text
79 /** color code replace, place inside of sprintf and parse the string */
80 string CCR(string input)
82 // See the autocvar declarations in util.qh for default values
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);
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);
96 input = strreplace("^BG", strcat("^", autocvar_hud_colorset_background), input);
97 input = strreplace("^N", "^7", input); // "none"-- reset to white...
101 #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0)
103 bool startsWithNocase(string haystack, string needle)
105 return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
108 /** unzone the string, and return it as tempstring. Safe to be called on string_null */
109 string fstrunzone(string s)
112 string sc = strcat(s, "");
117 /** returns first word */
120 int o = strstrofs(s, " ", 0);
122 return substring(s, 0, o);
125 /** returns all but first word */
128 int o = strstrofs(s, " ", 0);
129 if (o < 0) return string_null;
130 return substring(s, o + 1, strlen(s) - (o + 1));
133 string cons(string a, string b)
135 if (a == "") return b;
136 if (b == "") return a;
137 return strcat(a, " ", b);
140 string substring_range(string s, float b, float e)
142 return substring(s, b, e - b);
145 string swapwords(string str, float i, float j)
148 string s1, s2, s3, s4, s5;
149 float si, ei, sj, ej, s0, en;
150 n = tokenizebyseparator(str, " "); // must match g_maplist processing in ShuffleMaplist and "shuffle"
151 si = argv_start_index(i);
152 sj = argv_start_index(j);
153 ei = argv_end_index(i);
154 ej = argv_end_index(j);
155 s0 = argv_start_index(0);
156 en = argv_end_index(n - 1);
157 s1 = substring_range(str, s0, si);
158 s2 = substring_range(str, si, ei);
159 s3 = substring_range(str, ei, sj);
160 s4 = substring_range(str, sj, ej);
161 s5 = substring_range(str, ej, en);
162 return strcat(s1, s4, s3, s2, s5);
165 string _shufflewords_str;
166 void _shufflewords_swapfunc(float i, float j, entity pass)
168 _shufflewords_str = swapwords(_shufflewords_str, i, j);
170 string shufflewords(string str)
172 _shufflewords_str = str;
173 int n = tokenizebyseparator(str, " ");
174 shuffle(n, _shufflewords_swapfunc, NULL);
175 str = _shufflewords_str;
176 _shufflewords_str = string_null;
180 string unescape(string in)
182 in = strzone(in); // but it doesn't seem to be necessary in my tests at least
184 int len = strlen(in);
186 for (int i = 0; i < len; ++i)
188 string s = substring(in, i, 1);
191 s = substring(in, i + 1, 1);
192 if (s == "n") str = strcat(str, "\n");
193 else if (s == "\\") str = strcat(str, "\\");
194 else str = strcat(str, substring(in, i, 2));
198 str = strcat(str, s);
204 string strwords(string s, int w)
207 for ( ; w && endpos >= 0; --w)
208 endpos = strstrofs(s, " ", endpos + 1);
209 if (endpos < 0) return s;
210 return substring(s, 0, endpos);
213 #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0)
215 int u8_strsize(string s)
218 for (int i = 0, c; (c = str2chr(s, i)) > 0; ++i, ++l)
227 bool isInvisibleString(string s)
229 s = strdecolorize(s);
230 bool utf8 = cvar("utf8_enable");
231 for (int i = 0, n = strlen(s); i < n; ++i)
233 int c = str2chr(s, i);
239 case 192: // charmap space
242 case 160: // space in unicode fonts
243 case 0xE000 + 192: // utf8 charmap space
252 // Multiline text file buffers
254 int buf_load(string pFilename)
256 int buf = buf_create();
257 if (buf < 0) return -1;
258 int fh = fopen(pFilename, FILE_READ);
265 for (int i = 0; (l = fgets(fh)); ++i)
266 bufstr_set(buf, i, l);
271 void buf_save(float buf, string pFilename)
273 int fh = fopen(pFilename, FILE_WRITE);
274 if (fh < 0) error(strcat("Can't write buf to ", pFilename));
275 int n = buf_getsize(buf);
276 for (int i = 0; i < n; ++i)
277 fputs(fh, strcat(bufstr_get(buf, i), "\n"));
282 * converts a number to a string with the indicated number of decimals
283 * works for up to 10 decimals!
285 string ftos_decimals(float number, int decimals)
287 // inhibit stupid negative zero
288 if (number == 0) number = 0;
289 // we have sprintf...
290 return sprintf("%.*f", decimals, number);
293 int vercmp_recursive(string v1, string v2)
295 int dot1 = strstrofs(v1, ".", 0);
296 int dot2 = strstrofs(v2, ".", 0);
297 string s1 = (dot1 == -1) ? v1 : substring(v1, 0, dot1);
298 string s2 = (dot2 == -1) ? v2 : substring(v2, 0, dot2);
301 r = stof(s1) - stof(s2);
302 if (r != 0) return r;
304 r = strcasecmp(s1, s2);
305 if (r != 0) return r;
307 if (dot1 == -1) return (dot2 == -1) ? 0 : -1;
308 else return (dot2 == -1) ? 1 : vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
311 int vercmp(string v1, string v2)
313 if (strcasecmp(v1, v2) == 0) return 0; // early out check
316 if (v1 == "git") return 1;
317 if (v2 == "git") return -1;
319 return vercmp_recursive(v1, v2);
322 const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
323 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
324 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
325 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))