]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/string.qh
Merge branch 'TimePath/items' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / string.qh
1 #ifndef STRING_H
2 #define STRING_H
3
4 #ifndef SVQC
5         float stringwidth_colors(string s, vector theSize)
6         {
7                 return stringwidth(s, true, theSize);
8         }
9
10         float stringwidth_nocolors(string s, vector theSize)
11         {
12                 return stringwidth(s, false, theSize);
13         }
14 #endif
15
16 // Timer (#5)
17 //
18 // TODO: macro
19 string seconds_tostring(float sec)
20 {
21         float minutes = floor(sec / 60);
22         sec -= minutes * 60;
23         return sprintf("%d:%02d", minutes, sec);
24 }
25
26 int ColorTranslateMode;
27
28 string ColorTranslateRGB(string s)
29 {
30         return (ColorTranslateMode & 1) ? strdecolorize(s) : s;
31 }
32
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
39 // "kill" colors
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)
43 // background color
44 string autocvar_hud_colorset_background = "7";   // BG - White // neutral/unimportant text
45
46 /** color code replace, place inside of sprintf and parse the string */
47 string CCR(string input)
48 {
49         // See the autocvar declarations in util.qh for default values
50
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);
56
57         // "kill" colors
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);
61
62         // background colors
63         input = strreplace("^BG", strcat("^", autocvar_hud_colorset_background), input);
64         input = strreplace("^N", "^7", input);  // "none"-- reset to white...
65         return input;
66 }
67
68 bool startsWith(string haystack, string needle)
69 {
70         return substring(haystack, 0, strlen(needle)) == needle;
71 }
72
73 bool startsWithNocase(string haystack, string needle)
74 {
75         return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
76 }
77
78 /** unzone the string, and return it as tempstring. Safe to be called on string_null */
79 string fstrunzone(string s)
80 {
81         if (!s) return s;
82         string sc = strcat(s, "");
83         strunzone(s);
84         return sc;
85 }
86
87 string car(string s)
88 {
89         int o = strstrofs(s, " ", 0);
90         if (o < 0) return s;
91         return substring(s, 0, o);
92 }
93
94 string cdr(string s)
95 {
96         int o = strstrofs(s, " ", 0);
97         if (o < 0) return string_null;
98         return substring(s, o + 1, strlen(s) - (o + 1));
99 }
100
101 string substring_range(string s, float b, float e)
102 {
103         return substring(s, b, e - b);
104 }
105
106 string swapwords(string str, float i, float j)
107 {
108         float n;
109         string s1, s2, s3, s4, s5;
110         float si, ei, sj, ej, s0, en;
111         n = tokenizebyseparator(str, " ");  // must match g_maplist processing in ShuffleMaplist and "shuffle"
112         si = argv_start_index(i);
113         sj = argv_start_index(j);
114         ei = argv_end_index(i);
115         ej = argv_end_index(j);
116         s0 = argv_start_index(0);
117         en = argv_end_index(n - 1);
118         s1 = substring_range(str, s0, si);
119         s2 = substring_range(str, si, ei);
120         s3 = substring_range(str, ei, sj);
121         s4 = substring_range(str, sj, ej);
122         s5 = substring_range(str, ej, en);
123         return strcat(s1, s4, s3, s2, s5);
124 }
125
126 string _shufflewords_str;
127 void _shufflewords_swapfunc(float i, float j, entity pass)
128 {
129         _shufflewords_str = swapwords(_shufflewords_str, i, j);
130 }
131 string shufflewords(string str)
132 {
133         _shufflewords_str = str;
134         int n = tokenizebyseparator(str, " ");
135         shuffle(n, _shufflewords_swapfunc, NULL);
136         str = _shufflewords_str;
137         _shufflewords_str = string_null;
138         return str;
139 }
140
141 string unescape(string in)
142 {
143         in = strzone(in);  // but it doesn't seem to be necessary in my tests at least
144
145         int len = strlen(in);
146         string str = "";
147         for (int i = 0; i < len; ++i)
148         {
149                 string s = substring(in, i, 1);
150                 if (s == "\\")
151                 {
152                         s = substring(in, i + 1, 1);
153                         if (s == "n") str = strcat(str, "\n");
154                         else if (s == "\\") str = strcat(str, "\\");
155                         else str = strcat(str, substring(in, i, 2));
156                         ++i;
157                         continue;
158                 }
159                 str = strcat(str, s);
160         }
161         strunzone(in);
162         return str;
163 }
164
165 string strwords(string s, int w)
166 {
167         int endpos = 0;
168         for ( ; w && endpos >= 0; --w)
169                 endpos = strstrofs(s, " ", endpos + 1);
170         if (endpos < 0) return s;
171         return substring(s, 0, endpos);
172 }
173
174 bool strhasword(string s, string w)
175 {
176         return strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0;
177 }
178
179 int u8_strsize(string s)
180 {
181         int l = 0;
182         for (int i = 0, c; (c = str2chr(s, i)) > 0; ++i, ++l)
183         {
184                 l += (c >= 0x80);
185                 l += (c >= 0x800);
186                 l += (c >= 0x10000);
187         }
188         return l;
189 }
190
191 #endif