[REBOL] Re: [Nifty Function of the Day] Pad
From: greggirwin:mindspring at: 15-Aug-2003 8:57
Hi Bo, et al
BoRL> And an even shorter variation:
And a longer version, with a different name and more features:
justify: func [
{Justify the given string to the specified width.}
s [string!] "The string to justify"
wd [integer!] "The target width, in characters"
/left "Left justify the string (default)"
/center {Center justify the string. If the total length of the padding
is an odd number of characters, the extra character will be on
the right.}
/right "Right justify the string"
/with {Allows you to specify filler other than space. If you specify a
string more than 1 character in length, it will be repeated as
many times as necessary.}
filler [string! char!] "The character, or string, to use as filler"
/local pad-len result
][
if 0 >= pad-len: (wd - length? s) [return s]
filler: form any [filler " "]
result: head insert/dup make string! wd filler wd / length? filler
; If they gave us a multi-char filler, and it isn't evenly multiplied
; into the desired width, we have to add some extra chars at the end
; to make up for the difference.
if wd > length? result [
append result copy/part filler wd - length? result
]
pos: either center [
add 1 to integer! divide pad-len 2
][
either right [add 1 pad-len][1]
]
head change/part at result pos s length? s
]
-- Gregg