Sat, 16 Mar 2013 00:05:39 +0200
Color gibberish red. Check for line code length for gibberish (must be 1 to be valid)
0 | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | #include <stdarg.h> | |
5 | #include <assert.h> | |
6 | // #include <initializer_list> | |
7 | #include "str.h" | |
8 | ||
9 | #define ITERATE_STRING(u) \ | |
10 | for (unsigned int u = 0; u < strlen (text); u++) | |
11 | ||
12 | // ============================================================================ | |
13 | // vdynformat: Try to write to a formatted string with size bytes first, if | |
14 | // that fails, double the size and keep recursing until it works. | |
15 | char* vdynformat (const char* csFormat, va_list vArgs, long lSize) { | |
16 | char* buffer = new char[lSize]; | |
17 | int r = vsnprintf (buffer, lSize - 1, csFormat, vArgs); | |
18 | if (r > (signed)(lSize - 1) || r < 0) { | |
19 | delete[] buffer; | |
20 | buffer = vdynformat (csFormat, vArgs, lSize * 2); | |
21 | } | |
22 | return buffer; | |
23 | } | |
24 | ||
25 | // ============================================================================ | |
26 | str::str () { | |
27 | text = new char[1]; | |
28 | clear(); | |
29 | alloclen = strlen (text); | |
30 | } | |
31 | ||
32 | str::str (const char* c) { | |
33 | text = new char[1]; | |
34 | clear (); | |
35 | alloclen = strlen (text); | |
36 | append (c); | |
37 | } | |
38 | ||
39 | str::str (char c) { | |
40 | text = new char[1]; | |
41 | clear (); | |
42 | alloclen = strlen (text); | |
43 | append (c); | |
44 | } | |
45 | ||
46 | str::~str () { | |
47 | // delete[] text; | |
48 | } | |
49 | ||
50 | // ============================================================================ | |
51 | void str::clear () { | |
52 | delete[] text; | |
53 | text = new char[1]; | |
54 | text[0] = '\0'; | |
55 | curs = 0; | |
56 | alloclen = 0; | |
57 | } | |
58 | ||
59 | // ============================================================================ | |
60 | void str::resize (unsigned int len) { | |
61 | unsigned int oldlen = strlen (text); | |
62 | char* oldtext = new char[oldlen]; | |
63 | strncpy (oldtext, text, oldlen); | |
64 | ||
65 | delete[] text; | |
66 | text = new char[len+1]; | |
67 | for (unsigned int u = 0; u < len+1; u++) | |
68 | text[u] = 0; | |
69 | strncpy (text, oldtext, len); | |
70 | delete[] oldtext; | |
71 | ||
72 | alloclen = len; | |
73 | } | |
74 | ||
75 | // ============================================================================ | |
76 | void str::dump () { | |
77 | for (unsigned int u = 0; u <= alloclen; u++) | |
78 | printf ("\t%u. %u (%c)\n", u, text[u], text[u]); | |
79 | } | |
80 | ||
81 | // ============================================================================ | |
82 | // Adds a new character at the end of the string. | |
83 | void str::append (char c) { | |
84 | // Out of space, thus resize | |
85 | if (curs == alloclen) | |
86 | resize (alloclen + 1); | |
87 | text[curs] = c; | |
88 | curs++; | |
89 | } | |
90 | ||
91 | void str::append (const char* c) { | |
92 | resize (alloclen + strlen (c)); | |
93 | ||
94 | for (unsigned int u = 0; u < strlen (c); u++) { | |
95 | if (c[u] != 0) | |
96 | append (c[u]); | |
97 | } | |
98 | } | |
99 | ||
100 | void str::append (str c) { | |
101 | append (c.chars()); | |
102 | } | |
103 | ||
104 | // ============================================================================ | |
105 | void str::appendformat (const char* c, ...) { | |
106 | va_list v; | |
107 | ||
108 | va_start (v, c); | |
109 | char* buf = vdynformat (c, v, 256); | |
110 | va_end (v); | |
111 | ||
112 | append (buf); | |
113 | delete[] buf; | |
114 | } | |
115 | ||
116 | void str::format (const char* fmt, ...) { | |
117 | clear (); | |
118 | ||
119 | va_list v; | |
120 | ||
121 | va_start (v, fmt); | |
122 | char* buf = vdynformat (fmt, v, 256); | |
123 | va_end (v); | |
124 | ||
125 | append (buf); | |
126 | delete[] buf; | |
127 | } | |
128 | ||
129 | str str::format (...) { | |
130 | va_list va; | |
131 | char* buf; | |
132 | ||
133 | va_start (va, this); | |
134 | buf = vdynformat (text, va, 256); | |
135 | va_end (va); | |
136 | ||
137 | str val = buf; | |
138 | delete[] buf; | |
139 | return val; | |
140 | } | |
141 | ||
142 | // ============================================================================ | |
143 | char* str::chars () { | |
144 | return text; | |
145 | } | |
146 | ||
147 | // ============================================================================ | |
148 | int str::first (const char* c, unsigned int a) { | |
149 | unsigned int r = 0; | |
150 | unsigned int index = 0; | |
151 | for (; a < alloclen; a++) { | |
152 | if (text[a] == c[r]) { | |
153 | if (r == 0) | |
154 | index = a; | |
155 | ||
156 | r++; | |
157 | if (r == strlen (c)) | |
158 | return index; | |
159 | } else { | |
160 | if (r != 0) { | |
161 | // If the string sequence broke at this point, we need to | |
162 | // check this character again, for a new sequence just | |
163 | // might start right here. | |
164 | a--; | |
165 | } | |
166 | ||
167 | r = 0; | |
168 | } | |
169 | } | |
170 | ||
171 | return -1; | |
172 | } | |
173 | ||
174 | // ============================================================================ | |
175 | int str::last (const char* c, int a) { | |
176 | if (a == -1) | |
177 | a = len(); | |
178 | ||
179 | int max = strlen (c)-1; | |
180 | ||
181 | int r = max; | |
182 | for (; a >= 0; a--) { | |
183 | if (text[a] == c[r]) { | |
184 | r--; | |
185 | if (r == -1) | |
186 | return a; | |
187 | } else { | |
188 | if (r != max) | |
189 | a++; | |
190 | ||
191 | r = max; | |
192 | } | |
193 | } | |
194 | ||
195 | return -1; | |
196 | } | |
197 | ||
198 | // ============================================================================ | |
199 | str str::substr (unsigned int a, unsigned int b) { | |
200 | if (a > len()) a = len(); | |
201 | if (b > len()) b = len(); | |
202 | ||
203 | if (b == a) | |
204 | return ""; | |
205 | ||
206 | if (b < a) { | |
207 | printf ("str::substring:: indices %u and %u given, should be the other way around, swapping..\n", a, b); | |
208 | ||
209 | // Swap the variables | |
210 | unsigned int c = a; | |
211 | a = b; | |
212 | b = c; | |
213 | } | |
214 | ||
215 | char* s = new char[b - a + 1]; | |
216 | strncpy (s, text + a, b - a); | |
217 | s[b - a] = '\0'; | |
218 | ||
219 | str other = s; | |
220 | delete[] s; | |
221 | return other; | |
222 | } | |
223 | ||
224 | // ============================================================================ | |
225 | void str::remove (unsigned int idx, unsigned int dellen) { | |
226 | str s1 = substr (0, idx); | |
227 | str s2 = substr (idx + dellen, -1); | |
228 | ||
229 | clear(); | |
230 | ||
231 | append (s1); | |
232 | append (s2); | |
233 | } | |
234 | ||
235 | // ============================================================================ | |
236 | str str::trim (int dellen) { | |
237 | if (dellen > 0) | |
238 | return substr (0, len() - dellen); | |
239 | return substr (-dellen, len()); | |
240 | } | |
241 | ||
242 | // ============================================================================ | |
243 | void str::replace (const char* o, const char* n, unsigned int a) { | |
244 | for (int idx; (idx = first (o, a)) != -1;) { | |
245 | str s1 = substr (0, idx); | |
246 | str s2 = substr (idx + strlen (o), len()); | |
247 | ||
248 | clear(); | |
249 | ||
250 | append (s1); | |
251 | append (n); | |
252 | append (s2); | |
253 | } | |
254 | } | |
255 | ||
256 | // ============================================================================ | |
257 | /* | |
258 | void str::strip (char c) { | |
259 | strip ({c}); | |
260 | } | |
261 | ||
262 | void str::strip (std::initializer_list<char> unwanted) { | |
263 | str cache = text; | |
264 | uint oldlen = len(); | |
265 | ||
266 | char* buf = new char[oldlen]; | |
267 | char* bufptr = buf; | |
268 | for (uint i = 0; i < oldlen; i++) { | |
269 | bool valid = true; | |
270 | for (const char* j = unwanted.begin(); j < unwanted.end() && valid; j++) | |
271 | if (text[i] == *j) | |
272 | valid = false; | |
273 | ||
274 | if (valid) | |
275 | *bufptr++ = text[i]; | |
276 | } | |
277 | ||
278 | *bufptr = '\0'; | |
279 | assert (bufptr <= buf + oldlen); | |
280 | ||
281 | clear(); | |
282 | append (buf); | |
283 | ||
284 | delete[] buf; | |
285 | } | |
286 | */ | |
287 | ||
288 | void str::insert (char* c, unsigned int pos) { | |
289 | str s1 = substr (0, pos); | |
290 | str s2 = substr (pos, len()); | |
291 | ||
292 | clear(); | |
293 | append (s1); | |
294 | append (c); | |
295 | append (s2); | |
296 | } | |
297 | ||
298 | str str::reverse () { | |
299 | char* buf = new char[len() + 1]; | |
300 | ||
301 | for (uint i = 0; i < len(); i++) | |
302 | buf[i] = text[len() - i - 1]; | |
303 | buf[len()] = '\0'; | |
304 | ||
305 | str other = buf; | |
306 | delete[] buf; | |
307 | return other; | |
308 | } | |
309 | ||
310 | str str::repeat (int n) { | |
311 | assert (n >= 0); | |
312 | ||
313 | str other; | |
314 | for (int i = 0; i < n; i++) | |
315 | other += text; | |
316 | return other; | |
317 | } | |
318 | ||
319 | // ============================================================================ | |
320 | bool str::isnumber () { | |
321 | ITERATE_STRING (u) { | |
322 | // Minus sign as the first character is allowed for negatives | |
323 | if (!u && text[u] == '-') | |
324 | continue; | |
325 | ||
326 | if (text[u] < '0' || text[u] > '9') | |
327 | return false; | |
328 | } | |
329 | return true; | |
330 | } | |
331 | ||
332 | // ============================================================================ | |
333 | bool str::isword () { | |
334 | ITERATE_STRING (u) { | |
335 | // lowercase letters | |
336 | if (text[u] >= 'a' || text[u] <= 'z') | |
337 | continue; | |
338 | ||
339 | // uppercase letters | |
340 | if (text[u] >= 'A' || text[u] <= 'Z') | |
341 | continue; | |
342 | ||
343 | return false; | |
344 | } | |
345 | return true; | |
346 | } | |
347 | ||
348 | int str::instanceof (const char* c, uint n) { | |
349 | unsigned int r = 0; | |
350 | unsigned int index = 0; | |
351 | unsigned int x = 0; | |
352 | for (uint a = 0; a < alloclen; a++) { | |
353 | if (text[a] == c[r]) { | |
354 | if (r == 0) | |
355 | index = a; | |
356 | ||
357 | r++; | |
358 | if (r == strlen (c)) { | |
359 | if (x++ == n) | |
360 | return index; | |
361 | r = 0; | |
362 | } | |
363 | } else { | |
364 | if (r != 0) | |
365 | a--; | |
366 | r = 0; | |
367 | } | |
368 | } | |
369 | ||
370 | return -1; | |
371 | } | |
372 | ||
373 | // ============================================================================ | |
374 | int str::compare (const char* c) { | |
375 | return strcmp (text, c); | |
376 | } | |
377 | ||
378 | int str::compare (str c) { | |
379 | return compare (c.chars()); | |
380 | } | |
381 | ||
382 | int str::icompare (const char* c) { | |
383 | return icompare (str ((char*)c)); | |
384 | } | |
385 | ||
386 | int str::icompare (str b) { | |
387 | return strcmp (tolower().chars(), b.tolower().chars()); | |
388 | } | |
389 | ||
390 | // ============================================================================ | |
391 | str str::tolower () { | |
392 | str n = text; | |
393 | ||
394 | for (uint u = 0; u < len(); u++) { | |
395 | if (n[u] >= 'A' && n[u] < 'Z') | |
396 | n.text[u] += ('a' - 'A'); | |
397 | } | |
398 | ||
399 | return n; | |
400 | } | |
401 | ||
402 | // ============================================================================ | |
403 | str str::toupper () { | |
404 | str n = text; | |
405 | ||
406 | for (uint u = 0; u < len(); u++) { | |
407 | if (n[u] >= 'a' && n[u] < 'z') | |
408 | n.text[u] -= ('a' - 'A'); | |
409 | } | |
410 | ||
411 | return n; | |
412 | } | |
413 | ||
414 | // ============================================================================ | |
415 | unsigned str::count (char c) { | |
416 | unsigned n = 0; | |
417 | ITERATE_STRING (u) | |
418 | if (text[u] == c) | |
419 | n++; | |
420 | return n; | |
421 | } | |
422 | ||
423 | unsigned str::count (char* c) { | |
424 | unsigned int r = 0; | |
425 | unsigned int tmp = 0; | |
426 | ITERATE_STRING (u) { | |
427 | if (text[u] == c[r]) { | |
428 | r++; | |
429 | if (r == strlen (c)) { | |
430 | r = 0; | |
431 | tmp++; | |
432 | } | |
433 | } else { | |
434 | if (r != 0) | |
435 | u--; | |
436 | r = 0; | |
437 | } | |
438 | } | |
439 | ||
440 | return tmp; | |
441 | } | |
442 | ||
443 | // ============================================================================ | |
444 | std::vector<str> str::split (str del) { | |
445 | std::vector<str> res; | |
446 | unsigned int a = 0; | |
447 | ||
448 | // Find all separators and store the text left to them. | |
449 | while (1) { | |
450 | int b = first (del, a); | |
451 | ||
452 | if (b == -1) | |
453 | break; | |
454 | ||
455 | res.push_back (substr (a, b)); | |
456 | a = b + strlen (del); | |
457 | } | |
458 | ||
459 | // Add the string at the right of the last separator | |
460 | res.push_back (substr (a, len())); | |
461 | return res; | |
462 | } | |
463 | ||
464 | std::vector<str> str::operator/ (str splitstring) {return split(splitstring);} | |
465 | std::vector<str> str::operator/ (char* splitstring) {return split(splitstring);} | |
466 | std::vector<str> str::operator/ (const char* splitstring) {return split(splitstring);} |