-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutf8-input-stream.lisp
184 lines (154 loc) · 5.78 KB
/
utf8-input-stream.lisp
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
(defpackage #:utf8-input-stream
(:use #:cl #:trivial-gray-streams)
(:export #:make-utf8-input-stream #:*line-buffer-size* #:*line-tmp-size*))
(in-package :utf8-input-stream)
(defparameter *line-buffer-size* #x10000)
(defparameter *line-tmp-size* #x10)
(defconstant +max-bytes-in-ch+ 4)
(defstruct stream-context
binary-input-stream
(pos 0 :type integer)
(buf-pos 0 :type integer)
buf
(buf-len 0 :type integer))
(defclass character-input-stream (fundamental-character-input-stream)
((ctx :accessor ctx)))
(defun read-from-stream (ctx)
(setf (stream-context-buf-len ctx)
(read-sequence (stream-context-buf ctx)
(stream-context-binary-input-stream ctx))))
(defun new-buf ()
(make-array *line-buffer-size* :fill-pointer t
:element-type '(unsigned-byte 8)))
(defun make-utf8-input-stream (binary-input-stream)
(let ((s (make-instance 'character-input-stream))
(ctx (make-stream-context)))
(setf (stream-context-binary-input-stream ctx) binary-input-stream)
(setf (stream-context-pos ctx) 0)
(setf (stream-context-buf-pos ctx) 0)
(setf (stream-context-buf ctx) (new-buf))
(read-from-stream ctx)
(setf (ctx s) ctx)
s))
(defun end-of-stream? (ctx)
(eq 0 (stream-context-buf-len ctx)))
(defun reset-buf-pos (ctx)
(setf (stream-context-buf-pos ctx) 0))
(defmacro buf-is-consumed? (ctx)
`(>= (stream-context-buf-pos ,ctx)
(stream-context-buf-len ,ctx)))
(defmacro refill-buffer (ctx)
`(when (buf-is-consumed? ,ctx)
(read-from-stream ,ctx)
(reset-buf-pos ,ctx)))
(defun make-ch-buf ()
(make-array +max-bytes-in-ch+
:fill-pointer 0 :element-type '(unsigned-byte 8)))
(defun read-byte-from-buf (ctx)
(declare (optimize (speed 3) (debug 0) (safety 0)))
(let ((b (elt (stream-context-buf ctx) (stream-context-buf-pos ctx))))
(incf (stream-context-buf-pos ctx))
(incf (stream-context-pos ctx))
b))
(define-condition character-encoding-error (error)
((pos :initarg :pos :reader pos)))
(defun managed-read-byte (ctx)
(refill-buffer ctx)
(when (end-of-stream? ctx)
(error 'character-encoding-error :pos (stream-context-pos ctx)))
(read-byte-from-buf ctx))
(defmacro one-byte-ch? (b0)
`(eq 0 (mask-field (byte 1 7) ,b0)))
(defmacro two-bytes-ch? (b0)
`(eq #b11000000 (mask-field (byte 3 5) ,b0)))
(defmacro three-bytes-ch? (b0)
`(eq #b11100000 (mask-field (byte 4 4) ,b0)))
(defmacro four-bytes-ch? (b0)
`(eq #b11110000 (mask-field (byte 5 3) ,b0)))
(defun fetch-1-byte-ch (ctx b0)
(declare (ignore ctx))
(declare (optimize (speed 3) (debug 0) (safety 0)))
(code-char b0))
(defun fetch-2-bytes-ch (ctx b0)
(declare (optimize (speed 3) (debug 0) (safety 0)))
(let ((b1 (managed-read-byte ctx)))
(when (not (eq #b10000000 (mask-field (byte 2 6) b1)))
(error 'character-encoding-error :pos (stream-context-pos ctx)))
(code-char (+ (ash (mask-field (byte 5 0) b0) 6)
(mask-field (byte 6 0) b1)))))
(defun fetch-3-bytes-ch (ctx b0)
(declare (optimize (speed 3) (debug 0) (safety 0)))
(let ((b1 (managed-read-byte ctx))
(b2 (managed-read-byte ctx)))
(when (or (not (eq #b10000000 (mask-field (byte 2 6) b1)))
(not (eq #b10000000 (mask-field (byte 2 6) b2))))
(error 'character-encoding-error :pos (stream-context-pos ctx)))
(code-char (+ (mask-field (byte 6 0) b2)
(ash (mask-field (byte 6 0) b1) 6)
(ash (mask-field (byte 4 0) b0) 12)))))
(defun fetch-4-bytes-ch (ctx b0)
(declare (optimize (speed 3) (debug 0) (safety 0)))
(let ((b1 (managed-read-byte ctx))
(b2 (managed-read-byte ctx))
(b3 (managed-read-byte ctx)))
(when (or (not (eq #b10000000 (mask-field (byte 2 6) b1)))
(not (eq #b10000000 (mask-field (byte 2 6) b2)))
(not (eq #b10000000 (mask-field (byte 2 6) b3))))
(error 'character-encoding-error :pos (stream-context-pos ctx)))
(code-char (+ (mask-field (byte 6 0) b3)
(ash (mask-field (byte 6 0) b2) 6)
(ash (mask-field (byte 6 0) b1) 12)
(ash (mask-field (byte 4 0) b0) 18)))))
(declaim (inline utf8-char-code-size))
(defun utf8-char-code-size (code)
(cond
((<= code #x7F) 1)
((<= code #x7FF) 2)
((<= code #xFFFF) 3)
((<= code #x10FFFF) 4)
(t (error "Invalid UTF-8 character"))))
(defun fetch-ch (ctx)
(declare (optimize (speed 3) (debug 0) (safety 0)))
(let ((b0 (read-byte-from-buf ctx)))
(cond
((one-byte-ch? b0) (fetch-1-byte-ch ctx b0))
((two-bytes-ch? b0) (fetch-2-bytes-ch ctx b0))
((three-bytes-ch? b0) (fetch-3-bytes-ch ctx b0))
((four-bytes-ch? b0) (fetch-4-bytes-ch ctx b0))
(t (error 'character-encoding-error :stream-context-pos (pos ctx))))))
(defun read-char-from-buf (ctx)
(declare (optimize (speed 3) (debug 0) (safety 0)))
(loop do
(refill-buffer ctx)
(when (end-of-stream? ctx)
(return :EOF))
(return (fetch-ch ctx))))
(defmethod stream-read-char ((s character-input-stream))
(read-char-from-buf (ctx s)))
(defmethod stream-unread-char ((s character-input-stream) ch)
(decf (stream-context-pos (ctx s)))
(setf (stream-context-buf-pos (ctx s))
(- (stream-context-buf-pos (ctx s))
(utf8-char-code-size (char-code ch)))))
(defun vector-to-string (v)
(let* ((len (length v))
(str (make-string len)))
(loop for i from 0 below len
do (setf (aref str i) (aref v i)))
str))
(defmethod stream-read-line ((s character-input-stream))
(declare (optimize (speed 3) (debug 0) (safety 0)))
(let ((ctx (ctx s))
(ch-vec (make-array *line-tmp-size*
:fill-pointer 0
:element-type 'character
:adjustable t)))
(macrolet ((return-val (last?)
`(return (values (vector-to-string ch-vec)
,last?))))
(loop for ch = (read-char-from-buf ctx)
do (case ch
(:EOF (return-val t))
(#\Newline (return-val nil))
(#\Return nil)
(otherwise (vector-push-extend ch ch-vec)))))))