-
Notifications
You must be signed in to change notification settings - Fork 313
/
enc-basic.lisp
1107 lines (1075 loc) · 61.6 KB
/
enc-basic.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; encodings available regardless of build-time unicode settings
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; This software is derived from the CMU CL system, which was
;;;; written at Carnegie Mellon University and released into the
;;;; public domain. The software is in the public domain and is
;;;; provided with absolutely no warranty. See the COPYING and CREDITS
;;;; files for more information.
(in-package "SB-IMPL")
;;; General unibyte support
(defmacro define-unibyte-to-octets-functions
(external-format get-bytes-name string-to-octets-name code->-name)
(let ((get-bytes-name/cr (symbolicate get-bytes-name '/cr))
(string-to-octets-name/cr (symbolicate string-to-octets-name '/cr))
(string-to-octets-name/crlf (symbolicate string-to-octets-name '/crlf)))
`(progn
(declaim (inline ,get-bytes-name ,get-bytes-name/cr))
(defun ,get-bytes-name (string pos replacement)
(declare (optimize speed #.*safety-0*)
(type simple-string string)
(type array-range pos))
(get-latin-bytes #',code->-name ',external-format replacement string pos))
(defun ,string-to-octets-name (string sstart send null-padding replacement)
(declare (optimize speed #.*safety-0*)
(type simple-string string)
(type array-range sstart send))
(values (string->latin% string sstart send #',get-bytes-name null-padding replacement)))
(defun ,get-bytes-name/cr (string pos replacement)
(declare (optimize speed #.*safety-0*)
(type simple-string string)
(type array-range pos))
(get-latin-bytes (lambda (code) (,code->-name (if (= code 10) 13 code)))
'(,external-format :newline :cr) replacement string pos))
(defun ,string-to-octets-name/cr (string sstart send null-padding replacement)
(declare (optimize speed #.*safety-0*)
(type simple-string string)
(type array-range sstart send))
(values (string->latin% string sstart send #',get-bytes-name/cr null-padding replacement)))
(defun ,string-to-octets-name/crlf (string sstart send null-padding replacement)
(declare (optimize speed #.*safety-0*)
(type simple-string string)
(type array-range sstart send)
(type (integer 0 1) null-padding))
(let ((array (make-array (+ (* 2 (- send sstart)) null-padding)
:element-type '(unsigned-byte 8)
:fill-pointer 0 :adjustable t)))
(loop for i from sstart below send
do (let* ((code (char-code (char string i)))
(byte (,code->-name code)))
(cond
((= code 10)
(vector-push-extend (,code->-name 13) array)
(vector-push-extend byte array))
((null byte)
(let ((replacement (encoding-error '(,external-format :newline :crlf)
replacement string i)))
(declare (type (simple-array (unsigned-byte 8) (*)) replacement))
(dotimes (j (length replacement))
(vector-push-extend (aref replacement j) array))))
(t (vector-push-extend byte array)))))
(dotimes (i null-padding)
(vector-push-extend 0 array))
(coerce array '(simple-array (unsigned-byte 8) (*))))))))
(defmacro define-unibyte-to-string-functions
(external-format octets-to-string-name ->code-name &optional invalid-bytes-p)
(let ((octets-to-string-name/cr (symbolicate octets-to-string-name '/cr))
(octets-to-string-name/crlf (symbolicate octets-to-string-name '/crlf))
(decoding-condition-name (symbolicate 'malformed- external-format)))
(declare (ignorable decoding-condition-name))
`(macrolet ((def (accessor type)
(declare (ignorable type))
`(progn
,@,(when invalid-bytes-p
``((define-condition ,',decoding-condition-name (octet-decoding-error) ())))
,,(if invalid-bytes-p
``(progn
(defun ,(make-od-name ',octets-to-string-name accessor) (array astart aend replacement)
(declare (optimize speed)
(type ,type array)
(type array-range astart aend)
(ignorable replacement))
(let ((string (make-array 0 :element-type 'character :fill-pointer 0 :adjustable t)))
(loop for apos from astart below aend
do (let* ((byte (,accessor array apos))
(code (,',->code-name byte))
(string-content
(if code
(code-char code)
(decoding-error array apos (1+ apos)
,',external-format
replacement
',',decoding-condition-name
apos))))
(if (characterp string-content)
(vector-push-extend string-content string)
(loop for c across string-content
do (vector-push-extend c string))))
finally (return (coerce string 'simple-string)))))
(defun ,(make-od-name ',octets-to-string-name/cr accessor) (array astart aend replacement)
(declare (optimize speed)
(type ,type array)
(type array-range astart aend)
(ignorable replacement))
(let ((string (make-array 0 :element-type 'character :fill-pointer 0 :adjustable t)))
(loop for apos from astart below aend
do (let* ((byte (,accessor array apos))
(code (,',->code-name byte))
(string-content
(cond
((null code)
(decoding-error array apos (1+ apos)
'(,',external-format :newline :cr)
replacement
',',decoding-condition-name
apos))
((= code 13) (code-char 10))
(t (code-char code)))))
(if (characterp string-content)
(vector-push-extend string-content string)
(loop for c across string-content
do (vector-push-extend c string))))
finally (return (coerce string 'simple-string))))))
``(progn
(defun ,(make-od-name ',octets-to-string-name accessor) (array astart aend replacement)
(declare (ignore replacement))
(,(make-od-name 'latin->string accessor) array astart aend #',',->code-name))
(defun ,(make-od-name ',octets-to-string-name/cr accessor) (array astart aend replacement)
(declare (ignore replacement))
(,(make-od-name 'latin->string accessor) array astart aend
(lambda (x) (let ((code (,',->code-name x))) (if (= code 13) 10 code)))))))
(defun ,(make-od-name ',octets-to-string-name/crlf accessor) (array astart aend replacement)
(declare (optimize speed)
(type ,type array)
(type array-range astart aend)
(ignorable replacement))
(let ((string (make-array (- aend astart) :element-type 'character
:fill-pointer 0 :adjustable t)))
(loop for apos from astart below aend
do (let* ((byte (,accessor array apos))
(code (,',->code-name byte))
(string-content
(cond
,@',(when invalid-bytes-p
`(((null code)
(decoding-error array apos (1+ apos)
'(,external-format :newline :crlf)
replacement
',decoding-condition-name
apos))))
((= code 13)
(if (= apos (1- aend))
(code-char 13)
(let* ((next-byte (,accessor array (1+ apos)))
(next-code (,',->code-name next-byte)))
(if (= next-code 10)
(progn (incf apos) #\Newline)
(code-char 13)))))
(t (code-char code)))))
(if (characterp string-content)
(vector-push-extend string-content string)
(loop for c across string-content
do (vector-push-extend c string))))
finally (return (coerce string 'simple-string))))))))
(instantiate-octets-definition def))))
(defmacro define-unibyte-external-format-with-newline-variants
(name other-names
(->code-name code->-name)
(->string-name string->name)
(->string/cr-name string/cr->name)
(->string/crlf-name string/crlf->name)
&optional invalid-bytes-p)
`(progn
(define-unibyte-external-format ,name ,other-names
(,code->-name (char-code |ch|))
(let ((byte (,code->-name bits)))
(if byte
(setf (sap-ref-8 sap tail) byte)
(external-format-encoding-error stream bits)))
(let ((code (,->code-name byte)))
,(if invalid-bytes-p
'(if code (code-char code) (return-from decode-break-reason 1))
'(code-char code)))
,->string-name
,string->name)
(define-external-format/variable-width (,name)
t #\? 1
(let* ((newbits (if (= bits 10) 13 bits))
(byte (,code->-name newbits)))
(if byte
(setf (sap-ref-8 sap tail) byte)
(external-format-encoding-error stream bits)))
1
(let ((code (,->code-name byte)))
,(if invalid-bytes-p
`(if code
(if (= code 13) #\Newline (code-char code))
(return-from decode-break-reason 1))
'(if (= code 13) #\Newline (code-char code))))
,->string/cr-name
,string/cr->name
:char-encodable-p (,code->-name (char-code |ch|))
:newline-variant :cr)
(define-external-format/variable-width (,name)
t #\?
(if (char= |ch| #\Newline) 2 1)
(let ((byte (,code->-name bits)))
(cond
((null byte) (external-format-encoding-error stream bits))
((= bits 10)
;; FIXME: if we required that CODE->-NAME was required
;; to be callable at compile-time, then we could remove
;; the LOAD-TIME-VALUEs here and below.
(setf (sap-ref-8 sap tail) (load-time-value (,code->-name 13) t))
(setf (sap-ref-8 sap (1+ tail)) byte))
(t (setf (sap-ref-8 sap tail) byte))))
((2 1)
(cond
((= (- tail head) 1) 1) ; one octet away from EOF, can't possibly be CRLF
((and (= byte (load-time-value (,code->-name 13) t))
(= (sap-ref-8 sap (1+ head)) (load-time-value (,code->-name 10) t)))
2)
(t 1)))
(if (= size 2)
#\Newline
(let ((code (,->code-name byte)))
,(if invalid-bytes-p
'(if code (code-char code) (return-from decode-break-reason 1))
'(code-char code))))
,->string/crlf-name
,string/crlf->name
:char-encodable-p (,code->-name (char-code |ch|))
:newline-variant :crlf)))
;;; ASCII
(declaim (inline code->ascii-mapper))
(defun code->ascii-mapper (code)
(declare (optimize speed #.*safety-0*)
(%char-code code))
(if (> code 127) nil code))
(declaim (inline ascii->code-mapper))
(defun ascii->code-mapper (byte)
(declare (optimize speed #.*safety-0*)
(type (unsigned-byte 8) byte))
(if (> byte 127) nil byte))
(define-unibyte-to-octets-functions :ascii get-ascii-bytes string->ascii code->ascii-mapper)
(define-unibyte-to-string-functions :ascii ascii->string ascii->code-mapper t)
(define-unibyte-external-format-with-newline-variants :ascii
(:us-ascii :ansi_x3.4-1968 :iso-646 :iso-646-us :|646|)
(ascii->code-mapper code->ascii-mapper)
(ascii->string-aref string->ascii)
(ascii->string/cr-aref string->ascii/cr)
(ascii->string/crlf-aref string->ascii/crlf)
t)
;;; Latin-1
;;; FIXME: we need this for the CLRF definition, but having this
;;; probably now implies that the check for (< CODE 256) in
;;; GET-LATIN-BYTES is redundant.
(declaim (inline code->latin1-mapper))
(defun code->latin1-mapper (code)
(declare (optimize speed #.*safety-0*)
(%char-code code))
(and (< code 256) code))
(define-unibyte-to-octets-functions :latin-1 get-latin1-bytes string->latin1 code->latin1-mapper)
(define-unibyte-to-string-functions :latin-1 latin1->string identity)
(define-unibyte-external-format-with-newline-variants :latin-1 (:latin1 :iso-8859-1 :iso8859-1)
(identity code->latin1-mapper)
(latin1->string-aref string->latin1)
(latin1->string/cr-aref string->latin1/cr)
(latin1->string/crlf-aref string->latin1/crlf))
;;; UTF-8
;;; to UTF-8
(declaim (inline char-len-as-utf8))
(defun char-len-as-utf8 (code)
(declare (optimize speed #.*safety-0*)
(type (integer 0 (#.char-code-limit)) code))
(cond ((< code 0) (unreachable))
((< code #x80) 1)
((< code #x800) 2)
((< code #x10000) 3)
((< code #x110000) 4)
(t (unreachable))))
(macrolet ((ascii-bash (newline &optional (bytes nil bytesp))
(declare (type (member :lf :cr :crlf) newline))
(case newline
((:lf :cr)
;; KLUDGE: this depends on the fact that we know that
;; our arrays are initialized with zeros.
`(let ((array (make-array (+ null-padding ,(if bytesp bytes '(- send sstart)))
:element-type '(unsigned-byte 8))))
(loop for i from 0
and j from sstart below send
for code = (char-code (char string j))
do (setf (aref array i) ,(if (eql newline :cr) '(if (= code 10) 13 code) 'code)))
array))
(:crlf
`(let* ((utf8-length ,(if bytesp bytes '(+ (- send sstart) (loop for j from sstart below send count (char= (char string j) #\Newline)))))
(array (make-array (+ null-padding utf8-length)
:element-type '(unsigned-byte 8))))
(loop for i from 0
and j from sstart below send
for code = (char-code (char string j))
do (setf (aref array i) (if (= code 10) 13 code))
if (= code 10) do (setf (aref array (incf i)) 10))
array))))
(output-code (tag newline)
(declare (type (member :lf :cr :crlf) newline))
`(case (char-len-as-utf8 code)
(1 ,@(case newline
(:lf '((add-byte code)))
(:cr '((add-byte (if (= code 10) 13 code))))
(:crlf '((add-byte (if (= code 10) 13 code))
(when (= code 10) (add-byte 10))))))
(2 (add-byte (logior #xc0 (ldb (byte 5 6) code)))
(add-byte (logior #x80 (ldb (byte 6 0) code))))
(3 (when (<= #xd800 code #xdfff)
(setf error-position i)
(go ,tag))
(add-byte (logior #xe0 (ldb (byte 4 12) code)))
(add-byte (logior #x80 (ldb (byte 6 6) code)))
(add-byte (logior #x80 (ldb (byte 6 0) code))))
(4 (add-byte (logior #xf0 (ldb (byte 3 18) code)))
(add-byte (logior #x80 (ldb (byte 6 12) code)))
(add-byte (logior #x80 (ldb (byte 6 6) code)))
(add-byte (logior #x80 (ldb (byte 6 0) code))))))
(def (name newline)
`(defun ,name (string sstart send null-padding replacement)
(declare (optimize (speed 3) #.*safety-0*)
(type simple-string string)
(type (integer 0 1) null-padding)
(type array-range sstart send))
(etypecase string
((simple-array character (*))
(let ((utf8-length 0)
,@(when (eql newline :crlf) '((newline-count 0))))
;; Since it has to fit in a vector, it must be a fixnum!
(declare (type (and unsigned-byte fixnum) utf8-length))
(loop for i of-type index from sstart below send
for code = (char-code (char string i))
do (incf utf8-length (char-len-as-utf8 code))
,@(when (eql newline :crlf) '(if (= code 10) do (incf newline-count))))
(if (= utf8-length (- send sstart))
(ascii-bash ,newline ,(if (eql newline :crlf) '(+ newline-count utf8-length) 'utf8-length))
(let ((array (make-array (+ null-padding utf8-length ,@(when (eql newline :crlf) '(newline-count)))
:element-type '(unsigned-byte 8)))
(new-array nil)
(error-position 0)
(index 0))
(declare (type index index))
(tagbody
:no-error
(flet ((add-byte (b)
(setf (aref array index) b)
(incf index)))
(declare (inline add-byte))
(loop for i of-type index from sstart below send
for code = (char-code (char string i))
do (output-code :first-error ,newline)
finally (return-from ,name array)))
:first-error
(setf new-array (make-array (* index 2) :adjustable t
:element-type '(unsigned-byte 8)
:fill-pointer index))
(replace new-array array)
:error
(let ((replacement (encoding-error
,(if (eql newline :lf)
:utf-8
`'(:utf-8 :newline ,newline))
replacement string index)))
(flet ((add-byte (b) (vector-push-extend b new-array)))
(dotimes (i (length replacement))
(add-byte (aref replacement i)))
(loop for i of-type index from (1+ error-position) below send
for code = (char-code (char string i))
do (output-code :error ,newline)
finally (return-from ,name
(progn
(unless (zerop null-padding)
(vector-push-extend 0 new-array))
(copy-seq new-array)))))))))))
#+sb-unicode
((simple-array base-char (*))
;; On unicode builds BASE-STRINGs are limited to ASCII range,
;; so we can take a fast path -- and get benefit of the element
;; type information. On non-unicode build BASE-CHAR ==
;; CHARACTER, handled above.
(ascii-bash ,newline))))))
(def string->utf8 :lf)
(def string/cr->utf8 :cr)
(def string/crlf->utf8 :crlf))
;;; from UTF-8
(macrolet ((def (crlfp newline definer-name base-name)
`(progn
(defmacro ,definer-name (accessor type)
(let ((name (make-od-name ',base-name accessor)))
`(progn
;;(declaim (inline ,name))
(let ((lexically-max
(string->utf8 (string (code-char ,(1- char-code-limit)))
0 1 0 nil)))
(declare (type (simple-array (unsigned-byte 8) (#+sb-unicode 4 #-sb-unicode 2)) lexically-max))
(defun ,name (array pos end replacement)
(declare (optimize speed #.*safety-0*)
(type ,type array)
(type array-range pos end))
;; returns the number of bytes consumed and nil if it's a
;; valid character or the number of bytes consumed and a
;; replacement string if it's not.
(let ((initial-byte (,accessor array pos))
(reject-reason nil)
(reject-position pos)
(remaining-bytes (- end pos)))
(declare (type array-range reject-position remaining-bytes))
(labels ((valid-utf8-starter-byte-p (b)
(declare (type (unsigned-byte 8) b))
(let ((ok (cond
((zerop (logand b #b10000000)) 1)
((and (= (logand b #b11100000) #b11000000)
(>= b #xc2)) 2)
((= (logand b #b11110000) #b11100000) 3)
((and (= (logand b #b11111000) #b11110000)
(<= b #xf4)) 4)
(t nil))))
(unless ok
(setf reject-reason 'invalid-utf8-starter-byte))
ok))
(enough-bytes-left-p (x)
(let ((ok (> end (+ pos (1- x)))))
(unless ok
(setf reject-reason 'end-of-input-in-character))
ok))
(valid-secondary-p (x)
(let* ((idx (the array-range (+ pos x)))
(b (,accessor array idx))
(ok (= (logand b #b11000000) #b10000000)))
(when (and ok (= x 1))
(setf ok
(case initial-byte
(#xe0 (>= b #xa0))
(#xed (< b #xa0))
(#xf0 (>= b #x90))
(#xf4 (< b #x90))
(t t))))
(unless ok
(setf reject-reason 'invalid-utf8-continuation-byte)
(setf reject-position idx))
ok))
(preliminary-ok-for-length (maybe-len len)
(and (eql maybe-len len)
;; Has to be done in this order so that
;; certain broken sequences (e.g., the
;; two-byte sequence `"initial (length 3)"
;; "non-continuation"' -- `#xef #x32')
;; signal only part of that sequence as
;; erroneous.
(loop for i from 1 below (min len remaining-bytes)
always (valid-secondary-p i))
(enough-bytes-left-p len)))
(character-below-char-code-limit-p ()
;; This is only called on a four-byte sequence
;; (two in non-unicode builds) to ensure we
;; don't go over SBCL's character limts.
(let ((ok (cond ((< (aref lexically-max 0) (,accessor array pos))
nil)
((> (aref lexically-max 0) (,accessor array pos))
t)
((< (aref lexically-max 1) (,accessor array (+ pos 1)))
nil)
#+sb-unicode
((> (aref lexically-max 1) (,accessor array (+ pos 1)))
t)
#+sb-unicode
((< (aref lexically-max 2) (,accessor array (+ pos 2)))
nil)
#+sb-unicode
((> (aref lexically-max 2) (,accessor array (+ pos 2)))
t)
#+sb-unicode
((< (aref lexically-max 3) (,accessor array (+ pos 3)))
nil)
(t t))))
(unless ok
(setf reject-reason 'character-out-of-range))
ok)))
(declare (inline valid-utf8-starter-byte-p
enough-bytes-left-p
valid-secondary-p
preliminary-ok-for-length))
(let ((maybe-len (valid-utf8-starter-byte-p initial-byte)))
(cond
,@,(when crlfp
``(((= initial-byte 13)
(values
(if (and (> remaining-bytes 1)
(= (,accessor array (+ pos 1)) 10))
2
1)
nil))))
((eql maybe-len 1)
(values 1 nil))
((and (preliminary-ok-for-length maybe-len 2)
#-sb-unicode (character-below-char-code-limit-p))
(values 2 nil))
((and (preliminary-ok-for-length maybe-len 3)
#-sb-unicode (not (setf reject-reason 'character-out-of-range)))
(values 3 nil))
((and (preliminary-ok-for-length maybe-len 4)
#-sb-unicode (not (setf reject-reason 'character-out-of-range))
(character-below-char-code-limit-p))
(values 4 nil))
(t
(let* ((bad-end
(ecase reject-reason
(invalid-utf8-starter-byte (1+ pos))
(end-of-input-in-character end)
(invalid-utf8-continuation-byte reject-position)
(character-out-of-range (+ pos maybe-len))))
(bad-len (- bad-end pos)))
(declare (type array-range bad-end bad-len))
(let ((replacement (decoding-error array pos bad-end
,,(if newline ``'(:utf-8 :newline ,,newline) '':utf-8)
replacement reject-reason reject-position)))
(values bad-len replacement)))))))))))))
(instantiate-octets-definition ,definer-name))))
(def nil nil define-bytes-per-utf8-character bytes-per-utf8-character)
(def nil :cr define-bytes-per-utf8-character/clr bytes-per-utf8-character/cr)
(def t :crlf define-bytes-per-utf8-character/crlf bytes-per-utf8-character/crlf))
(macrolet ((def (newline definer-name base-name)
`(progn
(defmacro ,definer-name (accessor type)
(let ((name (make-od-name ',base-name accessor)))
`(progn
(declaim (inline ,name))
(defun ,name (array pos bytes)
(declare (optimize speed #.*safety-0*)
(type ,type array)
(type array-range pos)
(type (integer 1 4) bytes))
(flet ((cref (x)
(,accessor array (the array-range (+ pos x)))))
(declare (inline cref))
(code-char (ecase bytes
(1 ,(if (eql ,newline :cr)
'(let ((code (cref 0)))
(if (= code 13) 10 code))
'(cref 0)))
(2 ,(if (eql ,newline :crlf)
'(let ((code (cref 0)))
(if (= code 13)
10
(logior (ash (ldb (byte 5 0) (cref 0)) 6)
(ldb (byte 6 0) (cref 1)))))
'(logior (ash (ldb (byte 5 0) (cref 0)) 6)
(ldb (byte 6 0) (cref 1)))))
(3 (logior (ash (ldb (byte 4 0) (cref 0)) 12)
(ash (ldb (byte 6 0) (cref 1)) 6)
(ldb (byte 6 0) (cref 2))))
(4 (logior (ash (ldb (byte 3 0) (cref 0)) 18)
(ash (ldb (byte 6 0) (cref 1)) 12)
(ash (ldb (byte 6 0) (cref 2)) 6)
(ldb (byte 6 0) (cref 3)))))))))))
(instantiate-octets-definition ,definer-name))))
(def :lf define-simple-get-utf8-char simple-get-utf8-char)
(def :cr define-simple-get-utf8-char/cr simple-get-utf8-char/cr)
(def :crlf define-simple-get-utf8-char/crlf simple-get-utf8-char/crlf))
(macrolet ((def (newline definer-name base-name bytes-per-base-name getter-base-name)
(declare (ignore newline))
`(progn
(defmacro ,definer-name (accessor type)
(let ((name (make-od-name ',base-name accessor)))
`(progn
(defun ,name (array astart aend replacement)
(declare (optimize speed #.*safety-0*)
(type ,type array)
(type array-range astart aend))
(let ((string (make-array 0 :adjustable t :fill-pointer 0 :element-type 'character)))
(loop with pos = astart
while (< pos aend)
do (multiple-value-bind (bytes invalid)
(,(make-od-name ',bytes-per-base-name accessor) array pos aend replacement)
(declare (type (or null string) invalid))
(cond
((null invalid)
(vector-push-extend (,(make-od-name ',getter-base-name accessor) array pos bytes) string))
(t
(dotimes (i (length invalid))
(vector-push-extend (char invalid i) string))))
(incf pos bytes)))
(coerce string 'simple-string))))))
(instantiate-octets-definition ,definer-name))))
(def :lf define-utf8->string utf8->string bytes-per-utf8-character simple-get-utf8-char)
(def :cr define-utf8->string/cr utf8->string/cr bytes-per-utf8-character simple-get-utf8-char/cr)
(def :crlf define-utf8-string/crlf utf8->string/crlf bytes-per-utf8-character/crlf simple-get-utf8-char/crlf))
#+(and sb-unicode 64-bit little-endian
(not (or arm64 x86-64))) ;; have true simd definitions
(defun sb-vm::simd-copy-utf8-bytes-to-character-string (requested total-copied start string ibuf)
(declare (type index start requested total-copied)
(optimize speed (safety 0)))
(with-pinned-objects (string)
(let* ((head (buffer-head ibuf))
(tail (buffer-tail ibuf))
(sap (buffer-sap ibuf))
(left (- requested total-copied))
(result-characters (logand left (- sb-vm:n-word-bytes)))
(string-bytes (logand (- tail head) (- sb-vm:n-word-bytes)))
(n (min result-characters string-bytes))
(repeat (ldb (byte sb-vm:n-word-bits 0) #x0101010101010101))
(ascii-mask (* 128 repeat))
(string-sap (vector-sap string))
(string-start (truly-the fixnum (* (+ start total-copied) 4))))
(declare (optimize sb-c::preserve-single-use-debug-variables
sb-c::preserve-constants))
(loop for ibuf-offset from head below (+ head n) by sb-vm:n-word-bytes
for string-offset from string-start by (* 4 sb-vm:n-word-bytes)
do
(let ((word (sap-ref-word sap ibuf-offset)))
(when (logtest word ascii-mask)
(setf head ibuf-offset)
(incf total-copied
(truncate (- string-offset string-start) 4))
(return))
(setf (sap-ref-word string-sap string-offset)
(dpb (ldb (byte 8 8) word)
(byte 8 32)
(ldb (byte 8 0) word))
(sap-ref-word string-sap (+ string-offset 8))
(dpb (ldb (byte 8 24) word)
(byte 8 32)
(ldb (byte 8 16) word))
(sap-ref-word string-sap (+ string-offset 16))
(dpb (ldb (byte 8 40) word)
(byte 8 32)
(ldb (byte 8 32) word))
(sap-ref-word string-sap (+ string-offset 24))
(dpb (ldb (byte 8 56) word)
(byte 8 32)
(ldb (byte 8 48) word))))
finally (incf head n)
(incf total-copied n))
(setf (buffer-head ibuf) head)
total-copied)))
#+(and sb-unicode 64-bit little-endian
(not (or arm64 x86-64))) ;; have true simd definitions
(defun sb-vm::simd-copy-utf8-bytes-to-base-string (requested total-copied start string ibuf)
(declare (type index start requested total-copied)
(optimize speed (safety 0)))
(with-pinned-objects (string)
(let* ((head (buffer-head ibuf))
(tail (buffer-tail ibuf))
(sap (buffer-sap ibuf))
(n (logand (min (- requested total-copied)
(- tail head))
(- sb-vm:n-word-bytes)))
(repeat (ldb (byte sb-vm:n-word-bits 0) #x0101010101010101))
(ascii-mask (* 128 repeat))
(string-sap (vector-sap string))
(string-start (+ start total-copied)))
(declare (optimize sb-c::preserve-single-use-debug-variables
sb-c::preserve-constants))
(loop for ibuf-offset from head below (+ head n) by sb-vm:n-word-bytes
for string-offset from string-start by sb-vm:n-word-bytes
do
(let ((word (sap-ref-word sap ibuf-offset)))
(when (logtest word ascii-mask)
(setf head ibuf-offset)
(incf total-copied
(truncate (- string-offset string-start) 4))
(return))
(setf (sap-ref-word string-sap string-offset) word))
finally (incf head n)
(incf total-copied n))
(setf (buffer-head ibuf) head)
total-copied)))
(defun fd-stream-read-n-characters/utf-8 (stream buffer sbuffer start requested eof-error-p &aux (total-copied 0))
(declare (type fd-stream stream)
(type index start requested total-copied)
(type ansi-stream-cin-buffer buffer)
(type ansi-stream-csize-buffer sbuffer))
(when (fd-stream-eof-forced-p stream)
(setf (fd-stream-eof-forced-p stream) nil)
(return-from fd-stream-read-n-characters/utf-8 0))
(do ((instead (fd-stream-instead stream))
(index (+ start total-copied) (1+ index)))
((= (fill-pointer instead) 0)
(setf (fd-stream-listen stream) nil))
(setf (aref buffer index) (vector-pop instead))
(setf (aref sbuffer index) 0)
(incf total-copied)
(when (= requested total-copied)
(when (= (fill-pointer instead) 0)
(setf (fd-stream-listen stream) nil))
(return-from fd-stream-read-n-characters/utf-8 total-copied)))
#+(and sb-unicode 64-bit little-endian)
(let ((new-total (sb-vm::simd-copy-utf8-bytes-to-character-string requested total-copied
start buffer (fd-stream-ibuf stream))))
;; Make sure to change this 1 whenever
;; simd-copy-utf8-bytes-to-character-string starts processing more than
;; just ascii characters.
(fill sbuffer 1 :start (+ start total-copied) :end (+ start new-total))
(setf total-copied new-total))
(do ()
(())
(let* ((ibuf (fd-stream-ibuf stream))
(head (buffer-head ibuf))
(tail (buffer-tail ibuf))
(sap (buffer-sap ibuf))
(decode-break-reason nil))
(declare (type index head tail))
(do ((size nil nil))
((or (= tail head) (= requested total-copied)))
(setf decode-break-reason
(block decode-break-reason
(when (< (- tail head) 1) (return))
(let ((byte (sap-ref-8 sap head)))
(declare (ignorable byte))
(setq size (cond ((< byte 128) 1) ((< byte 194) (return-from decode-break-reason 1)) ((< byte 224) 2) ((< byte 240) 3) (t 4)))
(when (> size (- tail head)) (return))
(let ((index (+ start total-copied)))
(setf (aref buffer index)
(code-char
(ecase size
(1 byte)
(2
(let ((byte2 (sap-ref-8 sap (1+ head))))
(unless (<= 128 byte2 191) (return-from decode-break-reason 2))
(dpb byte (byte 5 6) byte2)))
(3
(let ((byte2 (sap-ref-8 sap (1+ head))) (byte3 (sap-ref-8 sap (+ 2 head))))
(unless
(and (<= 128 byte2 191) (<= 128 byte3 191) (or (/= byte 224) (<= 160 byte2 191))
(or (/= byte 237) (<= 128 byte2 159)))
(return-from decode-break-reason 3))
(dpb byte (byte 4 12) (dpb byte2 (byte 6 6) byte3))))
(4
(let ((byte2 (sap-ref-8 sap (1+ head))) (byte3 (sap-ref-8 sap (+ 2 head))) (byte4 (sap-ref-8 sap (+ 3 head))))
(unless
(and (<= 128 byte2 191) (<= 128 byte3 191) (<= 128 byte4 191) (or (/= byte 240) (<= 144 byte2 191))
(or (/= byte 244) (<= 128 byte2 143)))
(return-from decode-break-reason 4))
(dpb byte (byte 3 18) (dpb byte2 (byte 6 12) (dpb byte3 (byte 6 6) byte4))))))))
(setf (aref sbuffer index) size))
(incf total-copied)
(incf head size))
nil))
(setf (buffer-head ibuf) head)
(when decode-break-reason
(when (plusp total-copied) (return-from fd-stream-read-n-characters/utf-8 total-copied))
(when (stream-decoding-error-and-handle stream decode-break-reason 1)
(if eof-error-p
(error 'end-of-file :stream stream)
(return-from fd-stream-read-n-characters/utf-8 total-copied)))
(return-from fd-stream-read-n-characters/utf-8 total-copied)))
(setf (buffer-head ibuf) head)
(cond ((plusp total-copied) (return total-copied))
((null (catch 'eof-input-catcher (refill-input-buffer stream)))
(if eof-error-p
(error 'end-of-file :stream stream)
(return total-copied)))))))
;;; Bypass the character buffer, the caller must ensure that it's empty
(defun fd-stream-read-n-characters/utf-8-to-string (stream string start requested &aux (total-copied 0))
(declare (type fd-stream stream)
(type index start requested total-copied)
(type (simple-array character (*)) string))
(loop
(do ((instead (fd-stream-instead stream))
(index (+ start total-copied) (1+ index)))
((= (fill-pointer instead) 0)
(setf (fd-stream-listen stream) nil))
(setf (aref string index) (vector-pop instead))
(incf total-copied)
(when (= requested total-copied)
(when (= (fill-pointer instead) 0)
(setf (fd-stream-listen stream) nil))
(return total-copied)))
#+(and sb-unicode 64-bit little-endian)
(setf total-copied
(sb-vm::simd-copy-utf8-bytes-to-character-string requested total-copied
start string (fd-stream-ibuf stream)))
(let* ((ibuf (fd-stream-ibuf stream))
(head (buffer-head ibuf))
(tail (buffer-tail ibuf))
(sap (buffer-sap ibuf))
(decode-break-reason nil))
(declare (type index head tail))
(do ((size nil nil))
((or (= tail head) (= requested total-copied)))
(setf decode-break-reason
(block decode-break-reason
(when (< (- tail head) 1) (return))
(let ((byte (sap-ref-8 sap head)))
(declare (ignorable byte))
(setq size (cond ((< byte 128) 1) ((< byte 194) (return-from decode-break-reason 1)) ((< byte 224) 2) ((< byte 240) 3) (t 4)))
(when (> size (- tail head)) (return))
(let ((index (+ start total-copied)))
(setf (aref string index)
(code-char
(ecase size
(1 byte)
(2
(let ((byte2 (sap-ref-8 sap (1+ head))))
(unless (<= 128 byte2 191) (return-from decode-break-reason 2))
(dpb byte (byte 5 6) byte2)))
(3
(let ((byte2 (sap-ref-8 sap (1+ head))) (byte3 (sap-ref-8 sap (+ 2 head))))
(unless
(and (<= 128 byte2 191) (<= 128 byte3 191) (or (/= byte 224) (<= 160 byte2 191))
(or (/= byte 237) (<= 128 byte2 159)))
(return-from decode-break-reason 3))
(dpb byte (byte 4 12) (dpb byte2 (byte 6 6) byte3))))
(4
(let ((byte2 (sap-ref-8 sap (1+ head))) (byte3 (sap-ref-8 sap (+ 2 head))) (byte4 (sap-ref-8 sap (+ 3 head))))
(unless
(and (<= 128 byte2 191) (<= 128 byte3 191) (<= 128 byte4 191) (or (/= byte 240) (<= 144 byte2 191))
(or (/= byte 244) (<= 128 byte2 143)))
(return-from decode-break-reason 4))
(dpb byte (byte 3 18) (dpb byte2 (byte 6 12) (dpb byte3 (byte 6 6) byte4)))))))))
(incf total-copied)
(incf head size))
nil))
(setf (buffer-head ibuf) head)
(when decode-break-reason
(when (stream-decoding-error-and-handle stream decode-break-reason 1)
(return-from fd-stream-read-n-characters/utf-8-to-string total-copied))
(return)))
(when (= requested total-copied)
(setf (buffer-head ibuf) head)
(return total-copied))
(unless (catch 'eof-input-catcher (refill-input-buffer stream))
(return total-copied)))))
#+sb-unicode
(defun fd-stream-read-n-characters/utf-8-to-base-string (stream string start requested &aux (total-copied 0))
(declare (type fd-stream stream)
(type index start requested total-copied)
(type simple-base-string string))
(loop
(do ((instead (fd-stream-instead stream))
(index (+ start total-copied) (1+ index)))
((= (fill-pointer instead) 0)
(setf (fd-stream-listen stream) nil))
(setf (aref string index) (vector-pop instead))
(incf total-copied)
(when (= requested total-copied)
(when (= (fill-pointer instead) 0)
(setf (fd-stream-listen stream) nil))
(return total-copied)))
#+(and sb-unicode 64-bit little-endian)
(setf total-copied
(sb-vm::simd-copy-utf8-bytes-to-base-string requested total-copied
start string (fd-stream-ibuf stream)))
(let* ((ibuf (fd-stream-ibuf stream))
(head (buffer-head ibuf))
(tail (buffer-tail ibuf))
(sap (buffer-sap ibuf))
(decode-break-reason nil))
(declare (type index head tail))
(do ((size nil nil))
((or (= tail head) (= requested total-copied)))
(setf decode-break-reason
(block decode-break-reason
(when (< (- tail head) 1) (return))
(let ((byte (sap-ref-8 sap head)))
(declare (ignorable byte))
(setq size (cond ((< byte 128) 1) ((< byte 194) (return-from decode-break-reason 1)) ((< byte 224) 2) ((< byte 240) 3) (t 4)))
(when (> size (- tail head)) (return))
(let ((index (+ start total-copied)))
(setf (aref string index)
(code-char
(ecase size
(1 byte)
(2
(let ((byte2 (sap-ref-8 sap (1+ head))))
(unless (<= 128 byte2 191) (return-from decode-break-reason 2))
(dpb byte (byte 5 6) byte2)))
(3
(let ((byte2 (sap-ref-8 sap (1+ head))) (byte3 (sap-ref-8 sap (+ 2 head))))
(unless
(and (<= 128 byte2 191) (<= 128 byte3 191) (or (/= byte 224) (<= 160 byte2 191))
(or (/= byte 237) (<= 128 byte2 159)))
(return-from decode-break-reason 3))
(dpb byte (byte 4 12) (dpb byte2 (byte 6 6) byte3))))
(4
(let ((byte2 (sap-ref-8 sap (1+ head))) (byte3 (sap-ref-8 sap (+ 2 head))) (byte4 (sap-ref-8 sap (+ 3 head))))
(unless
(and (<= 128 byte2 191) (<= 128 byte3 191) (<= 128 byte4 191) (or (/= byte 240) (<= 144 byte2 191))
(or (/= byte 244) (<= 128 byte2 143)))
(return-from decode-break-reason 4))
(dpb byte (byte 3 18) (dpb byte2 (byte 6 12) (dpb byte3 (byte 6 6) byte4)))))))))
(incf total-copied)
(incf head size))
nil))
(setf (buffer-head ibuf) head)
(when decode-break-reason
(when (stream-decoding-error-and-handle stream decode-break-reason 1)
(return-from fd-stream-read-n-characters/utf-8-to-base-string total-copied))
(return)))
(when (= requested total-copied)
(setf (buffer-head ibuf) head)
(return total-copied))
(unless (catch 'eof-input-catcher (refill-input-buffer stream))
(return total-copied)))))
(define-external-format/variable-width (:utf-8 :utf8) t
#+sb-unicode (code-char #xfffd) #-sb-unicode #\?
(let ((bits (char-code |ch|)))
(cond ((< bits #x80) 1)
((< bits #x800) 2)
((< bits #x10000) 3)
(t 4)))
(ecase size
(1 (setf (sap-ref-8 sap tail) bits))
(2 (setf (sap-ref-8 sap tail) (logior #xc0 (ldb (byte 5 6) bits))
(sap-ref-8 sap (+ 1 tail)) (logior #x80 (ldb (byte 6 0) bits))))
(3 (when (<= #xd800 bits #xdfff)
(external-format-encoding-error stream bits))
(setf (sap-ref-8 sap tail) (logior #xe0 (ldb (byte 4 12) bits))
(sap-ref-8 sap (+ 1 tail)) (logior #x80 (ldb (byte 6 6) bits))
(sap-ref-8 sap (+ 2 tail)) (logior #x80 (ldb (byte 6 0) bits))))
(4 (setf (sap-ref-8 sap tail) (logior #xf0 (ldb (byte 3 18) bits))
(sap-ref-8 sap (+ 1 tail)) (logior #x80 (ldb (byte 6 12) bits))
(sap-ref-8 sap (+ 2 tail)) (logior #x80 (ldb (byte 6 6) bits))
(sap-ref-8 sap (+ 3 tail)) (logior #x80 (ldb (byte 6 0) bits)))))
(1 (cond ((< byte #x80) 1)
((< byte #xc2) (return-from decode-break-reason 1))
((< byte #xe0) 2)
((< byte #xf0) 3)
(t 4)))
(code-char (ecase size
(1 byte)
(2 (let ((byte2 (sap-ref-8 sap (1+ head))))
(unless (<= #x80 byte2 #xbf)
(return-from decode-break-reason 2))
(dpb byte (byte 5 6) byte2)))
(3 (let ((byte2 (sap-ref-8 sap (1+ head)))
(byte3 (sap-ref-8 sap (+ 2 head))))
(unless (and (<= #x80 byte2 #xbf)
(<= #x80 byte3 #xbf)
(or (/= byte #xe0) (<= #xa0 byte2 #xbf))
(or (/= byte #xed) (<= #x80 byte2 #x9f)))
(return-from decode-break-reason 3))
(dpb byte (byte 4 12) (dpb byte2 (byte 6 6) byte3))))
(4 (let ((byte2 (sap-ref-8 sap (1+ head)))
(byte3 (sap-ref-8 sap (+ 2 head)))
(byte4 (sap-ref-8 sap (+ 3 head))))
(unless (and (<= #x80 byte2 #xbf)
(<= #x80 byte3 #xbf)
(<= #x80 byte4 #xbf)
(or (/= byte #xf0) (<= #x90 byte2 #xbf))
(or (/= byte #xf4) (<= #x80 byte2 #x8f)))
(return-from decode-break-reason 4))
(dpb byte (byte 3 18)
(dpb byte2 (byte 6 12)
(dpb byte3 (byte 6 6) byte4)))))))
utf8->string-aref
string->utf8
#+sb-unicode :base-string-direct-mapping #+sb-unicode t
:fd-stream-read-n-characters fd-stream-read-n-characters/utf-8
:char-encodable-p (let ((bits (char-code |ch|))) (not (<= #xd800 bits #xdfff))))
(define-external-format/variable-width (:utf-8) t
#+sb-unicode (code-char #xfffd) #-sb-unicode #\?
(let ((bits (char-code |ch|)))
(cond ((< bits #x80) 1)
((< bits #x800) 2)
((< bits #x10000) 3)
(t 4)))
(ecase size
(1 (setf (sap-ref-8 sap tail) (if (= bits 10) 13 bits)))
(2 (setf (sap-ref-8 sap tail) (logior #xc0 (ldb (byte 5 6) bits))
(sap-ref-8 sap (+ 1 tail)) (logior #x80 (ldb (byte 6 0) bits))))
(3 (when (<= #xd800 bits #xdfff)
(external-format-encoding-error stream bits))