! See https://factorcode.org/license.txt for BSD license
USING: accessors arrays assocs byte-arrays calendar
calendar.english calendar.private combinators combinators.smart
-generalizations io io.streams.string kernel math math.functions
-math.parser multiline namespaces peg.ebnf present prettyprint
-quotations sequences sequences.generalizations splitting strings
-unicode ;
+generalizations interpolate.private io io.streams.string kernel
+math math.functions math.parser multiline namespaces peg.ebnf
+present prettyprint quotations sequences
+sequences.generalizations splitting strings unicode ;
IN: formatting
ERROR: unknown-format-directive value ;
formats = (types|fmt-%|lists|assocs|unknown)
]=]
+SINGLETON: printf-formatter
+printf-formatter formatter set-global
+
+M: printf-formatter format
+ [ [ present ] ] [ format-directive ] if-empty ;
+
EBNF: parse-printf [=[
formats = "%"~ <foreign format-directive formats>
plain-text = [^%]+ => [[ >string ]]
! Copyright (C) 2008, 2009 Slava Pestov.
! See https://factorcode.org/license.txt for BSD license.
-USING: accessors arrays assocs combinators formatting
-formatting.private fry generalizations io io.streams.string
-kernel make math math.order math.parser multiline namespaces
-present quotations sequences splitting strings strings.parser
-vocabs.parser ;
+
+USING: accessors arrays assocs combinators generalizations io
+io.streams.string kernel make math math.order math.parser
+multiline namespaces present quotations sequences splitting
+strings strings.parser vocabs.parser ;
+
IN: interpolate
<PRIVATE
+SYMBOL: formatter
+
+HOOK: format formatter ( directive -- quot )
+
+M: f format drop [ present ] ;
+
TUPLE: named-var name ;
TUPLE: stack-var n ;
[ string>number ]
[ 1 + stack-var boa ]
[ [ anon-var new ] [ named-var boa ] if-empty ] ?if
- ] [
- [ [ present ] ] [ format-directive ] if-empty
- ] bi* 2array ,
+ ] [ format ] bi* 2array ,
]
[ (parse-interpolate) ] bi*
] when*