From 18305b8d2f0dba08b4ce92376d52b4fe892becbb Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Wed, 28 Jun 2017 09:28:05 -0700 Subject: [PATCH] resume in writeString --- pitfall/restructure/string-test.rkt | 7 +++++++ pitfall/restructure/string.rkt | 15 ++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pitfall/restructure/string-test.rkt b/pitfall/restructure/string-test.rkt index 8fc0397c..db0bc96f 100644 --- a/pitfall/restructure/string-test.rkt +++ b/pitfall/restructure/string-test.rkt @@ -146,6 +146,13 @@ https://github.com/mbutterick/restructure/blob/master/test/String.coffee ; string = new StringT 7 ; string.encode(stream, 'testing') ; stream.end() + +(let ([string (+StringT 7)] + [stream (+EncodeStream)]) + (send string encode stream "testing") + (check-equal? (send stream dump) "testing")) + + ; ; it 'should encode length as number before string', (done) -> ; stream = new EncodeStream diff --git a/pitfall/restructure/string.rkt b/pitfall/restructure/string.rkt index 8c8017fb..55df7f43 100644 --- a/pitfall/restructure/string.rkt +++ b/pitfall/restructure/string.rkt @@ -30,13 +30,18 @@ https://github.com/mbutterick/restructure/blob/master/src/String.coffee string) - #;(define/augment (encode stream val [parent #f]) - (define bytes (($codec-encoder codec) (format "~a" val))) + (define/augment (encode stream val [parent #f]) + (define encoding__ + (cond + [(procedure? encoding_) (or (encoding_ (and parent (· parent val)) 'ascii))] + [else encoding_])) + (when (NumberT? length_) + (send length_ encode stream (byteLength val encoding__))) + (send stream writeString val encoding__) + (when (not length_) + (send stream writeUInt8 #x00))) - (when (Number? length_) ;; length-prefixed string - (send length_ encode stream (bytes-length bytes))) - (send stream write bytes)) (define/override (size [val #f] [parent #f]) ;; Use the defined value if no value was given