From e7c1a2f62bbb1616f481e66c5b1aea1824053615 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sun, 6 Apr 2014 15:53:19 -0500 Subject: PS: fix string escaping. Passes all tests. --- ps/core.ps | 2 +- ps/printer.ps | 5 ++++- ps/reader.ps | 7 +++++++ ps/types.ps | 26 +++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/ps/core.ps b/ps/core.ps index 8a0a92c..35f2b19 100644 --- a/ps/core.ps +++ b/ps/core.ps @@ -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 } diff --git a/ps/printer.ps b/ps/printer.ps index 956bb18..1be0963 100644 --- a/ps/printer.ps +++ b/ps/printer.ps @@ -43,7 +43,10 @@ obj 10 slen string cvrs }{ /stringtype obj type eq { % if string print_readably { - (") obj (") concatenate concatenate + (") + obj (\\) (\\\\) replace + (") (\\") replace + (") concatenate concatenate }{ obj } ifelse diff --git a/ps/reader.ps b/ps/reader.ps index bdc4580..430f0c9 100644 --- a/ps/reader.ps +++ b/ps/reader.ps @@ -68,10 +68,17 @@ } 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 diff --git a/ps/types.ps b/ps/types.ps index 03c772f..a22246f 100644 --- a/ps/types.ps +++ b/ps/types.ps @@ -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 @@ -32,6 +32,30 @@ ] } 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 -- cgit v1.2.3