]> gitweb.factorcode.org Git - factor.git/commitdiff
Revert "interpolate: allow format directives to be used" main master
authorJohn Benediktsson <mrjbq7@gmail.com>
Sat, 4 May 2024 11:37:22 +0000 (06:37 -0500)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 4 May 2024 11:37:22 +0000 (06:37 -0500)
This reverts commit 69588f398b7c02e73292f8a09b50656586734e3c.

basis/interpolate/interpolate-docs.factor
basis/interpolate/interpolate-tests.factor
basis/interpolate/interpolate.factor

index d0e5ca31ad47bad47ab414a9cd0c41c2b063b4ac..bf8a637f9bbf4809d636f030f1ede88bd9dbd678 100644 (file)
@@ -3,7 +3,7 @@ IN: interpolate
 
 HELP: interpolate
 { $values { "str" string } }
-{ $description "String interpolation using named variables and/or stack arguments, writing to the " { $link output-stream } ". Format directives from the " { $vocab-link "formatting" } " vocabulary can be used as well." }
+{ $description "String interpolation using named variables and/or stack arguments, writing to the " { $link output-stream } "." }
 { $notes "Stack arguments are numbered from the top of the stack, or provided anonymously by order of arguments." }
 { $examples
     { $example
@@ -21,11 +21,6 @@ HELP: interpolate
         "\"Mr.\" \"Anderson\"" "\"Hello, ${} ${}\" interpolate"
         "Hello, Mr. Anderson"
     }
-    { $example
-        "USING: interpolate ;"
-        "1.2345 \"${:011.5f}\" interpolate"
-        "00001.23450"
-    }
 } ;
 
 HELP: interpolate>string
index 9649a91af2f5737067ed52f6908e7da569412fba..771bd76cdb87134b3663083ed2f99c2fddffad8f 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2008 Slava Pestov.
 ! See https://factorcode.org/license.txt for BSD license.
-USING: formatting interpolate io.streams.string namespaces tools.test ;
+USING: interpolate io.streams.string namespaces tools.test locals ;
 
 { "A B" } [ "A" "B" "${1} ${0}" interpolate>string ] unit-test
 { "B A" } [ "A" "B" "${0} ${1}" interpolate>string ] unit-test
@@ -44,9 +44,3 @@ USING: formatting interpolate io.streams.string namespaces tools.test ;
 ] unit-test
 
 { "hello, world" } [ "world" I"hello, ${0}" ] unit-test
-
-{ "hello, ####world" } [ "world" I"hello, ${0:'#9s}" ] unit-test
-
-{ "0123.4500" } [ 123.45 I"${:09.4f}" ] unit-test
-
-[ I"${:ABCD}" ] [ unknown-format-directive? ] must-fail-with
index d65dc0716519297d9df7dff60c0c3c759af0c76a..9d7115ef866997f58a777ba3f4a3f001e2b3113a 100644 (file)
@@ -1,10 +1,8 @@
 ! 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 fry generalizations io io.streams.string kernel
+make math math.order math.parser multiline namespaces present
+sequences splitting strings strings.parser vocabs.parser ;
 IN: interpolate
 
 <PRIVATE
@@ -18,18 +16,15 @@ TUPLE: anon-var ;
 : (parse-interpolate) ( str -- )
     [
         "${" split1-slice [
-            [ >string [ ] 2array , ] unless-empty
+            [ >string , ] unless-empty
         ] [
             [
                 "}" split1-slice
                 [
-                    >string ":" split1 [
-                        [ string>number ]
-                        [ 1 + stack-var boa ]
-                        [ [ anon-var new ] [ named-var boa ] if-empty ] ?if
-                    ] [
-                        [ [ present ] ] [ format-directive ] if-empty
-                    ] bi* 2array ,
+                    >string
+                    [ string>number ]
+                    [ 1 + stack-var boa ]
+                    [ [ anon-var new ] [ named-var boa ] if-empty ] ?if ,
                 ]
                 [ (parse-interpolate) ] bi*
             ] when*
@@ -38,8 +33,8 @@ TUPLE: anon-var ;
 
 : deanonymize ( seq -- seq' )
     0 over <reversed> [
-        dup first anon-var? [
-            [ 1 + dup stack-var boa ] dip second 2array
+        dup anon-var? [
+            drop 1 + dup stack-var boa
         ] when
     ] map! 2drop ;
 
@@ -48,7 +43,7 @@ TUPLE: anon-var ;
 
 : max-stack-var ( seq -- n/f )
     f [
-        first dup stack-var? [ n>> [ or ] keep max ] [ drop ] if
+        dup stack-var? [ n>> [ or ] keep max ] [ drop ] if
     ] reduce ;
 
 :: (interpolate-quot) ( str quot -- quot' )
@@ -56,14 +51,16 @@ TUPLE: anon-var ;
     args max-stack-var    :> vars
 
     args [
-        [
-            {
-                { [ dup named-var? ] [ name>> quot call '[ _ @ ] ] }
-                { [ dup stack-var? ] [ n>> '[ _ npick ] ] }
-                [ 1quotation ]
-            } cond
-        ] dip '[ @ @ write ]
-    ] { } assoc>map concat
+        dup named-var? [
+            name>> quot call '[ _ @ present write ]
+        ] [
+            dup stack-var? [
+                n>> '[ _ npick present write ]
+            ] [
+                '[ _ write ]
+            ] if
+        ] if
+    ] map concat
 
     vars [
         '[ _ ndrop ] append