From f765b2b9426f4329166a9af267db435b614fdeb8 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 11 Apr 2017 19:57:48 -0700 Subject: [PATCH] introduce co-string struct --- pitfall/pitfall/parse.rkt | 16 ++++++++-------- pitfall/pitfall/render.rkt | 7 +++++-- pitfall/pitfall/struct.rkt | 3 ++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/pitfall/pitfall/parse.rkt b/pitfall/pitfall/parse.rkt index 78bb3851..90e19d4f 100644 --- a/pitfall/pitfall/parse.rkt +++ b/pitfall/pitfall/parse.rkt @@ -50,17 +50,17 @@ [("f") "\f"] [else sub])))] [str (regexp-replace* @pregexp{\\(\d{2,3})} str (λ (m sub) (string (integer->char (string->number sub 8)))))]) - str)])) + (co-string str))])) (module+ test - (check-equal? @pf-string{(Testing)} "Testing") + (check-equal? @pf-string{(Testing)} (co-string "Testing")) (check-equal? (pf-string @string-append{(Test\ - ing)}) "Testing") - (check-equal? @pf-string{(Test\)ing)} "Test)ing") - (check-equal? @pf-string{(Test\ning)} "Test\ning") - (check-equal? @pf-string{(Test\\ing)} "Test\\ing") - (check-equal? @pf-string{(A\53B)} "A+B") - (check-equal? @pf-string{(A\053B)} "A+B") + ing)}) (co-string "Testing")) + (check-equal? @pf-string{(Test\)ing)} (co-string "Test)ing")) + (check-equal? @pf-string{(Test\ning)} (co-string "Test\ning")) + (check-equal? @pf-string{(Test\\ing)} (co-string "Test\\ing")) + (check-equal? @pf-string{(A\53B)} (co-string "A+B")) + (check-equal? @pf-string{(A\053B)} (co-string "A+B")) #;(check-equal? @pf-string{(D:19990209153925-08\'00\')}) #;(check-true (andmap byte? @pf-string{<1C2D3F>})) #;(check-true (andmap byte? @pf-string{<1C 2D 3F>}))) diff --git a/pitfall/pitfall/render.rkt b/pitfall/pitfall/render.rkt index 987c1b44..f7aff82a 100644 --- a/pitfall/pitfall/render.rkt +++ b/pitfall/pitfall/render.rkt @@ -34,7 +34,7 @@ (if (co-io? cosexpr) (cons (cons (co-io-idx cosexpr) offset) io-locs) io-locs)))) - (define header-str (cosexpr->bytes (co-header "%PDF-1.3\n%¥±ë"))) + (define header-str (cosexpr->bytes (co-header "%PDF-1.4\n%¥±ë"))) (define trailer-str (cosexpr->bytes (co-trailer (co-dict (hasheq 'Size (length bstrs) 'Root (co-io-ref 1 0)))))) (define last-offset (for/sum ([bstr (in-list bstrs)]) @@ -78,6 +78,7 @@ >>}] [(co-io-ref? x) @string-append{@(loop (co-io-ref-idx x)) @(loop (co-io-ref-rev x)) R}] + [(co-string? x) (format "(~a)" (co-string-string x))] [(co-stream? x) @string-append{ @(loop (co-stream-dict x)) @@ -120,4 +121,6 @@ (co-dict (apply hasheq xs))) (define (make-co-stream bstr) - (co-stream (make-co-dict 'Length (bytes-length bstr)) bstr)) \ No newline at end of file + (co-stream (make-co-dict 'Length (bytes-length bstr)) bstr)) + +(cosexpr->bytes (make-co-dict 'Hello (co-string "World"))) \ No newline at end of file diff --git a/pitfall/pitfall/struct.rkt b/pitfall/pitfall/struct.rkt index 9b141a9c..d0a1b449 100644 --- a/pitfall/pitfall/struct.rkt +++ b/pitfall/pitfall/struct.rkt @@ -6,7 +6,8 @@ (struct co-stream (dict data) #:transparent) (struct co-version (num) #:transparent) (struct co-header (string) #:transparent) +(struct co-string (string) #:transparent) (struct co-io (idx rev thing) #:transparent) (struct co-io-ref (idx rev) #:transparent) (struct co-comment (text) #:transparent) -(struct co-trailer (dict) #:transparent) \ No newline at end of file +(struct co-trailer (dict) #:transparent)