aboutsummaryrefslogtreecommitdiff
path: root/module/util
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-11-03 14:44:27 +0100
committerHugo Hörnquist <hugo@hornquist.se>2019-11-03 14:44:27 +0100
commita7af480101881af9e007453c0003328fde89f3b1 (patch)
tree7f1bae5bb1bf97251ffc0ff2f6c7bf51dcef5752 /module/util
parentCleanup in parse. (diff)
downloadcalp-a7af480101881af9e007453c0003328fde89f3b1.tar.gz
calp-a7af480101881af9e007453c0003328fde89f3b1.tar.xz
Move strbuf to own file.
Diffstat (limited to 'module/util')
-rw-r--r--module/util/strbuf.scm47
1 files changed, 47 insertions, 0 deletions
diff --git a/module/util/strbuf.scm b/module/util/strbuf.scm
new file mode 100644
index 00000000..9c1b1e6a
--- /dev/null
+++ b/module/util/strbuf.scm
@@ -0,0 +1,47 @@
+(define-module (util strbuf)
+ :use-module (srfi srfi-9)
+ :use-module (rnrs bytevectors)
+ :use-module ((rnrs io ports)
+ :select (bytevector->string native-transcoder))
+ :use-module ((ice-9 optargs) :select (define*-public))
+ )
+
+(define-record-type <strbuf>
+ (make-strbuf% len bytes)
+ strbuf?
+ (len get-length set-length!)
+ (bytes get-bytes set-bytes!))
+
+(define-public (make-strbuf)
+ (make-strbuf% 0 (make-u8vector #x1000)))
+
+(define (strbuf-realloc! strbuf)
+ (let* ((len (u8vector-length (get-bytes strbuf)))
+ (nv (make-u8vector (ash len 1))))
+ (bytevector-copy! (get-bytes strbuf) 0
+ nv 0 len)
+ (set-bytes! strbuf nv)))
+
+;; TODO charset
+(define*-public (strbuf->string strbuf #:optional
+ (transcoder (native-transcoder)))
+ (let ((bv (make-u8vector (get-length strbuf))))
+ (bytevector-copy! (get-bytes strbuf) 0
+ bv 0
+ (get-length strbuf))
+ (bytevector->string bv transcoder)))
+
+(define-public (strbuf-reset! strbuf)
+ (set-length! strbuf 0))
+
+(define-public (strbuf-append! strbuf u8)
+ (catch 'out-of-range
+ (lambda ()
+ (u8vector-set! (get-bytes strbuf)
+ (get-length strbuf)
+ u8))
+ (lambda (err . args)
+ (strbuf-realloc! strbuf)
+ (strbuf-append! strbuf u8)))
+ (set-length! strbuf (1+ (get-length strbuf))))
+