]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/string.qh
Merge branch 'master' into TimePath/debug_draw
[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 #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0)
69
70 bool startsWithNocase(string haystack, string needle)
71 {
72         return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
73 }
74
75 /** unzone the string, and return it as tempstring. Safe to be called on string_null */
76 string fstrunzone(string s)
77 {
78         if (!s) return s;
79         string sc = strcat(s, "");
80         strunzone(s);
81         return sc;
82 }
83
84 /** returns first word */
85 string car(string s)
86 {
87         int o = strstrofs(s, " ", 0);
88         if (o < 0) return s;
89         return substring(s, 0, o);
90 }
91
92 /** returns all but first word */
93 string cdr(string s)
94 {
95         int o = strstrofs(s, " ", 0);
96         if (o < 0) return string_null;
97         return substring(s, o + 1, strlen(s) - (o + 1));
98 }
99
100 string substring_range(string s, float b, float e)
101 {
102         return substring(s, b, e - b);
103 }
104
105 string swapwords(string str, float i, float j)
106 {
107         float n;
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);
123 }
124
125 string _shufflewords_str;
126 void _shufflewords_swapfunc(float i, float j, entity pass)
127 {
128         _shufflewords_str = swapwords(_shufflewords_str, i, j);
129 }
130 string shufflewords(string str)
131 {
132         _shufflewords_str = str;
133         int n = tokenizebyseparator(str, " ");
134         shuffle(n, _shufflewords_swapfunc, NULL);
135         str = _shufflewords_str;
136         _shufflewords_str = string_null;
137         return str;
138 }
139
140 string unescape(string in)
141 {
142         in = strzone(in);  // but it doesn't seem to be necessary in my tests at least
143
144         int len = strlen(in);
145         string str = "";
146         for (int i = 0; i < len; ++i)
147         {
148                 string s = substring(in, i, 1);
149                 if (s == "\\")
150                 {
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));
155                         ++i;
156                         continue;
157                 }
158                 str = strcat(str, s);
159         }
160         strunzone(in);
161         return str;
162 }
163
164 string strwords(string s, int w)
165 {
166         int endpos = 0;
167         for ( ; w && endpos >= 0; --w)
168                 endpos = strstrofs(s, " ", endpos + 1);
169         if (endpos < 0) return s;
170         return substring(s, 0, endpos);
171 }
172
173 #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0)
174
175 int u8_strsize(string s)
176 {
177         int l = 0;
178         for (int i = 0, c; (c = str2chr(s, i)) > 0; ++i, ++l)
179         {
180                 l += (c >= 0x80);
181                 l += (c >= 0x800);
182                 l += (c >= 0x10000);
183         }
184         return l;
185 }
186
187 #endif