Skip to content

Commit c647c15

Browse files
committed
feat(string): add string:lpad and string:rpad
1 parent 0a1de1e commit c647c15

2 files changed

Lines changed: 61 additions & 20 deletions

File tree

String.ark

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,42 @@
465465
(let _end (- (len _str) (len _suffix)))
466466
(= _end (findAfter _str _suffix _end)) }))
467467

468+
# @brief Return a string left padded by a given character _c to make a string of length _n
469+
# @param _str string to left pad
470+
# @param _n width of the final string
471+
# @param _c char to use for padding
472+
# =begin
473+
# (print (string:lpad "42" 4 "a")) # aa42
474+
# =end
475+
# @author https://github.com/SuperFola
476+
(let lpad (fun (_str _n _c) {
477+
(assert (= 1 (len _c)) "string:lpad expected a single character to pad")
478+
(if (>= (len _str) _n)
479+
_str
480+
(+ (repeat _c (- _n (len _str))) _str)) }))
481+
482+
# @brief Return a string right padded by a given character _c to make a string of length _n
483+
# @param _str string to left pad
484+
# @param _n width of the final string
485+
# @param _c char to use for padding
486+
# =begin
487+
# (print (string:rpad "42" 4 "a")) # 42aa
488+
# =end
489+
# @author https://github.com/SuperFola
490+
(let rpad (fun (_str _n _c) {
491+
(assert (= 1 (len _c)) "string:rpad expected a single character to pad")
492+
(if (>= (len _str) _n)
493+
_str
494+
(+ _str (repeat _c (- _n (len _str))))) }))
495+
468496
# @brief Return a string filled with '0' digits to make a string of length _n
469497
# @param _str string to left fill
470498
# @param _n width of the final string
471499
# =begin
472500
# (print (string:zfill "42" 4)) # 0042
473501
# =end
474502
# @author https://github.com/SuperFola
475-
(let zfill (fun (_str _n)
476-
(if (>= (len _str) _n)
477-
_str
478-
(+ (repeat "0" (- _n (len _str))) _str))))
503+
(let zfill (fun (_str _n) (lpad _str _n "0")))
479504

480505
# @brief Return a centered string of length _len, using spaces as fill chars
481506
# @param _str string to center

tests/string-tests.ark

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,17 @@
125125
(string:stripMargin " |hello
126126
|abc")
127127
"hello\nabc")
128-
(test:eq
129-
(string:stripMargin "hello
130-
|abc
131-
|d")
132-
"hello\nabc\nd")
133-
(test:eq
134-
(string:stripMargin "hello
135-
|
136-
|abc
137-
|d")
138-
"hello\n\nabc\nd") })
128+
(test:eq
129+
(string:stripMargin "hello
130+
|abc
131+
|d")
132+
"hello\nabc\nd")
133+
(test:eq
134+
(string:stripMargin "hello
135+
|
136+
|abc
137+
|d")
138+
"hello\n\nabc\nd") })
139139

140140
(test:case "startsWith?" {
141141
(test:expect (string:startsWith? "hello world" "hello"))
@@ -145,11 +145,27 @@
145145
(test:expect (string:startsWith? "" "")) })
146146

147147
(test:case "endsWith?" {
148-
(test:expect (string:endsWith? "hello world" "world"))
149-
(test:expect (not (string:endsWith? "hello world" "World")))
150-
(test:expect (not (string:endsWith? "hello world" "hello")))
151-
(test:expect (not (string:endsWith? "hello world" "worl")))
152-
(test:expect (string:endsWith? "" "")) })
148+
(test:expect (string:endsWith? "hello world" "world"))
149+
(test:expect (not (string:endsWith? "hello world" "World")))
150+
(test:expect (not (string:endsWith? "hello world" "hello")))
151+
(test:expect (not (string:endsWith? "hello world" "worl")))
152+
(test:expect (string:endsWith? "" "")) })
153+
154+
(test:case "lpad" {
155+
(test:eq (string:lpad "" 4 "1") "1111")
156+
(test:eq (string:lpad "2" 4 "1") "1112")
157+
(test:eq (string:lpad "22" 4 "1") "1122")
158+
(test:eq (string:lpad "222" 4 "1") "1222")
159+
(test:eq (string:lpad "2222" 4 "1") "2222")
160+
(test:eq (string:lpad "22222" 4 "1") "22222") })
161+
162+
(test:case "rpad" {
163+
(test:eq (string:rpad "" 4 "1") "1111")
164+
(test:eq (string:rpad "2" 4 "1") "2111")
165+
(test:eq (string:rpad "22" 4 "1") "2211")
166+
(test:eq (string:rpad "222" 4 "1") "2221")
167+
(test:eq (string:rpad "2222" 4 "1") "2222")
168+
(test:eq (string:rpad "22222" 4 "1") "22222") })
153169

154170
(test:case "zfill" {
155171
(test:eq (string:zfill "" 4) "0000")

0 commit comments

Comments
 (0)