main
Matthew Butterick 8 years ago
parent c89971e5b5
commit ac7b8bc69b

@ -26,14 +26,14 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(define lazy (· options lazy))
(define relative-getter-or-0 (or (· options relativeTo) (λ (ctx) 0))) ; changed this to a simple lambda
(define/augment (decode stream [ctx #f])
(define offset (send offset-type decode stream ctx))
(define/augment (decode port [ctx #f])
(define offset (send offset-type decode port ctx))
(cond
[(and allow-null (= offset null-value)) #f] ; handle null pointers
[else
(define relative (+ (caseq pointer-style
[(local) (· ctx _startOffset)]
[(immediate) (- (· stream pos) (send offset-type size))]
[(immediate) (- (pos port) (send offset-type size))]
[(parent) (· ctx parent _startOffset)]
[(global) (or (· (find-top-ctx ctx) _startOffset) 0)]
[else (error 'unknown-pointer-style)])
@ -45,10 +45,10 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(cond
[(not (void? val)) val]
[else
(define orig-pos (· stream pos))
(send stream pos ptr)
(set! val (send type decode stream ctx))
(send stream pos orig-pos)
(define orig-pos (pos port))
(pos port ptr)
(set! val (send type decode port ctx))
(pos port orig-pos)
val]))
(if lazy
(LazyThunk decode-value)

@ -1,5 +1,5 @@
#lang reader (submod "racket.rkt" reader)
(require "stream.rkt" "utils.rkt")
(require "utils.rkt")
(provide (all-defined-out))
#|
@ -7,15 +7,15 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Reserved.coffee
|#
(define-subclass Streamcoder (Reserved type [count 1])
(define-subclass xenomorph-base% (Reserved type [count 1])
(define/augment (decode stream parent)
(send stream pos (+ (· stream pos) (size #f parent)))
(define/augment (decode port parent)
(pos port (+ (pos port) (size #f parent)))
(void))
(define/augment (size [val #f] [parent #f])
(* (send type size) (resolve-length count #f parent)))
(define/augment (encode stream val [parent #f])
(send stream fill 0 (size val parent))))
(define/augment (encode port val [parent #f])
(make-bytes (size val parent) 0)))

@ -7,34 +7,60 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/String.coffee
|#
(define (read-encoded-string port len [encoding 'ascii])
(define proc (caseq encoding
[(utf16le) (error 'bah)]
[(ucs2) (error 'bleh)]
[(utf8) bytes->string/utf-8]
[(ascii) bytes->string/latin-1]
[else identity]))
(proc (read-bytes len port)))
(define (write-encoded-string port string [encoding 'ascii])
;; todo: handle encodings correctly.
;; right now just utf8 and ascii are correct
(caseq encoding
[(utf16le ucs2 utf8 ascii) (write-bytes (string->bytes/utf-8 string) port)
(when (eq? encoding 'utf16le)
(error 'swap-bytes-unimplemented))]
[else (error 'unsupported-string-encoding)]))
(define (count-nonzero-chars port)
;; helper function for String
;; counts nonzero chars from current position
(length (car (regexp-match-peek "[^\u0]*" port))))
(define (byte-length val encoding)
(define encoder
(caseq encoding
[(ascii utf8) string->bytes/utf-8]))
(bytes-length (encoder (format "~a" val))))
(define-subclass Streamcoder (StringT [len #f] [encoding 'ascii])
(define (bytes-left-in-port? port)
(not (eof-object? (peek-byte port))))
(define-subclass xenomorph-base% (StringT [len #f] [encoding 'ascii])
(define/augment (decode stream [parent #f])
(let ([len (or (resolve-length len stream parent) (send stream count-nonzero-chars))]
(define/augment (decode port [parent #f])
(let ([len (or (resolve-length len port parent) (count-nonzero-chars port))]
[encoding (if (procedure? encoding)
(or (encoding parent) 'ascii)
encoding)]
[adjustment (if (and (not len) (< (· stream pos) (· stream length))) 1 0)])
(define string (send stream readString len encoding))
(send stream pos (+ (· stream pos) adjustment))
[adjustment (if (and (not len) (bytes-left-in-port? port)) 1 0)])
(define string (read-encoded-string port len encoding))
(pos port (+ (pos port) adjustment))
string))
(define/augment (encode stream val [parent #f])
(define/augment (encode port val [parent #f])
(let* ([val (format "~a" val)]
[encoding (if (procedure? encoding)
(or (encoding (and parent (· parent val)) 'ascii))
encoding)])
(when (NumberT? len)
(send len encode stream (byte-length val encoding)))
(send stream writeString val encoding)
(when (not len) (send stream writeUInt8 #x00)))) ; null terminated when no len
(send len encode port (byte-length val encoding)))
(write-encoded-string port val encoding)
(when (not len) (write-byte #x00 port)))) ; null terminated when no len
(define/augment (size [val #f] [parent #f])

@ -14,9 +14,9 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; should.not.exist pointer.decode(stream, _startOffset: 50)
(let ([stream (+DecodeStream (+Buffer '(0)))]
(let ([stream (open-input-bytes (bytes 0))]
[pointer (+Pointer uint8 uint8)])
(check-false (decode pointer stream (mhash '_startOffset 50))))
(check-false (decode pointer stream #:parent (mhash '_startOffset 50))))
;
; it 'should use local offsets from start of parent by default', ->
@ -25,9 +25,9 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; pointer.decode(stream, _startOffset: 0).should.equal 53
(let ([stream (+DecodeStream (+Buffer '(1 53)))]
(let ([stream (open-input-bytes (bytes 1 53))]
[pointer (+Pointer uint8 uint8)])
(check-equal? (decode pointer stream (mhash '_startOffset 0)) 53))
(check-equal? (decode pointer stream #:parent (mhash '_startOffset 0)) 53))
;
@ -37,7 +37,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; pointer.decode(stream).should.equal 53
(let ([stream (+DecodeStream (+Buffer '(1 53)))]
(let ([stream (open-input-bytes (bytes 1 53))]
[pointer (+Pointer uint8 uint8 (mhash 'type 'immediate))])
(check-equal? (decode pointer stream) 53))
@ -49,10 +49,10 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; pointer.decode(stream, parent: _startOffset: 2).should.equal 53
(let ([stream (+DecodeStream (+Buffer '(0 0 1 53)))]
(let ([stream (open-input-bytes (bytes 0 0 1 53))]
[pointer (+Pointer uint8 uint8 (mhash 'type 'parent))])
(pos stream 2)
(check-equal? (decode pointer stream (mhash 'parent (mhash '_startOffset 2))) 53))
(check-equal? (decode pointer stream #:parent (mhash 'parent (mhash '_startOffset 2))) 53))
;
@ -63,10 +63,10 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; pointer.decode(stream, parent: parent: _startOffset: 2).should.equal 53
(let ([stream (+DecodeStream (+Buffer '(1 2 4 0 0 0 53)))]
(let ([stream (open-input-bytes (bytes 1 2 4 0 0 0 53))]
[pointer (+Pointer uint8 uint8 (mhash 'type 'global))])
(pos stream 2)
(check-equal? (decode pointer stream (mhash 'parent (mhash 'parent (mhash '_startOffset 2)))) 53))
(check-equal? (decode pointer stream #:parent (mhash 'parent (mhash 'parent (mhash '_startOffset 2)))) 53))
; it 'should support offsets relative to a property on the parent', ->
@ -74,9 +74,9 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; pointer = new Pointer uint8, uint8, relativeTo: 'parent.ptr'
; pointer.decode(stream, _startOffset: 0, parent: ptr: 4).should.equal 53
(let ([stream (+DecodeStream (+Buffer '(1 0 0 0 0 53)))]
(let ([stream (open-input-bytes (bytes 1 0 0 0 0 53))]
[pointer (+Pointer uint8 uint8 (mhash 'relativeTo (λ (ctx) (· ctx parent ptr))))])
(check-equal? (decode pointer stream (mhash '_startOffset 0 'parent (mhash 'ptr 4))) 53))
(check-equal? (decode pointer stream #:parent (mhash '_startOffset 0 'parent (mhash 'ptr 4))) 53))
;
@ -85,9 +85,9 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; pointer = new Pointer uint8, 'void'
; pointer.decode(stream, _startOffset: 0).should.equal 4
(let ([stream (+DecodeStream (+Buffer '(4)))]
(let ([stream (open-input-bytes (bytes 4))]
[pointer (+Pointer uint8 'void)])
(check-equal? (decode pointer stream (mhash '_startOffset 0)) 4))
(check-equal? (decode pointer stream #:parent (mhash '_startOffset 0)) 4))
@ -101,7 +101,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; Object.getOwnPropertyDescriptor(res, 'ptr').enumerable.should.equal(true)
; res.ptr.should.equal 53
(let ([stream (+DecodeStream (+Buffer '(1 53)))]
(let ([stream (open-input-bytes (bytes 1 53))]
[struct (+Struct (dictify 'ptr (+Pointer uint8 uint8 (mhasheq 'lazy #t))))])
(define res (decode struct stream))
(check-true (LazyThunk? (hash-ref (get-field _kv res) 'ptr)))
@ -212,13 +212,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; stream.end()
(let ([stream (+EncodeStream)]
(let ([stream (open-output-bytes)]
[ptr (+Pointer uint8 uint8)]
[ctx (mhash 'pointerSize 0
'startOffset 0
'pointerOffset 0
'pointers null)])
(encode ptr stream #f ctx)
(encode ptr #f stream #:parent ctx)
(check-equal? (· ctx pointerSize) 0)
(check-equal? (dump stream) (+Buffer '(0))))
@ -245,7 +245,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; stream.end()
(let ([stream (+EncodeStream)]
(let ([stream (open-output-bytes)]
[ptr (+Pointer uint8 uint8)]
[ctx (mhash 'pointerSize 0
'startOffset 0
@ -281,13 +281,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
;
; stream.end()
(let ([stream (+EncodeStream)]
(let ([stream (open-output-bytes)]
[ptr (+Pointer uint8 uint8 (mhash 'type 'immediate))]
[ctx (mhash 'pointerSize 0
'startOffset 0
'pointerOffset 1
'pointers null)])
(encode ptr stream 10 ctx)
(encode ptr stream 10 #:parent ctx)
(check-equal? (· ctx pointerOffset) 2)
(check-equal? (· ctx pointers) (list (mhasheq 'type uint8
'val 10
@ -318,13 +318,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
;
; stream.end()
(let ([stream (+EncodeStream)]
(let ([stream (open-output-bytes)]
[ptr (+Pointer uint8 uint8 (mhash 'type 'parent))]
[ctx (mhash 'parent (mhash 'pointerSize 0
'startOffset 3
'pointerOffset 5
'pointers null))])
(encode ptr stream 10 ctx)
(encode ptr stream 10 #:parent ctx)
(check-equal? (· ctx parent pointerOffset) 6)
(check-equal? (· ctx parent pointers) (list (mhasheq 'type uint8
'val 10
@ -358,7 +358,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; stream.end()
(let ([stream (+EncodeStream)]
(let ([stream (open-output-bytes)]
[ptr (+Pointer uint8 uint8 (mhash 'type 'global))]
[ctx (mhash 'parent
(mhash 'parent
@ -366,7 +366,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
'startOffset 3
'pointerOffset 5
'pointers null))))])
(encode ptr stream 10 ctx)
(encode ptr stream 10 #:parent ctx)
(check-equal? (· ctx parent parent parent pointerOffset) 6)
(check-equal? (· ctx parent parent parent pointers) (list (mhasheq 'type uint8
'val 10
@ -399,14 +399,14 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; stream.end()
(let ([stream (+EncodeStream)]
(let ([stream (open-output-bytes)]
[ptr (+Pointer uint8 uint8 (mhash 'relativeTo (λ (ctx) (· ctx ptr))))]
[ctx (mhash 'pointerSize 0
'startOffset 0
'pointerOffset 10
'pointers null
'val (mhash 'ptr 4))])
(encode ptr stream 10 ctx)
(encode ptr stream 10 #:parent ctx)
(check-equal? (· ctx pointerOffset) 11)
(check-equal? (· ctx pointers) (list (mhasheq 'type uint8
'val 10
@ -435,13 +435,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
;
; stream.end()
(let ([stream (+EncodeStream)]
(let ([stream (open-output-bytes)]
[ptr (+Pointer uint8 'void)]
[ctx (mhash 'pointerSize 0
'startOffset 0
'pointerOffset 1
'pointers null)])
(encode ptr stream (+VoidPointer uint8 55) ctx)
(encode ptr stream (+VoidPointer uint8 55) #:parent ctx)
(check-equal? (· ctx pointerOffset) 2)
(check-equal? (· ctx pointers) (list (mhasheq 'type uint8
'val 55
@ -464,10 +464,10 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
; ptr.encode(stream, 44, ctx)
(let ([stream (+EncodeStream)]
(let ([stream (open-output-bytes)]
[ptr (+Pointer uint8 'void)]
[ctx (mhash 'pointerSize 0
'startOffset 0
'pointerOffset 1
'pointers null)])
(check-exn exn:fail:contract? (λ () (encode ptr stream 44 ctx))))
(check-exn exn:fail:contract? (λ () (encode ptr stream 44 #:parent ctx))))

@ -7,44 +7,26 @@ https://github.com/mbutterick/restructure/blob/master/test/Reserved.coffee
;describe 'Reserved', ->
; it 'should have a default count of 1', ->
; reserved = new Reserved uint8
; reserved.size().should.equal 1
(let ([reserved (+Reserved uint8)])
(check-equal? (size reserved) 1))
(check-equal? (size (+Reserved uint8)) 1)
;
; it 'should allow custom counts and types', ->
; reserved = new Reserved uint16, 10
; reserved.size().should.equal 20
(let ([reserved (+Reserved uint16be 10)])
(check-equal? (size reserved) 20))
(check-equal? (size (+Reserved uint16be 10)) 20)
;
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0, 0]
; reserved = new Reserved uint16
; should.not.exist reserved.decode(stream)
; stream.pos.should.equal 2
(let ([stream (+DecodeStream (+Buffer '(0 0)))]
[reserved (+Reserved uint16be)])
(check-equal? (decode reserved stream) (void))
(check-equal? (pos stream) 2))
(parameterize ([current-input-port (open-input-bytes (bytes 0 0))])
(define reserved (+Reserved uint16be))
(check-equal? (decode reserved) (void))
(check-equal? (pos (current-input-port)) 2))
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; reserved = new Reserved uint16
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0, 0]
; done()
;
; reserved.encode stream
; stream.end()
(let ([stream (+EncodeStream)]
[reserved (+Reserved uint16be)])
(encode reserved stream)
(check-equal? (dump stream) (+Buffer '(0 0))))
(parameterize ([current-output-port (open-output-bytes)])
(define reserved (+Reserved uint16be))
(encode reserved #f)
(check-equal? (dump (current-output-port)) (bytes 0 0)))

@ -9,227 +9,120 @@ https://github.com/mbutterick/restructure/blob/master/test/String.coffee
;describe 'String', ->
; describe 'decode', ->
; it 'should decode fixed length', ->
; stream = new DecodeStream new Buffer 'testing'
; string = new StringT 7
; string.decode(stream).should.equal 'testing'
(let ([stream (+DecodeStream (+Buffer "testing"))]
[string (+StringT 7)])
(check-equal? (decode string stream) "testing"))
(parameterize ([current-input-port (open-input-bytes #"testing")])
(check-equal? (decode (+StringT 7)) "testing"))
;
; it 'should decode length from parent key', ->
; stream = new DecodeStream new Buffer 'testing'
; string = new StringT 'len'
; string.decode(stream, len: 7).should.equal 'testing'
(let ([stream (+DecodeStream (+Buffer "testing"))]
[string (+StringT 'len)])
(check-equal? (decode string stream (mhash 'len 7)) "testing"))
(parameterize ([current-input-port (open-input-bytes #"testing")])
(check-equal? (decode (+StringT 'len) #:parent (mhash 'len 7)) "testing"))
;
; it 'should decode length as number before string', ->
; stream = new DecodeStream new Buffer '\x07testing'
; string = new StringT uint8
; string.decode(stream).should.equal 'testing'
; octal \7 will print as \a
(let ([stream (+DecodeStream (+Buffer "\x07testing"))]
[string (+StringT uint8)])
(check-equal? (decode string stream (mhash 'len 7)) "testing"))
;
; it 'should decode utf8', ->
; stream = new DecodeStream new Buffer '🍻'
; string = new StringT 4, 'utf8'
; string.decode(stream).should.equal '🍻'
(let ([stream (+DecodeStream (+Buffer "🍻"))]
[string (+StringT 4 'utf8)])
(check-equal? (decode string stream) "🍻"))
;
; it 'should decode encoding computed from function', ->
; stream = new DecodeStream new Buffer '🍻'
; string = new StringT 4, -> 'utf8'
; string.decode(stream).should.equal '🍻'
(let ([stream (+DecodeStream (+Buffer "🍻"))]
[string (+StringT 4 (λ _ 'utf8))])
(check-equal? (decode string stream) "🍻"))
;
(parameterize ([current-input-port (open-input-bytes #"\x07testing")])
(check-equal? (decode (+StringT uint8) #:parent (mhash 'len 7)) "testing"))
;; it 'should decode utf8', ->
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻"))])
(check-equal? (decode (+StringT 4 'utf8)) "🍻"))
;; it 'should decode encoding computed from function', ->
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻"))])
(check-equal? (decode (+StringT 4 (λ _ 'utf8))) "🍻"))
; it 'should decode null-terminated string and read past terminator', ->
; stream = new DecodeStream new Buffer '🍻\x00'
; string = new StringT null, 'utf8'
; string.decode(stream).should.equal '🍻'
; stream.pos.should.equal 5
(let ([stream (+DecodeStream (+Buffer "🍻\x00"))]
[string (+StringT #f 'utf8)])
(check-equal? (decode string stream) "🍻")
(check-equal? (pos stream) 5))
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻\x00"))])
(check-equal? (decode (+StringT #f 'utf8)) "🍻")
(check-equal? (pos (current-input-port)) 5))
;
; it 'should decode remainder of buffer when null-byte missing', ->
; stream = new DecodeStream new Buffer '🍻'
; string = new StringT null, 'utf8'
; string.decode(stream).should.equal '🍻'
(let ([stream (+DecodeStream (+Buffer "🍻"))]
[string (+StringT #f 'utf8)])
(check-equal? (decode string stream) "🍻"))
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻"))])
(check-equal? (decode (+StringT #f 'utf8)) "🍻"))
;
; describe 'size', ->
; it 'should use string length', ->
; string = new StringT 7
; string.size('testing').should.equal 7
(let ([string (+StringT 7)])
(check-equal? (size string "testing") 7))
(check-equal? (size (+StringT 7) "testing") 7)
;
; it 'should use correct encoding', ->
; string = new StringT 10, 'utf8'
; string.size('🍻').should.equal 4
(let ([string (+StringT 10 'utf8)])
(check-equal? (size string "🍻") 4))
(check-equal? (size (+StringT 10 'utf8) "🍻") 4)
;
; it 'should use encoding from function', ->
; string = new StringT 10, -> 'utf8'
; string.size('🍻').should.equal 4
(let ([string (+StringT 10 (λ _ 'utf8))])
(check-equal? (size string "🍻") 4))
(check-equal? (size (+StringT 10 (λ _ 'utf8)) "🍻") 4)
;
; it 'should add size of length field before string', ->
; string = new StringT uint8, 'utf8'
; string.size('🍻').should.equal 5
(let ([string (+StringT uint8 'utf8)])
(check-equal? (size string "🍻") 5))
(check-equal? (size (+StringT uint8 'utf8) "🍻") 5)
; todo
; it 'should work with utf16be encoding', ->
; string = new StringT 10, 'utf16be'
; string.size('🍻').should.equal 4
;
; it 'should take null-byte into account', ->
; string = new StringT null, 'utf8'
; string.size('🍻').should.equal 5
(let ([string (+StringT #f 'utf8)])
(check-equal? (size string "🍻") 5))
(check-equal? (size (+StringT #f 'utf8) "🍻") 5)
;
; it 'should use defined length if no value given', ->
; array = new StringT 10
; array.size().should.equal 10
(let ([string (+StringT 10)])
(check-equal? (size string) 10))
(check-equal? (size (+StringT 10)) 10)
;
; describe 'encode', ->
; it 'should encode using string length', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer 'testing'
; done()
;
; string = new StringT 7
; string.encode(stream, 'testing')
; stream.end()
(let ([string (+StringT 7)]
[stream (+EncodeStream)])
(encode string stream "testing")
(check-equal? (dump stream) #"testing"))
;
(parameterize ([current-output-port (open-output-bytes)])
(encode (+StringT 7) "testing")
(check-equal? (dump (current-output-port)) #"testing"))
; it 'should encode length as number before string', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer '\x07testing'
; done()
;
; string = new StringT uint8
; string.encode(stream, 'testing')
; stream.end()
(let ([string (+StringT uint8)]
[stream (+EncodeStream)])
(encode string stream "testing")
(check-equal? (dump stream) #"\x07testing"))
;
(parameterize ([current-output-port (open-output-bytes)])
(encode (+StringT uint8) "testing")
(check-equal? (dump (current-output-port)) #"\x07testing"))
; it 'should encode length as number before string utf8', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer '\x0ctesting 😜', 'utf8'
; done()
;
; string = new StringT uint8, 'utf8'
; string.encode(stream, 'testing 😜')
; stream.end()
(let ([string (+StringT uint8 'utf8)]
[stream (+EncodeStream)])
(encode string stream "testing 😜")
(check-equal? (dump stream) (+Buffer "\x0ctesting 😜" 'utf8)))
;
(parameterize ([current-output-port (open-output-bytes)])
(encode (+StringT uint8 'utf8) "testing 😜")
(check-equal? (dump (current-output-port)) (string->bytes/utf-8 "\x0ctesting 😜")))
; it 'should encode utf8', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer '🍻'
; done()
;
; string = new StringT 4, 'utf8'
; string.encode(stream, '🍻')
; stream.end()
(let ([string (+StringT 4 'utf8)]
[stream (+EncodeStream)])
(encode string stream "🍻")
(check-equal? (dump stream) (+Buffer "🍻")))
;
(parameterize ([current-output-port (open-output-bytes)])
(encode (+StringT 4 'utf8) "🍻" )
(check-equal? (dump (current-output-port)) (string->bytes/utf-8 "🍻")))
; it 'should encode encoding computed from function', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer '🍻'
; done()
;
; string = new StringT 4, -> 'utf8'
; string.encode(stream, '🍻')
; stream.end()
(let ([string (+StringT 4 (λ _ 'utf8))]
[stream (+EncodeStream)])
(encode string stream "🍻")
(check-equal? (dump stream) (+Buffer "🍻")))
;
(parameterize ([current-output-port (open-output-bytes)])
(encode (+StringT 4 (λ _ 'utf8)) "🍻")
(check-equal? (dump (current-output-port)) (string->bytes/utf-8 "🍻")))
; it 'should encode null-terminated string', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer '🍻\x00'
; done()
;
; string = new StringT null, 'utf8'
; string.encode(stream, '🍻')
; stream.end()
(let ([string (+StringT #f 'utf8)]
[stream (+EncodeStream)])
(encode string stream "🍻")
(check-equal? (dump stream) (+Buffer "🍻\x00")))
(parameterize ([current-output-port (open-output-bytes)])
(encode (+StringT #f 'utf8) "🍻" )
(check-equal? (dump (current-output-port)) (string->bytes/utf-8 "🍻\x00")))
Loading…
Cancel
Save