|
342 | 342 | (set _index (+ 1 _index)) }) |
343 | 343 | _output })) |
344 | 344 |
|
| 345 | +# _dir = 1 -> left to right, -1 -> right to left |
| 346 | +(let __strip_in_dir (fun (_str _dir) |
| 347 | + (if (emptyOrWhitespace? _str) |
| 348 | + "" |
| 349 | + { |
| 350 | + (mut _i (if (= 1 _dir) 0 (- (len _str) 1))) |
| 351 | + (mut _whitespaces (or (= (@ _str _i) " ") (= (@ _str _i) "\t") (= (@ _str _i) "\n") (= (@ _str _i) "\r"))) |
| 352 | + |
| 353 | + (while (and _whitespaces (< _i (len _str)) (>= _i 0)) { |
| 354 | + (let _c (@ _str _i)) |
| 355 | + (if (and (!= _c " ") (!= _c "\t") (!= _c "\n") (!= _c "\r")) |
| 356 | + (set _whitespaces false) |
| 357 | + (set _i (+ _dir _i))) }) |
| 358 | + |
| 359 | + (if (= 1 _dir) |
| 360 | + (slice _str _i (len _str)) |
| 361 | + (slice _str 0 (+ 1 _i))) }))) |
| 362 | + |
| 363 | +# @brief Removes whitespaces from the left side of a string |
| 364 | +# @details The original string isn't modified |
| 365 | +# @param _str string to sanitize |
| 366 | +# =begin |
| 367 | +# (import std.String) |
| 368 | +# (print (string:lstrip " a b c")) # "a b c" |
| 369 | +# =end |
| 370 | +# @author https://github.com/SuperFola |
| 371 | +(let lstrip (fun (_str) |
| 372 | + (__strip_in_dir _str 1))) |
| 373 | + |
| 374 | +# @brief Removes whitespaces from the right side of a string |
| 375 | +# @details The original string isn't modified |
| 376 | +# @param _str string to sanitize |
| 377 | +# =begin |
| 378 | +# (import std.String) |
| 379 | +# (print (string:rstrip " a b c ")) # " a b c" |
| 380 | +# =end |
| 381 | +# @author https://github.com/SuperFola |
| 382 | +(let rstrip (fun (_str) |
| 383 | + (__strip_in_dir _str -1))) |
| 384 | + |
| 385 | +# @brief Removes whitespaces from both sides of a string |
| 386 | +# @details The original string isn't modified |
| 387 | +# @param _str string to sanitize |
| 388 | +# =begin |
| 389 | +# (import std.String) |
| 390 | +# (print (string:strip " a b c ")) # "a b c" |
| 391 | +# =end |
| 392 | +# @author https://github.com/SuperFola |
| 393 | +(let strip (fun (_str) |
| 394 | + (rstrip (lstrip _str)))) |
| 395 | + |
345 | 396 | # @brief Strip the margin of a multiline string |
346 | 397 | # @param _str multiline string, margin is (space)*(|) |
347 | 398 | # =begin |
|
0 commit comments