PS: fix string escaping. Passes all tests.
authorJoel Martin <github@martintribe.org>
Sun, 6 Apr 2014 20:53:19 +0000 (15:53 -0500)
committerJoel Martin <github@martintribe.org>
Sun, 6 Apr 2014 20:53:19 +0000 (15:53 -0500)
ps/core.ps
ps/printer.ps
ps/reader.ps
ps/types.ps

index 8a0a92c..35f2b19 100644 (file)
@@ -216,7 +216,7 @@ end } def
     (pr-str)  { /data get ( ) true _pr_str_args }
     (str)     { /data get () false _pr_str_args }
     (prn)     { /data get ( ) true _pr_str_args print (\n) print null }
-    (println) { /data get () false _pr_str_args print (\n) print null }
+    (println) { /data get ( ) false _pr_str_args print (\n) print null }
     (<)       { dup 0 _nth exch 1 _nth lt }
     (<=)      { dup 0 _nth exch 1 _nth le }
     (>)       { dup 0 _nth exch 1 _nth gt }
index 956bb18..1be0963 100644 (file)
         obj 10 slen string cvrs
     }{ /stringtype obj type eq { % if string
         print_readably {
-            (") obj (") concatenate concatenate
+            (")
+            obj (\\) (\\\\) replace
+                (") (\\") replace
+            (") concatenate concatenate
         }{
             obj
         } ifelse
index bdc4580..430f0c9 100644 (file)
         } if
         /ch str idx get def  % current character
         /idx idx 1 add def
+        ch 92 eq { % if \
+            str idx get 34 eq { %if \"
+                /idx idx 1 add def
+                /cnt cnt 1 add def % 1 more below
+            } if
+        } if
         ch 34 eq { exit } if % '"' is end of string
         /cnt cnt 1 add def
     } loop
     str start cnt getinterval % the matched string
+    (\\") (") replace
     str idx % return: new_string string new_idx
 } def
 
index 03c772f..a22246f 100644 (file)
@@ -3,7 +3,7 @@
 % General functions
 
 % concatenate: concatenate two strings or two arrays
-% From Thinking in PostScript 1990 Reid
+% From Thinking in PostScript 1990 Reid, Example 11.7
 % (string1) (string2) concatenate string3
 % array1 array2 concatenate array3
 /concatenate { %def
     ]
 } bind def
 
+% string1 string2 string3 -> replace -> string4
+% Return a string4 with all occurrences of string2 in string1 replaced
+% with string3
+/replace { 4 dict begin
+    /repstr exch def
+    /needle exch def
+    /haystack exch def
+    /result () def
+    { % loop
+        haystack needle search
+        { %if found
+            % stack: post match pre
+            repstr concatenate 3 1 roll pop % stack: pre+ post
+            /haystack exch def % stack: pre+
+            result exch concatenate /result exch def
+        }{
+            result exch concatenate /result exch def
+            exit
+        } ifelse
+    } loop
+    result
+end } def
+
+
 % objA objB -> _equal? -> bool
 /_equal? { 6 dict begin
     /b exch def