~williewillus/public-inbox

racket-rfc8949: Make read-n-bytes performant (tm) v1 SUPERSEDED

: 1
 Make read-n-bytes performant (tm)

 1 files changed, 12 insertions(+), 21 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~williewillus/public-inbox/patches/21883/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH racket-rfc8949] Make read-n-bytes performant (tm) Export this patch

From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>

---
 main.rkt | 33 ++++++++++++---------------------
 1 file changed, 12 insertions(+), 21 deletions(-)

diff --git a/main.rkt b/main.rkt
index 73366e5..430069d 100644
--- a/main.rkt
+++ b/main.rkt
@@ -8,15 +8,13 @@
; Parameter that holds the object used to represent undefined
(define cbor-undefined (make-parameter 'undefined #f 'cbor-undefined))

; read n bytes from port into buf. n is inferred from the length of buf
(define (read-n-bytes buf port)
  (define n (bytes-length buf))
  (let loop ([cur 0])
    (if (= cur n)
        buf
        (begin
          (bytes-set! buf cur (read-byte port))
          (loop (add1 cur))))))
; read exactly n bytes, or throw
(define (read-n-bytes n port)
  (let* ([bytes (read-bytes n port)]
         [len   (if (eof-object? bytes) 0 (bytes-length bytes))])
    (if (= len n)
      bytes
      (error "Unexpected EOF: Expected ~a bytes, got ~a" n len))))

(define (read-arg-as-int port major additional)
  (if (< additional 24)
@@ -58,14 +56,13 @@
(define (cbor-read-byte-string port additional)
  (define payload-len (read-arg-as-int port 2 additional))
  ; todo implement 'indefinite and 'break support
  (let ([buf (make-bytes payload-len)])
    (bytes->immutable-bytes (read-n-bytes buf port))))
  (let ([buf (read-n-bytes payload-len port)])
    (bytes->immutable-bytes buf)))

(define (cbor-read-string port additional)
  (define payload-len (read-arg-as-int port 3 additional))
  ; todo implement 'indefinite and 'break support
  (let ([buf (make-bytes payload-len)])
    (read-n-bytes buf port)
  (let ([buf (read-n-bytes payload-len port)])
    (string->immutable-string (bytes->string/utf-8 buf))))

(define (cbor-read-list port additional)
@@ -135,14 +132,8 @@
            (if (zero? (bitwise-and half #x8000))
                val
                (- val))))]
        [(26)
         (define buf (bytes 0 0 0 0))
         (read-n-bytes buf port)
         (floating-point-bytes->real buf #t)]
        [(27)
         (define buf (bytes 0 0 0 0 0 0 0 0))
         (read-n-bytes buf port)
         (floating-point-bytes->real buf #t)]
        [(26) (floating-point-bytes->real (read-n-bytes 4 port) #t)]
        [(27) (floating-point-bytes->real (read-n-bytes 8 port) #t)]
        [(28 29 30) (error "Additional information ~a is reserved in major type 7" additional)]
        [(31) (error "Unexpected break code")]))) ; We handle break explicitly in deserialization of lists of things, so it should never appear here.

-- 
2.25.1