34 #include "format.h" |
34 #include "format.h" |
35 #include "md5.h" |
35 #include "md5.h" |
36 |
36 |
37 // ------------------------------------------------------------------------------------------------- |
37 // ------------------------------------------------------------------------------------------------- |
38 // |
38 // |
39 METHOD |
39 int String::compare (const String& other) const |
40 String::compare (const String& other) const -> int |
|
41 { |
40 { |
42 return m_string.compare (other.std_string()); |
41 return m_string.compare (other.std_string()); |
43 } |
42 } |
44 |
43 |
45 // ------------------------------------------------------------------------------------------------- |
44 // ------------------------------------------------------------------------------------------------- |
46 // |
45 // |
47 METHOD |
46 void String::trim (int n) |
48 String::trim (int n) -> void |
|
49 { |
47 { |
50 if (n > 0) |
48 if (n > 0) |
51 m_string = mid (0, length() - n).std_string(); |
49 m_string = mid (0, length() - n).std_string(); |
52 else |
50 else |
53 m_string = mid (n, -1).std_string(); |
51 m_string = mid (n, -1).std_string(); |
54 } |
52 } |
55 |
53 |
56 // ------------------------------------------------------------------------------------------------- |
54 // ------------------------------------------------------------------------------------------------- |
57 // |
55 // |
58 METHOD |
56 String String::strip (const List<char>& unwanted) |
59 String::strip (const List<char>& unwanted) -> String |
|
60 { |
57 { |
61 String copy (m_string); |
58 String copy (m_string); |
62 |
59 |
63 for (char c : unwanted) |
60 for (char c : unwanted) |
64 { |
61 { |
138 return res; |
131 return res; |
139 } |
132 } |
140 |
133 |
141 // ------------------------------------------------------------------------------------------------- |
134 // ------------------------------------------------------------------------------------------------- |
142 // |
135 // |
143 METHOD |
136 void String::replace (const char* a, const char* b) |
144 String::replace (const char* a, const char* b) -> void |
|
145 { |
137 { |
146 long pos; |
138 long pos; |
147 |
139 |
148 while ((pos = find (a)) != -1) |
140 while ((pos = find (a)) != -1) |
149 m_string = m_string.replace (pos, strlen (a), b); |
141 m_string = m_string.replace (pos, strlen (a), b); |
150 } |
142 } |
151 |
143 |
152 // ------------------------------------------------------------------------------------------------- |
144 // ------------------------------------------------------------------------------------------------- |
153 // |
145 // |
154 METHOD |
146 int String::count (char needle) const |
155 String::count (char needle) const -> int |
|
156 { |
147 { |
157 int needles = 0; |
148 int needles = 0; |
158 |
149 |
159 for (const char & c : m_string) |
150 for (const char & c : m_string) |
160 if (c == needle) |
151 if (c == needle) |
279 return i; |
263 return i; |
280 } |
264 } |
281 |
265 |
282 // ------------------------------------------------------------------------------------------------- |
266 // ------------------------------------------------------------------------------------------------- |
283 // |
267 // |
284 METHOD |
268 String String::operator+ (const String& data) const |
285 String::operator+ (const String& data) const -> String |
|
286 { |
269 { |
287 String newString = *this; |
270 String newString = *this; |
288 newString.append (data); |
271 newString.append (data); |
289 return newString; |
272 return newString; |
290 } |
273 } |
291 |
274 |
292 // ------------------------------------------------------------------------------------------------- |
275 // ------------------------------------------------------------------------------------------------- |
293 // |
276 // |
294 METHOD |
277 String String::operator+ (const char* data) const |
295 String::operator+ (const char* data) const -> String |
|
296 { |
278 { |
297 String newstr = *this; |
279 String newstr = *this; |
298 newstr.append (data); |
280 newstr.append (data); |
299 return newstr; |
281 return newstr; |
300 } |
282 } |
301 |
283 |
302 // ------------------------------------------------------------------------------------------------- |
284 // ------------------------------------------------------------------------------------------------- |
303 // |
285 // |
304 METHOD |
286 bool String::is_numeric() const |
305 String::is_numeric() const -> bool |
287 { |
306 { |
288 char* endptr; |
307 bool gotDot = false; |
289 strtol (chars(), &endptr, 10); |
308 |
290 return not (endptr && *endptr); |
309 for (const char & c : m_string) |
291 } |
310 { |
292 |
311 // Allow leading hyphen for negatives |
293 // ------------------------------------------------------------------------------------------------- |
312 if (&c == &m_string[0] and c == '-') |
294 // |
313 continue; |
295 bool String::ends_with (const String& other) |
314 |
|
315 // Check for decimal point |
|
316 if (!gotDot and c == '.') |
|
317 { |
|
318 gotDot = true; |
|
319 continue; |
|
320 } |
|
321 |
|
322 if (c >= '0' and c <= '9') |
|
323 continue; // Digit |
|
324 |
|
325 // If the above cases didn't catch this character, it was |
|
326 // illegal and this is therefore not a number. |
|
327 return false; |
|
328 } |
|
329 |
|
330 return true; |
|
331 } |
|
332 |
|
333 // ------------------------------------------------------------------------------------------------- |
|
334 // |
|
335 METHOD |
|
336 String::ends_with (const String& other) -> bool |
|
337 { |
296 { |
338 if (length() < other.length()) |
297 if (length() < other.length()) |
339 return false; |
298 return false; |
340 |
299 |
341 const int ofs = length() - other.length(); |
300 const int ofs = length() - other.length(); |
342 return strncmp (chars() + ofs, other.chars(), other.length()) == 0; |
301 return strncmp (chars() + ofs, other.chars(), other.length()) == 0; |
343 } |
302 } |
344 |
303 |
345 // ------------------------------------------------------------------------------------------------- |
304 // ------------------------------------------------------------------------------------------------- |
346 // |
305 // |
347 METHOD |
306 bool String::starts_with (const String& other) |
348 String::starts_with (const String& other) -> bool |
|
349 { |
307 { |
350 if (length() < other.length()) |
308 if (length() < other.length()) |
351 return false; |
309 return false; |
352 |
310 |
353 return strncmp (chars(), other.chars(), other.length()) == 0; |
311 return strncmp (chars(), other.chars(), other.length()) == 0; |
354 } |
312 } |
355 |
313 |
356 // ------------------------------------------------------------------------------------------------- |
314 // ------------------------------------------------------------------------------------------------- |
357 // |
315 // |
358 METHOD |
316 void String::sprintf (const char* fmtstr, ...) |
359 String::sprintf (const char* fmtstr, ...) -> void |
317 { |
360 { |
318 char* buf = nullptr; |
361 char* buf; |
|
362 int bufsize = 256; |
319 int bufsize = 256; |
363 va_list va; |
320 va_list va; |
364 va_start (va, fmtstr); |
321 va_start (va, fmtstr); |
365 |
322 |
366 do |
323 do |
|
324 { |
|
325 delete[] buf; |
367 buf = new char[bufsize]; |
326 buf = new char[bufsize]; |
|
327 } |
368 while (vsnprintf (buf, bufsize, fmtstr, va) >= bufsize); |
328 while (vsnprintf (buf, bufsize, fmtstr, va) >= bufsize); |
369 |
329 |
370 va_end (va); |
330 va_end (va); |
371 m_string = buf; |
331 m_string = buf; |
372 delete[] buf; |
332 delete[] buf; |
373 } |
333 } |
374 |
334 |
375 // ------------------------------------------------------------------------------------------------- |
335 // ------------------------------------------------------------------------------------------------- |
376 // |
336 // |
377 METHOD |
337 String StringList::join (const String& delim) |
378 StringList::join (const String& delim) -> String |
|
379 { |
338 { |
380 String result; |
339 String result; |
381 |
340 |
382 for (const String& it : container()) |
341 for (const String& it : container()) |
383 { |
342 { |
439 return true; |
397 return true; |
440 } |
398 } |
441 |
399 |
442 // ------------------------------------------------------------------------------------------------- |
400 // ------------------------------------------------------------------------------------------------- |
443 // |
401 // |
444 METHOD |
402 String String::from_number (short int a) |
445 String::from_number (short int a) -> String |
|
446 { |
403 { |
447 char buf[32]; |
404 char buf[32]; |
448 ::sprintf (buf, "%d", a); |
405 ::sprintf (buf, "%d", a); |
449 return String (buf); |
406 return String (buf); |
450 } |
407 } |
451 |
408 |
452 // ------------------------------------------------------------------------------------------------- |
409 // ------------------------------------------------------------------------------------------------- |
453 // |
410 // |
454 METHOD |
411 String String::from_number (int a) |
455 String::from_number (int a) -> String |
|
456 { |
412 { |
457 char buf[32]; |
413 char buf[32]; |
458 ::sprintf (buf, "%d", a); |
414 ::sprintf (buf, "%d", a); |
459 return String (buf); |
415 return String (buf); |
460 } |
416 } |
461 |
417 |
462 // ------------------------------------------------------------------------------------------------- |
418 // ------------------------------------------------------------------------------------------------- |
463 // |
419 // |
464 METHOD |
420 String String::from_number (long int a) |
465 String::from_number (long int a) -> String |
|
466 { |
421 { |
467 char buf[32]; |
422 char buf[32]; |
468 ::sprintf (buf, "%ld", a); |
423 ::sprintf (buf, "%ld", a); |
469 return String (buf); |
424 return String (buf); |
470 } |
425 } |
471 |
426 |
472 // ------------------------------------------------------------------------------------------------- |
427 // ------------------------------------------------------------------------------------------------- |
473 // |
428 // |
474 METHOD |
429 String String::from_number (unsigned short int a) |
475 String::from_number (unsigned short int a) -> String |
|
476 { |
430 { |
477 char buf[32]; |
431 char buf[32]; |
478 ::sprintf (buf, "%u", a); |
432 ::sprintf (buf, "%u", a); |
479 return String (buf); |
433 return String (buf); |
480 } |
434 } |
481 |
435 |
482 // ------------------------------------------------------------------------------------------------- |
436 // ------------------------------------------------------------------------------------------------- |
483 // |
437 // |
484 METHOD |
438 String String::from_number (unsigned int a) |
485 String::from_number (unsigned int a) -> String |
|
486 { |
439 { |
487 char buf[32]; |
440 char buf[32]; |
488 ::sprintf (buf, "%u", a); |
441 ::sprintf (buf, "%u", a); |
489 return String (buf); |
442 return String (buf); |
490 } |
443 } |
491 |
444 |
492 // ------------------------------------------------------------------------------------------------- |
445 // ------------------------------------------------------------------------------------------------- |
493 // |
446 // |
494 METHOD |
447 String String::from_number (unsigned long int a) |
495 String::from_number (unsigned long int a) -> String |
|
496 { |
448 { |
497 char buf[32]; |
449 char buf[32]; |
498 ::sprintf (buf, "%lu", a); |
450 ::sprintf (buf, "%lu", a); |
499 return String (buf); |
451 return String (buf); |
500 } |
452 } |
501 |
453 |
502 // ------------------------------------------------------------------------------------------------- |
454 // ------------------------------------------------------------------------------------------------- |
503 // |
455 // |
504 METHOD |
456 String String::from_number (double a) |
505 String::from_number (double a) -> String |
|
506 { |
457 { |
507 char buf[64]; |
458 char buf[64]; |
508 ::sprintf (buf, "%f", a); |
459 ::sprintf (buf, "%f", a); |
509 return String (buf); |
460 return String (buf); |
510 } |
461 } |
511 |
462 |
512 // ------------------------------------------------------------------------------------------------- |
463 // ------------------------------------------------------------------------------------------------- |
513 // |
464 // |
514 METHOD |
465 String String::md5() const |
515 String::md5() const -> String |
|
516 { |
466 { |
517 char checksum[33]; |
467 char checksum[33]; |
518 CalculateMD5 (reinterpret_cast<const unsigned char*> (chars()), length(), checksum); |
468 CalculateMD5 (reinterpret_cast<const unsigned char*> (chars()), length(), checksum); |
519 checksum[sizeof checksum - 1] = '\0'; |
469 checksum[sizeof checksum - 1] = '\0'; |
520 return String (checksum); |
470 return String (checksum); |
521 } |
471 } |
522 |
472 |
523 // ------------------------------------------------------------------------------------------------- |
473 // ------------------------------------------------------------------------------------------------- |
524 // |
474 // |
525 METHOD |
475 void String::normalize (int (*filter)(int)) |
526 String::normalize (int (*filter)(int)) -> void |
|
527 { |
476 { |
528 int a = 0; |
477 int a = 0; |
529 int b = length() - 1; |
478 int b = length() - 1; |
530 |
479 |
531 while ((*filter) (m_string[a]) and a != b) |
480 while ((*filter) (m_string[a]) and a != b) |