5 float stringwidth_colors(string s, vector theSize)
7 return stringwidth(s, true, theSize);
10 float stringwidth_nocolors(string s, vector theSize)
12 return stringwidth(s, false, theSize);
19 string seconds_tostring(float sec)
21 float minutes = floor(sec / 60);
23 return sprintf("%d:%02d", minutes, sec);
26 int ColorTranslateMode;
28 string ColorTranslateRGB(string s)
30 return (ColorTranslateMode & 1) ? strdecolorize(s) : s;
33 // color code replace, place inside of sprintf and parse the string... defaults described as constants
34 // foreground/normal colors
35 string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green // primary priority (important names, etc)
36 string autocvar_hud_colorset_foreground_2 = "3"; // F2 - Yellow // secondary priority (items, locations, numbers, etc)
37 string autocvar_hud_colorset_foreground_3 = "4"; // F3 - Blue // tertiary priority or relatively inconsequential text
38 string autocvar_hud_colorset_foreground_4 = "1"; // F4 - Red // notice/attention grabbing texting
40 string autocvar_hud_colorset_kill_1 = "1"; // K1 - Red // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
41 string autocvar_hud_colorset_kill_2 = "3"; // K2 - Yellow // similar to above, but less important... OR, a highlight out of above message type
42 string autocvar_hud_colorset_kill_3 = "4"; // K3 - Blue // "good" or "beneficial" text (you fragging someone, etc)
44 string autocvar_hud_colorset_background = "7"; // BG - White // neutral/unimportant text
46 /** color code replace, place inside of sprintf and parse the string */
47 string CCR(string input)
49 // See the autocvar declarations in util.qh for default values
51 // foreground/normal colors
52 input = strreplace("^F1", strcat("^", autocvar_hud_colorset_foreground_1), input);
53 input = strreplace("^F2", strcat("^", autocvar_hud_colorset_foreground_2), input);
54 input = strreplace("^F3", strcat("^", autocvar_hud_colorset_foreground_3), input);
55 input = strreplace("^F4", strcat("^", autocvar_hud_colorset_foreground_4), input);
58 input = strreplace("^K1", strcat("^", autocvar_hud_colorset_kill_1), input);
59 input = strreplace("^K2", strcat("^", autocvar_hud_colorset_kill_2), input);
60 input = strreplace("^K3", strcat("^", autocvar_hud_colorset_kill_3), input);
63 input = strreplace("^BG", strcat("^", autocvar_hud_colorset_background), input);
64 input = strreplace("^N", "^7", input); // "none"-- reset to white...
68 #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0)
70 bool startsWithNocase(string haystack, string needle)
72 return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
75 /** unzone the string, and return it as tempstring. Safe to be called on string_null */
76 string fstrunzone(string s)
79 string sc = strcat(s, "");
84 /** returns first word */
87 int o = strstrofs(s, " ", 0);
89 return substring(s, 0, o);
92 /** returns all but first word */
95 int o = strstrofs(s, " ", 0);
96 if (o < 0) return string_null;
97 return substring(s, o + 1, strlen(s) - (o + 1));
100 string substring_range(string s, float b, float e)
102 return substring(s, b, e - b);
105 string swapwords(string str, float i, float j)
108 string s1, s2, s3, s4, s5;
109 float si, ei, sj, ej, s0, en;
110 n = tokenizebyseparator(str, " "); // must match g_maplist processing in ShuffleMaplist and "shuffle"
111 si = argv_start_index(i);
112 sj = argv_start_index(j);
113 ei = argv_end_index(i);
114 ej = argv_end_index(j);
115 s0 = argv_start_index(0);
116 en = argv_end_index(n - 1);
117 s1 = substring_range(str, s0, si);
118 s2 = substring_range(str, si, ei);
119 s3 = substring_range(str, ei, sj);
120 s4 = substring_range(str, sj, ej);
121 s5 = substring_range(str, ej, en);
122 return strcat(s1, s4, s3, s2, s5);
125 string _shufflewords_str;
126 void _shufflewords_swapfunc(float i, float j, entity pass)
128 _shufflewords_str = swapwords(_shufflewords_str, i, j);
130 string shufflewords(string str)
132 _shufflewords_str = str;
133 int n = tokenizebyseparator(str, " ");
134 shuffle(n, _shufflewords_swapfunc, NULL);
135 str = _shufflewords_str;
136 _shufflewords_str = string_null;
140 string unescape(string in)
142 in = strzone(in); // but it doesn't seem to be necessary in my tests at least
144 int len = strlen(in);
146 for (int i = 0; i < len; ++i)
148 string s = substring(in, i, 1);
151 s = substring(in, i + 1, 1);
152 if (s == "n") str = strcat(str, "\n");
153 else if (s == "\\") str = strcat(str, "\\");
154 else str = strcat(str, substring(in, i, 2));
158 str = strcat(str, s);
164 string strwords(string s, int w)
167 for ( ; w && endpos >= 0; --w)
168 endpos = strstrofs(s, " ", endpos + 1);
169 if (endpos < 0) return s;
170 return substring(s, 0, endpos);
173 #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0)
175 int u8_strsize(string s)
178 for (int i = 0, c; (c = str2chr(s, i)) > 0; ++i, ++l)