Skip to content

Commit 69d782b

Browse files
committed
feat(String): adding string:stripMargin
1 parent 7b86e89 commit 69d782b

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

String.ark

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
(mut _index _startingIndex)
8888

8989
(let _end
90-
(if (> _length (len _string))
90+
(if (>= _length (len _string))
9191
(len _string)
9292
(+ _index _length)))
9393

@@ -183,3 +183,29 @@
183183
"")))
184184
(set _index (+ 1 _index)) })
185185
_output }))
186+
187+
# @brief Strip the margin of a multiline string
188+
# @param _str multiline string, margin is (space)*(|)
189+
# =begin
190+
# (let s "hello
191+
# |abc
192+
# |def")
193+
# (string:stripMargin s)
194+
# =end
195+
(let stripMargin (fun (_str) {
196+
(mut _output "")
197+
(let _lines (split _str "\n"))
198+
(let _line_count (len _lines))
199+
(mut _index 0)
200+
201+
(while (< _index _line_count) {
202+
(let _current (@ _lines _index))
203+
(let _marker_pos (string:find _current "|"))
204+
(if (= -1 _marker_pos)
205+
(set _output (+ _output _current))
206+
(set _output (+ _output (slice _current (+ 1 _marker_pos) (len _current)))))
207+
(set _index (+ 1 _index))
208+
(if (!= _index _line_count)
209+
(set _output (+ _output "\n"))) })
210+
211+
_output }))

tests/string-tests.ark

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,16 @@
2929

3030
(test:eq "hello;3.14;true;world" (string:join ["hello" 3.14 true "world"] ";"))
3131
(test:eq "hello" (string:join ["hello"] ";"))
32-
(test:eq "" (string:join [] ";;"))})
32+
(test:eq "" (string:join [] ";;"))
33+
34+
(test:eq "" (string:stripMargin ""))
35+
(test:eq " hello" (string:stripMargin " hello"))
36+
(test:eq "hello" (string:stripMargin " |hello"))
37+
(test:eq "hello\nabc"
38+
(string:stripMargin " |hello
39+
|abc"))
40+
(test:eq "hello\nabc\nd"
41+
(string:stripMargin "hello
42+
|abc
43+
|d"))
44+
})

0 commit comments

Comments
 (0)