Skip to content

Commit 09d5600

Browse files
committed
FEAT: added pad mezzanine function
1 parent b75a0eb commit 09d5600

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/mezz/mezz-series.r

+15
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,21 @@ collect: func [
393393
either into [output] [head output]
394394
]
395395

396+
pad: func [
397+
"Pad a FORMed value on right side with spaces"
398+
str "Value to pad, FORM it if not a string"
399+
n [integer!] "Total size (in characters) of the new string (pad on left side if negative)"
400+
/with "Pad with char"
401+
c [char!]
402+
return: [string!] "Modified input string at head"
403+
][
404+
unless string? str [str: form str]
405+
head insert/dup
406+
any [all [n < 0 n: negate n str] tail str]
407+
any [c #" "]
408+
(n - length? str)
409+
]
410+
396411
format: function [
397412
"Format a string according to the format dialect."
398413
rules {A block in the format dialect. E.g. [10 -10 #"-" 4 $32 "green" $0]}

src/tests/units/series-test.r3

+10-2
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,18 @@ Rebol [
498498
--test-- "SORT/reverse"
499499
;@@ https://github.com/Oldes/Rebol-issues/issues/128
500500
--assert [9 3 2 1] = sort/reverse [9 1 2 3]
501+
===end-group===
501502

502503

503-
504-
504+
===start-group=== "PAD"
505+
--test-- "pad"
506+
--assert "ab " = pad "ab" 4
507+
--assert " ab" = pad "ab" -4
508+
--assert "12 " = pad 12 4
509+
--assert " 12" = pad 12 -4
510+
--test-- "pad/with"
511+
--assert "1200" = pad/with 12 4 #"0"
512+
--assert "0012" = pad/with 12 -4 #"0"
505513
===end-group===
506514

507515

0 commit comments

Comments
 (0)