angerangel edited this page Jan 3, 2013 · 1 revision

USAGE

   FORMAT rules values /pad p

DESCRIPTION

Format a string according to the format dialect.

FORMAT is a function value.

ARGUMENTS

  • rules -- A block in the format dialect. E.g. [10 -10 #"-" 4]
  • values

REFINEMENTS

  • /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
] ]
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.