Skip to content

Commit 737ae20

Browse files
committed
feat(string): ARK-292, add string:lstrip, string:rstrip and string:strip to remove whitespaces from strings
1 parent d4eb7da commit 737ae20

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

String.ark

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,57 @@
342342
(set _index (+ 1 _index)) })
343343
_output }))
344344

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+
345396
# @brief Strip the margin of a multiline string
346397
# @param _str multiline string, margin is (space)*(|)
347398
# =begin

tests/string-tests.ark

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@
6161
(test:eq "hello" (string:join ["hello"] ";"))
6262
(test:eq "" (string:join [] ";;"))
6363

64+
(test:eq "abc" (string:lstrip "abc"))
65+
(test:eq "abc" (string:lstrip " \t\n\rabc"))
66+
(test:eq "abc \t\n\r" (string:lstrip "abc \t\n\r"))
67+
(test:eq "" (string:lstrip ""))
68+
(test:eq "" (string:lstrip " \t\n\r"))
69+
70+
(test:eq "abc" (string:rstrip "abc"))
71+
(test:eq "abc" (string:rstrip "abc \t\n\r"))
72+
(test:eq " \t\n\rabc" (string:rstrip " \t\n\rabc"))
73+
(test:eq "" (string:rstrip ""))
74+
(test:eq "" (string:rstrip " \t\n\r"))
75+
76+
(test:eq "abc" (string:strip "\t\n\r abc \t\n\r"))
77+
(test:eq "" (string:strip "\t\n\r "))
78+
(test:eq "abc" (string:strip "abc"))
79+
(test:eq "" (string:strip ""))
80+
(test:eq "abc" (string:strip " \t\n\rabc"))
81+
(test:eq "abc" (string:strip "abc \t\n\r"))
82+
6483
(test:eq "" (string:stripMargin ""))
6584
(test:eq " hello" (string:stripMargin " hello"))
6685
(test:eq "hello" (string:stripMargin " |hello"))

0 commit comments

Comments
 (0)