-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcj-u8vector-util.scm
313 lines (266 loc) · 8.24 KB
/
cj-u8vector-util.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
;;; Copyright 2005-2018 by Christian Jaeger <ch@christianjaeger.ch>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
(require fixnum
cj-math ;; quotient-ceiling
cj-let-named-star ;; let-named*
(test TEST)
cj-warn)
(export u8vector->hex-string
u8vector->hex-string-lc
u8vector->alphanumeric-string
u8vector->integer
u8vector->integer-string
u8vector->string
string->u8vector
string->u8vector0
write-u8vector
read-u8vector
read-u8vector-from-file
#!optional
digit->hexchar)
(include "cj-standarddeclares.scm")
;; ---- hex encoding (16 bits per char) -----
;; use upper case by default, as (number->string n 16) is using.
(def (digit->hexchar@ i hexbasechar)
(if (##fixnum.> i 9)
(##fixnum.->char
(##fixnum.+
(##fixnum.<-char hexbasechar)
(##fixnum.- i 10)))
(##fixnum.->char
(##fixnum.+
(##fixnum.<-char #\0)
i))))
(def (digit->hexchar #(fixnum? i)
#!optional
(hexbasechar #\A))
(digit->hexchar@ i hexbasechar))
(def (u8vector->hex-string #(u8vector? u8vec)
#!optional
(hexbasechar #\A))
(let* ((length/2 (u8vector-length u8vec))
(str (##make-string (* length/2 2))))
(let loop ((pos 0))
(if (< pos length/2)
(let* ((v (u8vector-ref u8vec pos))
(hi (quotient v 16))
(lo (modulo v 16)))
(string-set! str (+ (* pos 2) 0)
(digit->hexchar@ hi hexbasechar))
(string-set! str (+ (* pos 2) 1)
(digit->hexchar@ lo hexbasechar))
(loop (+ pos 1)))
str))))
(def (u8vector->hex-string-lc u8vec)
(u8vector->hex-string u8vec #\a))
;; ---- alphanumeric encoding (32 bits per char) -----
(def cj-u8vector-util:_chars
'#(#\a #\b #\c #\d #\e #\f #\g #\h #\i #\j #\k
#\l #\m #\n #\o #\p #\q #\r #\s #\t #\u
#\v #\w #\x #\y #\z #\0 #\1 #\2 #\3 #\4 #\5))
;; A base 32 encoding (not following any standard)
(def (u8vector->alphanumeric-string #(u8vector? u8vec))
(let* ((src-len (u8vector-length u8vec))
(src-bits (* 8 src-len))
(dst-len (quotient-ceiling (* 8 src-len) 5))
(str (##make-string dst-len)))
(let loop ((src-bitpos 0)
(dst-pos 0))
(if (< src-bitpos src-bits)
(let ((src-bytepos (quotient src-bitpos 8))
(src-subbit (modulo src-bitpos 8)))
(let* ((srcvalue
(+ (u8vector-ref u8vec src-bytepos)
(* 256 (let ((src-bytepos-next (+ src-bytepos 1)))
(if (< src-bytepos-next src-len)
(u8vector-ref u8vec src-bytepos-next)
0)))))
(srcvalue
(bitwise-and (arithmetic-shift srcvalue (- src-subbit))
31)))
(string-set! str dst-pos
(vector-ref cj-u8vector-util:_chars srcvalue))
(loop (+ src-bitpos 5)
(+ dst-pos 1))))
str))))
(TEST
> (u8vector->alphanumeric-string (u8vector))
""
> (u8vector->alphanumeric-string (u8vector 0))
"aa"
> (u8vector->alphanumeric-string (u8vector 0 0))
"aaaa"
> (u8vector->alphanumeric-string (u8vector 0 0 0))
"aaaaa"
> (u8vector->alphanumeric-string (u8vector 0 0 1))
"aaaca"
> (u8vector->alphanumeric-string (u8vector 0 1 1))
"aiaca"
> (u8vector->alphanumeric-string (u8vector 1 1 1))
"biaca")
(def (u8vector->integer #(u8vector? v) #!optional big-endian?)
(let* ((len (u8vector-length v))
(len-1 (- len 1)))
(let lp ((i 0)
(t 0))
(if (>= i len)
t
(lp (+ i 1)
(+ t (arithmetic-shift
(u8vector-ref v i)
(arithmetic-shift
(if big-endian?
(- len-1 i)
i)
3))))))))
(TEST
> (u8vector->integer (u8vector 1 2) #t)
258
> (u8vector->integer (u8vector 1 0) #t)
256
> (u8vector->integer (u8vector 1 0) #f)
1
> (u8vector->integer (u8vector 0 1) #f)
256
> (u8vector->integer (u8vector ))
0
> (u8vector->integer (u8vector 3))
3
> (u8vector->integer (u8vector 3) #t)
3
> (u8vector->integer (u8vector 1 2 3 4 5 6 7 8) #t)
72623859790382856
> (u8vector->integer (u8vector 1 2 3 4 5 6 7 8) #f)
578437695752307201
)
(def (u8vector->integer-string #(u8vector? v) #!optional big-endian?)
;; "cheap" variant in sense of programmer time
(number->string (u8vector->integer v big-endian?)))
;; also see u8vector.string in oo-vector-lib
(def (u8vector->string #(u8vector? v))
(let* ((len (u8vector-length v))
(str (##make-string len)))
(let loop ((i 0))
(if (< i len)
(begin
(string-set! str i (integer->char (u8vector-ref v i)))
(loop (+ i 1)))
str))))
;probably faster, but check first if really correct
; (define u8vector->string
; (lambda(v)
; (if (not (##u8vector? v))
; (error "wrong type"))
; (let* ((len (##u8vector-length v))
; (out (##make-string len)))
; (let loop ((i 0))
; (if (##fixnum.< i len)
; (begin
; (##string-set! out i (##u8vector-ref v i))
; (loop (##fixnum.+ i 1)))
; out)))))
;; there's also a slower definition in oo-vector-lib
(def (string->u8vector #(string? v))
(let* ((len (##string-length v))
(out (##make-u8vector len)))
(let loop ((i 0))
(if (##fixnum.< i len)
(let ((ch (##string-ref v i)))
(or (##char<=? ch (##fixnum.->char 255))
(error
"string->u8vector: can't convert non-ascii char at pos"
i ch))
(##u8vector-set! out i ch)
(loop (##fixnum.+ i 1)))
out))))
;; bad copy paste. but i don't care so much right now.:
(def (string->u8vector0 #(string? v))
(let* ((len (##string-length v))
(out (##make-u8vector (+ len 1))))
(let loop ((i 0))
(if (##fixnum.< i len)
(let ((ch (##string-ref v i)))
(or (##char<=? ch (##fixnum.->char 255))
(error
"string->u8vector: can't convert non-ascii char at pos"
i ch))
(##u8vector-set! out i ch)
(loop (##fixnum.+ i 1)))
(begin
(##u8vector-set! out len 0)
out)))))
(def (write-u8vector #(u8vector? v)
#!optional
(port (current-output-port)))
(let ((len (u8vector-length v)))
(let-named*
loop ((written 0)
(warned #f))
(let ((done (write-subu8vector v written len port)))
(let ((written (+ written done)))
(if (< written len)
(begin
(warn (string-append "write-u8vector warning: not written"
" whole buffer at once, retrying.."))
(loop warned: #t))
(when warned
(warn "write-u8vector note: now buffer fully written."))))))))
(def readbuf-size 4096)
(def (read-u8vector #!optional
(port (current-output-port))
(maxlen #f))
(define (while-not endval in out)
(let loop ()
(let ((i (in)))
(if (eqv? i endval)
i
(begin
;;(warn "while-not onceagain since I got:" i);;;
(out i)
(loop))))))
(define buf (##make-u8vector readbuf-size))
(when maxlen (error "maxlen not yet supported"))
(with-output-to-u8vector
(u8vector)
(lambda ()
(while-not 0
(lambda()
(read-subu8vector buf 0 readbuf-size))
(lambda(n)
(ALL-write-subu8vector buf 0 n))))))
(def (ALL-write-subu8vector uv from to)
(let ((written (write-subu8vector uv from to)))
(or (= written (- to from))
(error "ALL-write-subu8vector: couldn't write it all out at once"
;; why would that be if the output is an u8vector port?
from to written))))
;; This is about 3 times faster than (with-input-from-file path
;; read-u8vector) for an 7.7kb example file.
(def (read-u8vector-from-file path
#!optional
(maxlen #f))
(let ((len (file-size path)))
(let ((v (##make-u8vector len)))
(call-with-input-file path
(lambda(port)
(let ((read (##read-subu8vector v 0 len port)))
(if (##fixnum.= read len)
v
(error (string-append
"read-u8vector-from-file: some error "
"(maybe timeout?) made it not read the whole file:")
path read))))))))
;; report first occurrence of x, if any
;; XX name? also, provide for all collections...
(def. (u8vector.maybe-scan v x #!optional (start 0) (end (u8vector-length v)))
(let lp ((i start))
(and (< i end)
(if (= (u8vector-ref v i) x)
i
(lp (inc i))))))
(TEST
> (map (C .maybe-scan (u8vector 10 30 1) _) '(0 10 30 1))
(#f 0 1 2))