commit b7c2142e00d1999edb20778e18d60ae487978f47 parent db463436c3f01a90b3d77c50bcfdf5cd0655cc3f Author: Yuval Langer <yuval.langer@gmail.com> Date: Tue, 9 Jan 2024 03:21:23 +0200 Put algorithmic bits where they belong to make the code make more sense, also DRY Diffstat:
M | rsv/arbitrary-null.scm | | | 55 | +++++++++++++++++++++++-------------------------------- |
1 file changed, 23 insertions(+), 32 deletions(-)
diff --git a/rsv/arbitrary-null.scm b/rsv/arbitrary-null.scm @@ -47,40 +47,31 @@ (row->rsv row null-value? port) (loop rows))))) - (define (field->scm null-value port) - (match (peek-u8 port) - ((? null-value-byte?) - (read-u8 port) ;; remove null byte. - (read-u8 port) ;; remove value terminator byte. - null-value) - ((? value-terminator-byte?) - (read-u8 port) ;; remove value terminator byte. - "") - (byte - (let ((output-field-port (open-output-bytevector))) - (let loop () - (match (read-u8 port) - ((? value-terminator-byte? byte) - (utf8->string (get-output-bytevector output-field-port))) - (byte - (write-u8 byte - output-field-port) - (loop)))))))) + (define (string->scm null-value port) + (let ((output-field-port (open-output-bytevector))) + (let loop () + (match (read-u8 port) + ((? value-terminator-byte? byte) + (utf8->string (get-output-bytevector output-field-port))) + (byte + (write-u8 byte + output-field-port) + (loop)))))) (define (row->scm null-value port) - (match (peek-u8 port) - ((? row-terminator-byte?) - (read-u8 port) ;; remove row terminator. - '()) - (_ - (let loop ((row '())) - (match (peek-u8 port) - ((? row-terminator-byte? byte) - (read-u8 port) ;; Remove row terminator. - (reverse row)) - (byte - (loop (cons (field->scm null-value port) - row)))))))) + (let loop ((row '())) + (match (peek-u8 port) + ((? row-terminator-byte? byte) ;; End of row. + (read-u8 port) ;; Remove row terminator. + (reverse row)) + ((? null-value-byte?) ;; Field is null. + (read-u8 port) ;; remove null byte. + (read-u8 port) ;; remove value terminator byte. + (loop (cons null-value + row))) + (byte ;; Field is a string. + (loop (cons (string->scm null-value port) + row)))))) (define (rsv->scm null-value port) (let loop ((rows '()))