]> gitweb.factorcode.org Git - factor.git/blob - extra/printf/printf.factor
Merge branch 'master' into experimental (untested!)
[factor.git] / extra / printf / printf.factor
1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: io io.encodings.ascii io.files io.streams.string combinators
5 kernel sequences splitting strings math math.functions math.parser 
6 macros fry peg.ebnf ascii unicode.case arrays quotations vectors ;
7
8 IN: printf
9
10 <PRIVATE
11
12 : compose-all ( seq -- quot )
13     [ ] [ compose ] reduce ;
14
15 : fix-sign ( string -- string )
16     dup CHAR: 0 swap index 0 = 
17       [ dup 0 swap [ [ CHAR: 0 = not ] keep digit? and ] find-from
18          [ dup 1- rot dup [ nth ] dip swap
19             {
20                { CHAR: - [ [ 1- ] dip remove-nth "-" prepend ] }
21                { CHAR: + [ [ 1- ] dip remove-nth "+" prepend ] }
22                [ drop swap drop ] 
23             } case 
24          ] [ drop ] if
25       ] when ;
26
27 : >digits ( string -- digits ) 
28     [ 0 ] [ string>number ] if-empty ;
29
30 : pad-digits ( string digits -- string' )
31     [ "." split1 ] dip [ CHAR: 0 pad-right ] [ head-slice ] bi "." glue ;
32
33 : max-digits ( n digits -- n' )
34     10 swap ^ [ * round ] keep / ;
35
36 : max-width ( string length -- string' ) 
37     short head ;
38
39 : >exp ( x -- exp base )
40     [ 
41         abs 0 swap
42         [ dup [ 10.0 >= ] [ 1.0 < ] bi or ]
43         [ dup 10.0 >=
44           [ 10.0 / [ 1+ ] dip ]
45           [ 10.0 * [ 1- ] dip ] if
46         ] [ ] while 
47      ] keep 0 < [ neg ] when ;
48
49 : exp>string ( exp base digits -- string )
50     [ max-digits ] keep -rot
51     [
52         [ 0 < "-" "+" ? ]
53         [ abs number>string 2 CHAR: 0 pad-left ] bi 
54         "e" -rot 3append
55     ]
56     [ number>string ] bi*
57     rot pad-digits prepend ;
58
59 EBNF: parse-format-string
60
61 zero      = "0"                  => [[ CHAR: 0 ]]
62 char      = "'" (.)              => [[ second ]]
63
64 pad-char  = (zero|char)?         => [[ CHAR: \s or ]]
65 pad-align = ("-")?               => [[ \ pad-right \ pad-left ? ]] 
66 pad-width = ([0-9])*             => [[ >digits ]]
67 pad       = pad-align pad-char pad-width => [[ reverse >quotation dup first 0 = [ drop [ ] ] when ]]
68
69 sign      = ("+")?               => [[ [ dup CHAR: - swap index [ "+" prepend ] unless ] [ ] ? ]]
70
71 width_    = "." ([0-9])*         => [[ second >digits '[ _ max-width ] ]]
72 width     = (width_)?            => [[ [ ] or ]] 
73
74 digits_   = "." ([0-9])*         => [[ second >digits ]]
75 digits    = (digits_)?           => [[ 6 or ]]
76
77 fmt-%     = "%"                  => [[ [ "%" ] ]] 
78 fmt-c     = "c"                  => [[ [ 1string ] ]]
79 fmt-C     = "C"                  => [[ [ 1string >upper ] ]]
80 fmt-s     = "s"                  => [[ [ ] ]]
81 fmt-S     = "S"                  => [[ [ >upper ] ]]
82 fmt-d     = "d"                  => [[ [ >fixnum number>string ] ]]
83 fmt-e     = digits "e"           => [[ first '[ >exp _ exp>string ] ]]
84 fmt-E     = digits "E"           => [[ first '[ >exp _ exp>string >upper ] ]]
85 fmt-f     = digits "f"           => [[ first dup '[ >float _ max-digits number>string _ pad-digits ] ]] 
86 fmt-x     = "x"                  => [[ [ >hex ] ]]
87 fmt-X     = "X"                  => [[ [ >hex >upper ] ]]
88 unknown   = (.)*                 => [[ "Unknown directive" throw ]]
89
90 strings_  = fmt-c|fmt-C|fmt-s|fmt-S
91 strings   = pad width strings_   => [[ reverse compose-all ]]
92
93 numbers_  = fmt-d|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
94 numbers   = sign pad numbers_    => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
95
96 formats   = "%" (strings|numbers|fmt-%|unknown) => [[ second '[ _ dip ] ]]
97
98 plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]]
99
100 text      = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
101
102 ;EBNF
103
104 PRIVATE>
105
106 MACRO: printf ( format-string -- )
107     parse-format-string [ length ] keep compose-all '[ _ <vector> @ reverse [ write ] each ] ;
108
109 : sprintf ( format-string -- result )
110     [ printf ] with-string-writer ; inline
111
112