| 1 |
;;; |
| 2 |
;;; SRFI-207 - String-notated bytevectors |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2023-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module srfi.207 |
| 35 |
(use gauche.generator) |
| 36 |
(use gauche.lazy) |
| 37 |
(use gauche.unicode) |
| 38 |
(use gauche.uvector) |
| 39 |
(use rfc.base64) |
| 40 |
(use srfi.42) |
| 41 |
(use srfi.175) |
| 42 |
(use util.match) |
| 43 |
(export bytestring make-bytestring make-bytestring! |
| 44 |
bytevector->hex-string hex-string->bytevector |
| 45 |
bytevector->base64 base64->bytevector |
| 46 |
bytestring->list make-bytestring-generator |
| 47 |
|
| 48 |
bytestring-pad bytestring-pad-right |
| 49 |
bytestring-trim bytestring-trim-right bytestring-trim-both |
| 50 |
bytestring-replace |
| 51 |
bytestring<? bytestring>? bytestring<=? bytestring>=? |
| 52 |
|
| 53 |
bytestring-index bytestring-index-right |
| 54 |
bytestring-break bytestring-span |
| 55 |
bytestring-join bytestring-split |
| 56 |
|
| 57 |
read-textual-bytestring |
| 58 |
write-textual-bytestring |
| 59 |
write-binary-bytestring |
| 60 |
|
| 61 |
bytestring-error?) |
| 62 |
) |
| 63 |
(select-module srfi.207) |
| 64 |
|
| 65 |
(define-condition-type <bytestring-error> <error> bytestring-error?) |
| 66 |
|
| 67 |
;; TODO: We create intermediate u8vectors to concatenate. It is debatable |
| 68 |
;; whether filling individual elements may be faster. U8vector-concatenate |
| 69 |
;; uses block copy written in C, so it may compensate the overhead of |
| 70 |
;; u8vector allocations. |
| 71 |
|
| 72 |
(define (x->u8vector obj) |
| 73 |
(cond [(u8vector? obj) obj] |
| 74 |
[(number? obj) |
| 75 |
(if (and (exact-integer? obj) (<= 0 obj 255)) |
| 76 |
(u8vector obj) |
| 77 |
(error <bytestring-error> |
| 78 |
"Out of range number for an element of bytestring:" obj))] |
| 79 |
[(char? obj) |
| 80 |
(let1 c (char->integer obj) |
| 81 |
(if (<= 0 c 127) |
| 82 |
(u8vector c) |
| 83 |
(error <bytestring-error> |
| 84 |
"Out of range character for an element of bytestring:" obj)))] |
| 85 |
[(string? obj) |
| 86 |
(unless (ascii-string? obj) |
| 87 |
(error <bytestring-error> |
| 88 |
"Only ASCII string is allowed for bytestring:" obj)) |
| 89 |
|
| 90 |
(string->u8vector obj 0 -1 #t) ; we can avoid copying |
| 91 |
] |
| 92 |
[else |
| 93 |
(error <bytestring-error> |
| 94 |
"Ivalid object to construct a bytestring:" obj)])) |
| 95 |
|
| 96 |
(define (bytestring . args) (make-bytestring args)) |
| 97 |
|
| 98 |
(define (make-bytestring lis) |
| 99 |
(u8vector-concatenate (map x->u8vector lis))) |
| 100 |
|
| 101 |
(define (make-bytestring! bv start lis) |
| 102 |
(assume-type bv <u8vector>) |
| 103 |
;; TODO: This may be added to gauche.uvector |
| 104 |
(let1 uvs (map x->u8vector lis) |
| 105 |
(assume (<= (+ start (fold (^[uv sum] (+ sum (u8vector-length uv))) 0 uvs)) |
| 106 |
(u8vector-length bv)) |
| 107 |
"Destination bytevector overflow" bv) |
| 108 |
(let loop ([uvs uvs] [start start]) |
| 109 |
(match uvs |
| 110 |
[() (undefined)] |
| 111 |
[(uv . uvs) |
| 112 |
(u8vector-copy! bv start uv) |
| 113 |
(loop uvs (+ start (u8vector-length uv)))])))) |
| 114 |
|
| 115 |
(define (bytevector->hex-string bv) |
| 116 |
(assume-type bv <u8vector>) |
| 117 |
(with-output-to-string |
| 118 |
(^[] (u8vector-for-each (^b (format #t "~2,'0x" b)) bv)))) |
| 119 |
|
| 120 |
(define (hex-string->bytevector str) |
| 121 |
(assume-type str <string>) |
| 122 |
(let1 slen (string-length str) |
| 123 |
(assume (even? slen) <bytestring-error> |
| 124 |
"Hex string must have an even length:" str) |
| 125 |
(rlet1 bv (make-u8vector (ash slen -1)) |
| 126 |
(let1 in (open-input-string str) |
| 127 |
(let loop ([i 0]) |
| 128 |
(let* ([a (read-char in)] |
| 129 |
[b (read-char in)]) |
| 130 |
(unless (eof-object? a) |
| 131 |
(let ([aa (digit->integer a 16)] |
| 132 |
[bb (digit->integer b 16)]) |
| 133 |
(assume aa <bytestring-error> "Invalid hexdigit char:" a) |
| 134 |
(assume bb <bytestring-error> "Invalid hexdigit char:" b) |
| 135 |
(u8vector-set! bv i (+ (* aa 16) bb)) |
| 136 |
(loop (+ i 1)))))))))) |
| 137 |
|
| 138 |
(define (bytevector->base64 bv :optional (digits #f)) |
| 139 |
(assume-type bv <u8vector>) |
| 140 |
(assume-type digits (<?> <string>)) |
| 141 |
(base64-encode-bytevector bv :line-width #f :digits digits)) |
| 142 |
|
| 143 |
(define (base64->bytevector string :optional (digits #f)) |
| 144 |
(assume-type string <string>) |
| 145 |
(assume-type digits (<?> <string>)) |
| 146 |
(guard (e [else (raise (make-condition <bytestring-error> |
| 147 |
'message (~ e'message)))]) |
| 148 |
(base64-decode-bytevector string :digits digits :strict #t))) |
| 149 |
|
| 150 |
(define (%byte->elt b) |
| 151 |
(if (<= 32 b 127) |
| 152 |
(integer->char b) |
| 153 |
b)) |
| 154 |
|
| 155 |
(define (%elt->byte e) |
| 156 |
(cond [(char? e) |
| 157 |
(let1 b (char->integer e) |
| 158 |
(unless (<= 32 b 127) |
| 159 |
(error "Invalid char for bytestring:" e)) |
| 160 |
b)] |
| 161 |
[(exact-integer? e) |
| 162 |
(unless (<= 32 e 127) |
| 163 |
(error "Integer out of range for bytestring:" e)) |
| 164 |
e] |
| 165 |
[else (error "Invalid element for bytestring:" e)])) |
| 166 |
|
| 167 |
(define (bytestring->list bv :optional (start 0) (end -1)) |
| 168 |
(assume-type bv <u8vector>) |
| 169 |
(let ([end (if (and end (>= end 0)) end (u8vector-length bv))]) |
| 170 |
(assume (<= 0 start end (u8vector-length bv))) |
| 171 |
(list-ec (: i start end) |
| 172 |
(%byte->elt (u8vector-ref bv i))))) |
| 173 |
|
| 174 |
(define (make-bytestring-generator . objs) |
| 175 |
;; We must check all args before start generating bytes |
| 176 |
(define gens (map ($ uvector->generator $ x->u8vector $) objs)) |
| 177 |
(rec (gen) |
| 178 |
(if (null? gens) |
| 179 |
(eof-object) |
| 180 |
(let1 v ((car gens)) |
| 181 |
(if (eof-object? v) |
| 182 |
(begin (pop! gens) (gen)) |
| 183 |
v))))) |
| 184 |
|
| 185 |
(define (%bytestring-pad bv len elt where) |
| 186 |
(assume-type bv <u8vector>) |
| 187 |
(let1 origlen (u8vector-length bv) |
| 188 |
(if (>= origlen len) |
| 189 |
(u8vector-copy bv) ;; required to allocate |
| 190 |
(rlet1 rv (make-u8vector len (%elt->byte elt)) |
| 191 |
(case where |
| 192 |
[(left) (u8vector-copy! rv (- len origlen) bv)] |
| 193 |
[(right) (u8vector-copy! rv 0 bv)]))))) |
| 194 |
|
| 195 |
(define (bytestring-pad bv len elt) (%bytestring-pad bv len elt 'left)) |
| 196 |
(define (bytestring-pad-right bv len elt) (%bytestring-pad bv len elt 'right)) |
| 197 |
|
| 198 |
(define (bytestring-trim bv pred) |
| 199 |
(define len (u8vector-length bv)) |
| 200 |
(let loop ([i 0]) |
| 201 |
(cond [(= i len) (u8vector)] |
| 202 |
[(pred (u8vector-ref bv i)) (loop (+ i 1))] |
| 203 |
[else (u8vector-copy bv i len)]))) |
| 204 |
|
| 205 |
(define (bytestring-trim-right bv pred) |
| 206 |
(define len (u8vector-length bv)) |
| 207 |
(let loop ([i (- len 1)]) |
| 208 |
(cond [(< i 0) (u8vector)] |
| 209 |
[(pred (u8vector-ref bv i)) (loop (- i 1))] |
| 210 |
[else (u8vector-copy bv 0 (+ i 1))]))) |
| 211 |
|
| 212 |
(define (bytestring-trim-both bv pred) |
| 213 |
(define len (u8vector-length bv)) |
| 214 |
(let leading ([i 0]) |
| 215 |
(cond [(= i len) (u8vector)] |
| 216 |
[(pred (u8vector-ref bv i)) (leading (+ i 1))] |
| 217 |
[else |
| 218 |
(let trailing ([j (- len 1)]) |
| 219 |
(if (pred (u8vector-ref bv j)) |
| 220 |
(trailing (- j 1)) |
| 221 |
(u8vector-copy bv i (+ j 1))))]))) |
| 222 |
|
| 223 |
(define (bytestring-replace bv1 bv2 start1 end1 :optional (start2 0) (end2 #f)) |
| 224 |
(assume-type bv1 <u8vector>) |
| 225 |
(assume-type bv2 <u8vector>) |
| 226 |
(u8vector-append-subvectors bv1 0 start1 |
| 227 |
bv2 start2 (or end2 (u8vector-length bv2)) |
| 228 |
bv1 end1 (u8vector-length bv1))) |
| 229 |
|
| 230 |
(define (%bytestring-compare bv1 bv2) |
| 231 |
(assume-type bv1 <u8vector>) |
| 232 |
(assume-type bv2 <u8vector>) |
| 233 |
(let ([len1 (u8vector-length bv1)] |
| 234 |
[len2 (u8vector-length bv2)]) |
| 235 |
(let loop ([k 0]) |
| 236 |
(cond [(= k len1) (if (= k len2) 0 -1)] |
| 237 |
[(= k len2) 1] |
| 238 |
[else (let ([b1 (u8vector-ref bv1 k)] |
| 239 |
[b2 (u8vector-ref bv2 k)]) |
| 240 |
(cond [(< b1 b2) -1] |
| 241 |
[(> b1 b2) 1] |
| 242 |
[else (loop (+ k 1))]))])))) |
| 243 |
|
| 244 |
(define (bytestring<? bv1 bv2) (< (%bytestring-compare bv1 bv2) 0)) |
| 245 |
(define (bytestring>? bv1 bv2) (> (%bytestring-compare bv1 bv2) 0)) |
| 246 |
(define (bytestring<=? bv1 bv2) (<= (%bytestring-compare bv1 bv2) 0)) |
| 247 |
(define (bytestring>=? bv1 bv2) (>= (%bytestring-compare bv1 bv2) 0)) |
| 248 |
|
| 249 |
;; These differ from u8vector-index/u8vector-index-right, so we |
| 250 |
;; scan by our own. |
| 251 |
(define (bytestring-index bv pred :optional (start 0) (end #f)) |
| 252 |
(assume-type bv <u8vector>) |
| 253 |
(let1 end (or end (u8vector-length bv)) |
| 254 |
(let loop ([i start]) |
| 255 |
(and (< i end) |
| 256 |
(if (pred (u8vector-ref bv i)) |
| 257 |
i |
| 258 |
(loop (+ i 1))))))) |
| 259 |
|
| 260 |
(define (bytestring-index-right bv pred :optional (start 0) (end #f)) |
| 261 |
(assume-type bv <u8vector>) |
| 262 |
(let1 end (or end (u8vector-length bv)) |
| 263 |
(let loop ([i (- end 1)]) |
| 264 |
(and (>= i start) |
| 265 |
(if (pred (u8vector-ref bv i)) |
| 266 |
i |
| 267 |
(loop (- i 1))))))) |
| 268 |
|
| 269 |
(define (bytestring-break bv pred) |
| 270 |
(assume-type bv <u8vector>) |
| 271 |
(if-let1 i (bytestring-index bv pred) |
| 272 |
(values (u8vector-copy bv 0 i) |
| 273 |
(u8vector-copy bv i)) |
| 274 |
(values bv (u8vector)))) |
| 275 |
|
| 276 |
(define (bytestring-span bv pred) |
| 277 |
(assume-type bv <u8vector>) |
| 278 |
(if-let1 i (bytestring-index bv (complement pred)) |
| 279 |
(values (u8vector-copy bv 0 i) |
| 280 |
(u8vector-copy bv i)) |
| 281 |
(values bv (u8vector)))) |
| 282 |
|
| 283 |
(define (bytestring-join bvs delim :optional (grammar 'infix)) |
| 284 |
(case grammar |
| 285 |
[(infix) (u8vector-concatenate (intersperse (x->u8vector delim) bvs))] |
| 286 |
[(strict-infix) |
| 287 |
(if (null? bvs) |
| 288 |
(error <bytestring-error> |
| 289 |
"Zero bytevectors cannot be joined with strict-infix grammar.") |
| 290 |
(bytestring-join bvs delim 'infix))] |
| 291 |
[(suffix) (u8vector-concatenate |
| 292 |
(append-map (cute list <> (x->u8vector delim)) bvs))] |
| 293 |
[(prefix) (u8vector-concatenate |
| 294 |
(append-map (cute list (x->u8vector delim) <>) bvs))] |
| 295 |
[else (error <bytestring-error> |
| 296 |
"Invalid grammar argument:" grammar)])) |
| 297 |
|
| 298 |
(define (bytestring-split bv delim :optional (grammar 'infix)) |
| 299 |
(define dbyte |
| 300 |
(cond [(and (char? delim) (#[\x00-\x7f] delim)) (char->integer delim)] |
| 301 |
[(and (exact-integer? delim) (<= 0 delim 255)) delim] |
| 302 |
[else (error "Delimiter out of domain:" delim)])) |
| 303 |
(assume-type bv <u8vector>) |
| 304 |
(let1 end (u8vector-length bv) |
| 305 |
(let loop ([i 0] [s 0] [r '()]) |
| 306 |
(cond [(= i end) |
| 307 |
(let1 r (cons (u8vector-copy bv s i) r) |
| 308 |
(case grammar |
| 309 |
[(infix strict-infix) (if (equal? r '(#u8())) '() (reverse r))] |
| 310 |
[(prefix) (let1 rr (reverse r) |
| 311 |
(if (and (pair? rr) (equal? #u8() (car rr))) |
| 312 |
(cdr rr) |
| 313 |
rr))] |
| 314 |
[(suffix) (if (and (pair? r) (equal? #u8() (car r))) |
| 315 |
(reverse (cdr r)) |
| 316 |
(reverse r))] |
| 317 |
[else (error <bytestring-error> |
| 318 |
"Invalid grammar argument:" grammar)]))] |
| 319 |
[(= (u8vector-ref bv i) dbyte) |
| 320 |
(loop (+ i 1) (+ i 1) (cons (u8vector-copy bv s i) r))] |
| 321 |
[else |
| 322 |
(loop (+ i 1) s r)])))) |
| 323 |
|
| 324 |
(define (read-textual-bytestring prefix :optional (port (current-input-port))) |
| 325 |
(define (err msg . args) (apply error <bytestring-error> msg args)) |
| 326 |
(define (ensure char expected) |
| 327 |
(unless (eqv? char expected) |
| 328 |
(err (format "Bad bytestring sytnax. '~s' expected, but got:" expected) |
| 329 |
char))) |
| 330 |
(define (premature) |
| 331 |
(error <bytestring-error> "Premature end of bytestring literal")) |
| 332 |
(define (badhex c) |
| 333 |
(error <bytestring-error> |
| 334 |
"Invalid hexadecimal digit in bytestring literal:" c)) |
| 335 |
(define (skip-ws next bytes newline-seen?) |
| 336 |
(let1 ch (read-char port) |
| 337 |
(cond [(eof-object? ch) (premature)] |
| 338 |
[(eqv? ch #\newline) (skip-ws next bytes #t)] |
| 339 |
[(char-whitespace? ch) (skip-ws next bytes newline-seen?)] |
| 340 |
[else (if newline-seen? |
| 341 |
(next ch bytes) |
| 342 |
(err "Invalid \\+newline sequence"))]))) |
| 343 |
(when prefix |
| 344 |
(ensure (read-char port) #\#) |
| 345 |
(ensure (read-char port) #\u) |
| 346 |
(ensure (read-char port) #\8)) |
| 347 |
(ensure (read-char port) #\") |
| 348 |
(let loop ((ch (read-char port)) |
| 349 |
(bytes '())) |
| 350 |
(when (eof-object? ch) (premature)) |
| 351 |
(cond [(eqv? ch #\") (list->u8vector (reverse bytes))] |
| 352 |
[(eqv? ch #\\) |
| 353 |
(let1 ch2 (read-char port) |
| 354 |
(case ch2 |
| 355 |
[(#\a) (loop (read-char port) (cons 7 bytes))] |
| 356 |
[(#\b) (loop (read-char port) (cons 8 bytes))] |
| 357 |
[(#\t) (loop (read-char port) (cons 9 bytes))] |
| 358 |
[(#\n) (loop (read-char port) (cons 10 bytes))] |
| 359 |
[(#\r) (loop (read-char port) (cons 13 bytes))] |
| 360 |
[(#\|) (loop (read-char port) (cons 124 bytes))] |
| 361 |
[(#\" #\\) |
| 362 |
(loop (read-char port) (cons (char->integer ch2) bytes))] |
| 363 |
[(#\x) |
| 364 |
(let* ([x0 (read-char port)] |
| 365 |
[x1 (read-char port)]) |
| 366 |
(unless (and (char? x0) (char? x1)) (premature)) |
| 367 |
(ensure (read-char port) #\;) |
| 368 |
(let ([d0 (digit->integer x0 16)] |
| 369 |
[d1 (digit->integer x1 16)]) |
| 370 |
(unless d0 (badhex x0)) |
| 371 |
(unless d1 (badhex x1)) |
| 372 |
(loop (read-char port) (cons (+ (* d0 16) d1) bytes))))] |
| 373 |
[(#\tab #\space) (skip-ws loop bytes #f)] |
| 374 |
[(#\newline) (skip-ws loop bytes #t)] |
| 375 |
[else |
| 376 |
(err "Invalid escape sequence in bytestring literal:" ch2)]))] |
| 377 |
[(ascii-char? ch) |
| 378 |
(loop (read-char port) (cons (char->integer ch) bytes))] |
| 379 |
[else (err "Non-ascii character in bytestring literal:" ch)]))) |
| 380 |
|
| 381 |
(define (write-textual-bytestring bv :optional (port (current-output-port))) |
| 382 |
(assume-type bv <u8vector>) |
| 383 |
(display "#u8\"" port) |
| 384 |
(do-ec (: byte bv) |
| 385 |
(cond |
| 386 |
[(eq? byte (char->integer #\")) (display "\\\"" port)] |
| 387 |
[(eq? byte (char->integer #\\)) (display "\\\\" port)] |
| 388 |
[(eq? byte #x07) (display "\\a" port)] |
| 389 |
[(eq? byte #x08) (display "\\b" port)] |
| 390 |
[(eq? byte #x09) (display "\\t" port)] |
| 391 |
[(eq? byte #x0a) (display "\\n" port)] |
| 392 |
[(eq? byte #x0d) (display "\\r" port)] |
| 393 |
[(eq? byte #x7c) (display "\\|" port)] |
| 394 |
[(<= #x20 byte #x7e) (display (integer->char byte) port)] |
| 395 |
[else |
| 396 |
;;(format port "\\x~2,'0x;" byte) |
| 397 |
(display "\\x" port) |
| 398 |
(display (integer->digit (ash byte -4) 16) port) |
| 399 |
(display (integer->digit (logand byte #x0f) 16) port) |
| 400 |
(display ";" port)])) |
| 401 |
(display "\"" port)) |
| 402 |
|
| 403 |
(define (write-binary-bytestring port . args) |
| 404 |
;; TODO: We can avoid intermediate u8vectors. |
| 405 |
(for-each (^x (write-uvector (x->u8vector x) port)) args)) |