| 37 |
37 |
| 38 // ------------------------------------------------------------------------------------------------- |
38 // ------------------------------------------------------------------------------------------------- |
| 39 // |
39 // |
| 40 int String::compare (const String& other) const |
40 int String::compare (const String& other) const |
| 41 { |
41 { |
| 42 return m_string.compare (other.std_string()); |
42 return m_string.compare (other.stdString()); |
| 43 } |
43 } |
| 44 |
44 |
| 45 // ------------------------------------------------------------------------------------------------- |
45 // ------------------------------------------------------------------------------------------------- |
| 46 // |
46 // |
| 47 void String::trim (int n) |
47 void String::trim (int n) |
| 48 { |
48 { |
| 49 if (n > 0) |
49 if (n > 0) |
| 50 m_string = mid (0, length() - n).std_string(); |
50 m_string = mid (0, length() - n).stdString(); |
| 51 else |
51 else |
| 52 m_string = mid (n, -1).std_string(); |
52 m_string = mid (n, -1).stdString(); |
| 53 } |
53 } |
| 54 |
54 |
| 55 // ------------------------------------------------------------------------------------------------- |
55 // ------------------------------------------------------------------------------------------------- |
| 56 // |
56 // |
| 57 String String::strip (char unwanted) const |
57 String String::strip (char unwanted) const |
| 58 { |
58 { |
| 59 String result (m_string); |
59 String result (m_string); |
| 60 |
60 |
| 61 for (int pos = 0; (pos = result.find (unwanted)) != -1;) |
61 for (int pos = 0; (pos = result.find (unwanted)) != -1;) |
| 62 result.remove_at (pos--); |
62 result.removeAt (pos--); |
| 63 |
63 |
| 64 return result; |
64 return result; |
| 65 } |
65 } |
| 66 |
66 |
| 67 // ------------------------------------------------------------------------------------------------- |
67 // ------------------------------------------------------------------------------------------------- |
| 70 { |
70 { |
| 71 String result (m_string); |
71 String result (m_string); |
| 72 |
72 |
| 73 for (String c : unwanted) |
73 for (String c : unwanted) |
| 74 for (int pos = 0; (pos = result.find (c)) != -1;) |
74 for (int pos = 0; (pos = result.find (c)) != -1;) |
| 75 result.remove_at (pos--); |
75 result.removeAt (pos--); |
| 76 |
76 |
| 77 return result; |
77 return result; |
| 78 } |
78 } |
| 79 |
79 |
| 80 // ------------------------------------------------------------------------------------------------- |
80 // ------------------------------------------------------------------------------------------------- |
| 81 // |
81 // |
| 82 String String::to_uppercase() const |
82 String String::toUpperCase() const |
| 83 { |
83 { |
| 84 String result (m_string); |
84 String result (m_string); |
| 85 |
85 |
| 86 for (char &ch : result) |
86 for (char &ch : result) |
| 87 { |
87 { |
| 195 return String(chars() + this->length() - length); |
195 return String(chars() + this->length() - length); |
| 196 } |
196 } |
| 197 |
197 |
| 198 // ------------------------------------------------------------------------------------------------- |
198 // ------------------------------------------------------------------------------------------------- |
| 199 // |
199 // |
| 200 int String::word_position (int n) const |
|
| 201 { |
|
| 202 int count = 0; |
|
| 203 |
|
| 204 for (char ch : *this) |
|
| 205 { |
|
| 206 if (not isspace(ch) or ++count < n) |
|
| 207 continue; |
|
| 208 } |
|
| 209 |
|
| 210 return -1; |
|
| 211 } |
|
| 212 |
|
| 213 // ------------------------------------------------------------------------------------------------- |
|
| 214 // |
|
| 215 int String::find (const char* c, int a) const |
200 int String::find (const char* c, int a) const |
| 216 { |
201 { |
| 217 int pos = m_string.find (c, a); |
202 int pos = m_string.find (c, a); |
| 218 |
203 |
| 219 if (pos == int (std::string::npos)) |
204 if (pos == int (std::string::npos)) |
| 308 return newstr; |
293 return newstr; |
| 309 } |
294 } |
| 310 |
295 |
| 311 // ------------------------------------------------------------------------------------------------- |
296 // ------------------------------------------------------------------------------------------------- |
| 312 // |
297 // |
| 313 bool String::is_numeric() const |
298 bool String::isNumeric() const |
| 314 { |
299 { |
| 315 char* endptr; |
300 char* endptr; |
| 316 strtol (chars(), &endptr, 10); |
301 strtol (chars(), &endptr, 10); |
| 317 return not (endptr && *endptr); |
302 return not (endptr && *endptr); |
| 318 } |
303 } |
| 319 |
304 |
| 320 // ------------------------------------------------------------------------------------------------- |
305 // ------------------------------------------------------------------------------------------------- |
| 321 // |
306 // |
| 322 bool String::ends_with (const String& other) |
307 bool String::endsWith (const String& other) const |
| 323 { |
308 { |
| 324 if (length() < other.length()) |
309 if (length() < other.length()) |
| 325 return false; |
310 return false; |
| 326 |
311 |
| 327 const int ofs = length() - other.length(); |
312 const int ofs = length() - other.length(); |
| 328 return strncmp (chars() + ofs, other.chars(), other.length()) == 0; |
313 return strncmp (chars() + ofs, other.chars(), other.length()) == 0; |
| 329 } |
314 } |
| 330 |
315 |
| 331 // ------------------------------------------------------------------------------------------------- |
316 // ------------------------------------------------------------------------------------------------- |
| 332 // |
317 // |
| 333 bool String::starts_with (const String& other) const |
318 bool String::startsWith (const String& other) const |
| 334 { |
319 { |
| 335 if (length() < other.length()) |
320 if (length() < other.length()) |
| 336 return false; |
321 return false; |
| 337 |
322 |
| 338 return strncmp (chars(), other.chars(), other.length()) == 0; |
323 return strncmp (chars(), other.chars(), other.length()) == 0; |
| 373 { |
358 { |
| 374 String result; |
359 String result; |
| 375 |
360 |
| 376 for (const String &item : container()) |
361 for (const String &item : container()) |
| 377 { |
362 { |
| 378 if (result.is_empty() == false) |
363 if (result.isEmpty() == false) |
| 379 result += delim; |
364 result += delim; |
| 380 |
365 |
| 381 result += item; |
366 result += item; |
| 382 } |
367 } |
| 383 |
368 |
| 384 return result; |
369 return result; |
| 385 } |
370 } |
| 386 |
371 |
| 387 // ------------------------------------------------------------------------------------------------- |
372 // ------------------------------------------------------------------------------------------------- |
| 388 // |
373 // |
| 389 bool String::mask_against (const String& pattern) const |
374 bool String::maskAgainst (const String& pattern) const |
| 390 { |
375 { |
| 391 // Elevate to uppercase for case-insensitive matching |
376 // Elevate to uppercase for case-insensitive matching |
| 392 String pattern_upper = pattern.to_uppercase(); |
377 String pattern_upper = pattern.toUpperCase(); |
| 393 String this_upper = to_uppercase(); |
378 String this_upper = toUpperCase(); |
| 394 const char* maskstring = pattern_upper.chars(); |
379 const char* maskstring = pattern_upper.chars(); |
| 395 const char* mptr = &maskstring[0]; |
380 const char* mptr = &maskstring[0]; |
| 396 |
381 |
| 397 for (const char* sptr = this_upper.chars(); *sptr != '\0'; sptr++) |
382 for (const char* sptr = this_upper.chars(); *sptr != '\0'; sptr++) |
| 398 { |
383 { |
| 432 return true; |
417 return true; |
| 433 } |
418 } |
| 434 |
419 |
| 435 // ------------------------------------------------------------------------------------------------- |
420 // ------------------------------------------------------------------------------------------------- |
| 436 // |
421 // |
| 437 String String::from_number (short int a) |
422 String String::fromNumber (short int a) |
| 438 { |
423 { |
| 439 char buf[32]; |
424 char buf[32]; |
| 440 ::sprintf (buf, "%d", a); |
425 ::sprintf (buf, "%d", a); |
| 441 return String (buf); |
426 return String (buf); |
| 442 } |
427 } |
| 443 |
428 |
| 444 // ------------------------------------------------------------------------------------------------- |
429 // ------------------------------------------------------------------------------------------------- |
| 445 // |
430 // |
| 446 String String::from_number (int a) |
431 String String::fromNumber (int a) |
| 447 { |
432 { |
| 448 char buf[32]; |
433 char buf[32]; |
| 449 ::sprintf (buf, "%d", a); |
434 ::sprintf (buf, "%d", a); |
| 450 return String (buf); |
435 return String (buf); |
| 451 } |
436 } |
| 452 |
437 |
| 453 // ------------------------------------------------------------------------------------------------- |
438 // ------------------------------------------------------------------------------------------------- |
| 454 // |
439 // |
| 455 String String::from_number (long int a) |
440 String String::fromNumber (long int a) |
| 456 { |
441 { |
| 457 char buf[32]; |
442 char buf[32]; |
| 458 ::sprintf (buf, "%ld", a); |
443 ::sprintf (buf, "%ld", a); |
| 459 return String (buf); |
444 return String (buf); |
| 460 } |
445 } |
| 461 |
446 |
| 462 // ------------------------------------------------------------------------------------------------- |
447 // ------------------------------------------------------------------------------------------------- |
| 463 // |
448 // |
| 464 String String::from_number (unsigned short int a) |
449 String String::fromNumber (unsigned short int a) |
| 465 { |
450 { |
| 466 char buf[32]; |
451 char buf[32]; |
| 467 ::sprintf (buf, "%u", a); |
452 ::sprintf (buf, "%u", a); |
| 468 return String (buf); |
453 return String (buf); |
| 469 } |
454 } |
| 470 |
455 |
| 471 // ------------------------------------------------------------------------------------------------- |
456 // ------------------------------------------------------------------------------------------------- |
| 472 // |
457 // |
| 473 String String::from_number (unsigned int a) |
458 String String::fromNumber (unsigned int a) |
| 474 { |
459 { |
| 475 char buf[32]; |
460 char buf[32]; |
| 476 ::sprintf (buf, "%u", a); |
461 ::sprintf (buf, "%u", a); |
| 477 return String (buf); |
462 return String (buf); |
| 478 } |
463 } |
| 479 |
464 |
| 480 // ------------------------------------------------------------------------------------------------- |
465 // ------------------------------------------------------------------------------------------------- |
| 481 // |
466 // |
| 482 String String::from_number (unsigned long int a) |
467 String String::fromNumber (unsigned long int a) |
| 483 { |
468 { |
| 484 char buf[32]; |
469 char buf[32]; |
| 485 ::sprintf (buf, "%lu", a); |
470 ::sprintf (buf, "%lu", a); |
| 486 return String (buf); |
471 return String (buf); |
| 487 } |
472 } |
| 488 |
473 |
| 489 // ------------------------------------------------------------------------------------------------- |
474 // ------------------------------------------------------------------------------------------------- |
| 490 // |
475 // |
| 491 String String::from_number (double a) |
476 String String::fromNumber (double a) |
| 492 { |
477 { |
| 493 char buf[64]; |
478 char buf[64]; |
| 494 ::sprintf (buf, "%f", a); |
479 ::sprintf (buf, "%f", a); |
| 495 return String (buf); |
480 return String (buf); |
| 496 } |
481 } |