[REBOL] Re: [Nifty Function of the Day] Pad
From: dockimbel:free at: 15-Aug-2003 13:17
A J Martin wrote:
> With acknowledgements to Bohdan "Bo" Lechnowsky:
> Pad: function [
> "Pads a value with leading zeroes or a specified fill character."
> Value [string! number!] Length [integer!]
> /Fill Character [char!] "Optional Fill Character".
> ] [Field] [
> if not string? Value [
> Value: form Value
> if none? Character [
> Character: #"0"
> insert/dup Field: copy "" Character Length
> change/part Field head reverse Value length? Value
> head reverse Field
You can avoid testing if Value is a string! in order to FORM it. FORM won't
change the value if it's already a string!, it will just COPY it.
You can also write a shorter version of 'pad :
pad: func [
"Pads a value with leading zeroes or a specified fill character."
value [string! number!] n [integer!]
/with c [char!] "Optional Fill Character"
value: form value
head insert/dup value any [all [with c] #"0"] n - length? value