forked from rebol/rebol
-
Notifications
You must be signed in to change notification settings - Fork 2
Format
angerangel edited this page Mar 19, 2013
·
1 revision
FORMAT rules values /pad p
Format a string according to the format dialect.
FORMAT is a function value.
- rules -- A block in the format dialect. E.g. [10 -10 #"-" 4]
- values
-
/pad
- p
#SOURCE
format: make function! [ [
"Format a string according to the format dialect."
rules {A block in the format dialect. E.g. [10 -10 #"-" 4]}
values
/pad p
/local val rule out
][
p: any [p #" "]
unless block? :rules [rules: reduce [:rules] ]
unless block? :values [values: reduce [:values] ]
val: 0
foreach rule rules [
if word? :rule [rule: get rule]
val: val + switch/default type?/word :rule [
integer! [abs rule]
string! [length? rule]
char! [1]
] [0]
]
out: make string! val
insert/dup out p val
foreach rule rules [
if word? :rule [rule: get rule]
switch type?/word :rule [
integer! [
pad: rule
val: form first+ values
clear at val 1 + abs rule
if negative? rule [
pad: rule + length? val
if negative? pad [out: skip out negate pad]
pad: length? val
]
change out :val
out: skip out pad
]
string! [out: change out rule]
char! [out: change out rule]
]
]
if not tail? values [append out values]
head out
] ]