|
431 | 431 | (set _output (+ _output "\n"))) }) |
432 | 432 |
|
433 | 433 | _output })) |
| 434 | + |
| 435 | +# @brief Check if a string starts with a given prefix |
| 436 | +# @param _str string |
| 437 | +# @param _prefix prefix to look for |
| 438 | +# =begin |
| 439 | +# (print (string:startsWith? "Hello, world" "Hell")) # true |
| 440 | +# (print (string:startsWith? "Hello, world" ", world")) # false |
| 441 | +# =end |
| 442 | +# @author https://github.com/SuperFola |
| 443 | +(let startsWith? (fun (_str _prefix) |
| 444 | + (= 0 (find _str _prefix)))) |
| 445 | + |
| 446 | +# @brief Check if a string ends with a given suffix |
| 447 | +# @param _str string |
| 448 | +# @param _suffix suffix to look for |
| 449 | +# =begin |
| 450 | +# (print (string:endsWith? "Hello, world" "ld")) # true |
| 451 | +# (print (string:endsWith? "Hello, world" "worl")) # false |
| 452 | +# =end |
| 453 | +# @author https://github.com/SuperFola |
| 454 | +(let endsWith? (fun (_str _suffix) |
| 455 | + (= 0 (find (reverse _str) (reverse _suffix))))) |
| 456 | + |
| 457 | +# @brief Return a string filled with '0' digits to make a string of length _n |
| 458 | +# @param _str string to left fill |
| 459 | +# @param _n width of the final string |
| 460 | +# =begin |
| 461 | +# (print (string:zfill "42" 4)) # 0042 |
| 462 | +# =end |
| 463 | +# @author https://github.com/SuperFola |
| 464 | +(let zfill (fun (_str _n) |
| 465 | + (if (>= (len _str) _n) |
| 466 | + _str |
| 467 | + (+ (repeat "0" (- _n (len _str))) _str)))) |
| 468 | + |
| 469 | +# @brief Return a centered string of length _len, using spaces as fill chars |
| 470 | +# @param _str string to center |
| 471 | +# @param _len width of the final string |
| 472 | +# =begin |
| 473 | +# (print (string:center "ArkScript" 15)) # " ArkScript " |
| 474 | +# =end |
| 475 | +# @author https://github.com/SuperFola |
| 476 | +(let center (fun (_str _len) |
| 477 | + (if (>= (len _str) _len) |
| 478 | + _str |
| 479 | + { |
| 480 | + (let _fill (- _len (len _str))) |
| 481 | + (let _fill_left (if (= 1 (mod _fill 2)) (/ (- _fill 1) 2) (/ _fill 2))) |
| 482 | + (let _fill_right (if (= 1 (mod _fill 2)) (/ (+ _fill 1) 2) (/ _fill 2))) |
| 483 | + (+ |
| 484 | + (repeat " " _fill_left) |
| 485 | + _str |
| 486 | + (repeat " " _fill_right)) }))) |
| 487 | + |
| 488 | +# @brief If a string starts with a given prefix, remove it |
| 489 | +# @param _str string |
| 490 | +# @param _prefix prefix to remove |
| 491 | +# =begin |
| 492 | +# (print (string:removePrefix "TestCase" "Test")) # Case |
| 493 | +# (print (string:removePrefix "BaseTestCase" "Test")) # BaseTestCase |
| 494 | +# =end |
| 495 | +# @author https://github.com/SuperFola |
| 496 | +(let removePrefix (fun (_str _prefix) |
| 497 | + (if (startsWith? _str _prefix) |
| 498 | + (slice _str (len _prefix) (len _str)) |
| 499 | + _str))) |
| 500 | + |
| 501 | +# @brief If a string ends with a given suffix, remove it |
| 502 | +# @param _str string |
| 503 | +# @param _suffix suffix to remove |
| 504 | +# =begin |
| 505 | +# (print (string:removeSuffix "TestCase" "Case")) # Test |
| 506 | +# (print (string:removeSuffix "BaseTestCase" "Test")) # BaseTestCase |
| 507 | +# =end |
| 508 | +# @author https://github.com/SuperFola |
| 509 | +(let removeSuffix (fun (_str _suffix) |
| 510 | + (if (endsWith? _str _suffix) |
| 511 | + (slice _str 0 (- (len _str) (len _suffix))) |
| 512 | + _str))) |
0 commit comments