-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path135.body.scm
1906 lines (1724 loc) · 70.8 KB
/
135.body.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
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
;;; Copyright (C) William D Clinger (2016). All Rights Reserved.
;;; Copyright (C) Wolfgang Corcoran-Mathe (2022)
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation
;;; files (the "Software"), to deal in the Software without
;;; restriction, including without limitation the rights to use,
;;; copy, modify, merge, publish, distribute, sublicense, and/or
;;; sell copies of the Software, and to permit persons to whom the
;;; Software is furnished to do so, subject to the following
;;; conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
;;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
;;; OTHER DEALINGS IN THE SOFTWARE.
;;;; Checking forms
;; Check that i is a valid index into t.
(: %check-index (symbol textual fixnum -> undefined))
(define (%check-index loc t i)
(unless (and (>= i 0) (< i (textual-length t)))
(bounds-exception loc "index out of bounds" i t)))
;; Check that i is a valid lower bound of a range of t.
(: %check-start-index (symbol textual fixnum -> undefined))
(define (%check-start-index loc t i)
(unless (<= 0 i (textual-length t))
(bounds-exception loc "invalid start index" i t)))
;; Check that [start, end) defines a valid range of t.
(: %check-range (symbol textual fixnum fixnum -> undefined))
(define (%check-range loc t start end)
(unless (<= 0 start end (textual-length t))
(bounds-exception loc "invalid range" start end t)))
;; Check that i is a valid index into bv.
(: %check-bv-index (symbol bytevector fixnum -> undefined))
(define (%check-bv-index loc bv i)
(unless (and (>= i 0) (< i (bytevector-length bv)))
(bounds-exception loc "index out of bounds" i bv)))
;; Check that [start, end) defines a valid range of bv.
(: %check-bv-range (symbol bytevector fixnum fixnum -> undefined))
(define (%check-bv-range loc bv start end)
(unless (<= 0 start end (bytevector-length bv))
(bounds-exception loc "invalid range" start end bv)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Some macros to make textual arguments and optional arguments
;;; less painful.
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-syntax %textual->text
(syntax-rules ()
((_ x name arg ...) ; args are ignored
(begin
(assert-type name (textual? x))
(if (string? x)
(string->text x)
x)))))
;;; Several procedures take a first argument that can be either
;;; a text or a string. They can be written as though the first
;;; argument is always a text:
;;;
;;; (define-textual (f textual args ...) ...)
(define-syntax define-textual
(syntax-rules ()
((_ (f textual arg . args) expr1 expr2 ...)
(define (f textual arg . args)
(let ((textual (%textual->text textual 'f textual arg)))
expr1 expr2 ...)))))
;;; Several procedures take optional start and end arguments
;;; that follow a textual argument. They can be written as
;;; though the textual argument is always a text, the start
;;; and end arguments are always provided, and the start and
;;; end arguments are always legal:
;;;
;;; (define-textual-start-end (f args ... textual start end)
;;; ...)
(define-syntax define-textual-start-end
(syntax-rules ()
((_ (f args ... textual start end) expr1 expr2 ...)
(define f
;; Don't change this to letrec or an internal definition,
;; because recursive calls should call the version that checks.
(let ((f
(lambda (args ... textual start end) expr1 expr2 ...)))
(case-lambda
((args ... textual)
(let ((text (%textual->text textual 'f args ... textual)))
(f args ... text 0 (text-length text))))
((args ... textual start)
(let* ((text (%textual->text textual 'f args ... textual start))
(n (text-length text)))
(assert-type 'f (fixnum? start))
(%check-range 'f text start n)
(f args ... text start n)))
((args ... textual start end)
(let* ((text (%textual->text textual 'f args ... textual start end))
(n (text-length text)))
(assert-type 'f (fixnum? start))
(assert-type 'f (fixnum? end))
(%check-range 'f text start end)
(f args ... text start end)))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Predicates
;;;
;;; text? is defined by the kernel
(: textual? (* --> boolean))
(define (textual? x)
(or (text? x)
(string? x)))
(: textual-null? (textual --> boolean))
(define (textual-null? txt)
(assert-type 'textual-null? (textual? txt))
(= 0 (textual-length txt)))
(: textual-every ((char -> *) textual fixnum fixnum -> *))
(define-textual-start-end (textual-every pred textual start end)
(assert-type 'textual-every (procedure? pred))
(if (= start end)
#t
(let ((end-1 (- end 1)))
(let loop ((i start))
(if (= i end-1)
(pred (text-ref/no-checks textual i))
(and (pred (text-ref/no-checks textual i))
(loop (+ i 1))))))))
(: textual-any ((char -> *) textual fixnum fixnum -> *))
(define-textual-start-end (textual-any pred textual start end)
(assert-type 'textual-any (procedure? pred))
(let loop ((i start))
(if (= i end)
#f
(or (pred (text-ref/no-checks textual i))
(loop (+ i 1))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Constructors
;;;
;;; text-tabulate is defined by the kernel
(: make-text (fixnum char -> text))
(define (make-text n c)
(assert-type 'make-text (natural-fixnum? n))
(assert-type 'make-text (char? c))
(text-tabulate (lambda (i) c) n))
(: text (#!rest char -> text))
(define (text . chars)
(assert-type 'text (every char? chars))
(string->text (list->string chars)))
;;; These next two procedures take care to accumulate texts of
;;; the kernel's preferred size.
(: text-unfold
(procedure procedure procedure * #!optional (or char string text) procedure
-> text))
(define text-unfold
(case-lambda
((stop? mapper succ seed)
(text-unfold stop? mapper succ seed (text) (lambda (x) (text))))
((stop? mapper succ seed base)
(text-unfold stop? mapper succ seed base (lambda (x) (text))))
((stop? mapper succ seed base make-final)
(assert-type 'text-unfold (procedure? stop?))
(assert-type 'text-unfold (procedure? mapper))
(assert-type 'text-unfold (procedure? succ))
(assert-type 'text-unfold (procedure? make-final))
(let* ((txt (%textual->text (if (char? base) (text base) base)
'text-unfold
stop? mapper succ seed base make-final))
(k (text-length txt)))
(let loop ((k k)
(texts (list txt))
(chars '())
(seed seed))
(cond ((>= k chunk-size)
(let* ((k/chunk-size (quotient k chunk-size))
(k (- k (* k/chunk-size chunk-size)))
(texts (cons (reverse-list->text (list-tail chars k))
texts))
(chars (take chars k)))
(loop k texts chars seed)))
((stop? seed)
(let ((texts (if (null? chars)
texts
(cons (reverse-list->text chars) texts)))
(final (make-final seed)))
(textual-concatenate-reverse
texts
(%textify-final 'text-unfold final))))
(else
(let ((x (mapper seed)))
(assert-type 'text-unfold (%textual-or-char? x))
(cond ((char? x)
(loop (+ k 1)
texts
(cons x chars)
(succ seed)))
((string? x)
(loop (+ k (string-length x))
texts
(append (reverse (string->list x)) chars)
(succ seed)))
((text? x)
(loop (+ k (text-length x))
texts
(append (reverse (textual->list x)) chars)
(succ seed))))))))))))
(: text-unfold-right
(procedure procedure procedure * #!optional (or char string text) procedure
-> text))
(define text-unfold-right
(case-lambda
((stop? mapper succ seed)
(text-unfold-right stop? mapper succ seed (text) (lambda (x) (text))))
((stop? mapper succ seed base)
(text-unfold-right stop? mapper succ seed base (lambda (x) (text))))
((stop? mapper succ seed base make-final)
(assert-type 'text-unfold-right (procedure? stop?))
(assert-type 'text-unfold-right (procedure? mapper))
(assert-type 'text-unfold-right (procedure? succ))
(assert-type 'text-unfold-right (procedure? make-final))
(let* ((txt (%textual->text (if (char? base) (text base) base)
'text-unfold-right
stop? mapper succ seed base make-final))
(k (text-length txt)))
(let loop ((k k)
(texts (list txt))
(chars '())
(seed seed))
(cond ((>= k chunk-size)
(let* ((k/chunk-size (quotient k chunk-size))
(k (- k (* k/chunk-size chunk-size)))
(texts (cons (list->text (list-tail chars k)) texts))
(chars (take chars k)))
(loop k texts chars seed)))
((stop? seed)
(let* ((texts (if (null? chars)
texts
(cons (list->text chars) texts)))
(final (make-final seed)))
(textual-concatenate
(cons (%textify-final 'text-unfold-right final)
texts))))
(else
(let ((x (mapper seed)))
(assert-type 'text-unfold (%textual-or-char? x))
(cond ((char? x)
(loop (+ k 1)
texts
(cons x chars)
(succ seed)))
((string? x)
(loop (+ k (string-length x))
texts
(append (string->list x) chars)
(succ seed)))
((text? x)
(loop (+ k (text-length x))
texts
(append (textual->list x) chars)
(succ seed))))))))))))
;; Convert the final element of an unfold to a text.
(: %textify-final (symbol (or char string text) -> text))
(define (%textify-final loc x)
(cond ((char? x) (text x))
((string? x) (string->text x))
((text? x) x)
(else (type-exception loc "invalid final value" x))))
(: %textual-or-char? (* -> boolean))
(define (%textual-or-char? x)
(or (string? x) (text? x) (char? x)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Conversion
;;;
;;; FIXME: a lot of these could be made more efficient, especially
;;; when a string is passed instead of a text.
(: textual->text (textual -> text))
(define (textual->text x)
(assert-type 'textual->text (textual? x))
(cond ((string? x)
(string->text x))
((text? x)
x)))
(: textual->string (textual #!optional fixnum fixnum -> string))
(define textual->string
(case-lambda
((txt)
(assert-type 'textual->string (textual? txt))
(if (string? txt)
txt
(textual->string txt 0 (textual-length txt))))
((txt start)
(assert-type 'textual-string (fixnum? start))
(%check-start-index 'textual-string txt start)
(if (string? txt)
(substring txt start (string-length txt))
(textual->string txt start (textual-length txt))))
((txt start end)
(assert-type 'textual-string (textual? txt))
(assert-type 'textual-string (fixnum? start))
(assert-type 'textual-string (fixnum? end))
(%check-range 'textual-string txt start end)
(let* ((txt (%textual->text txt 'textual->string txt start end))
(n (- end start))
(s (make-string n)))
(do ((i start (+ i 1)))
((= i end)
s)
(string-set! s (- i start) (text-ref/no-checks txt i)))))))
;;; FIXME: Improve these two.
(: textual->vector (textual #!optional fixnum fixnum -> (vector-of char)))
(define-textual-start-end (textual->vector txt start end)
(list->vector (string->list (textual->string (subtext txt start end)))))
(: textual->list (textual #!optional fixnum fixnum -> (list-of char)))
(define-textual-start-end (textual->list txt start end)
(string->list (textual->string (subtext txt start end))))
(: string->text (string #!optional fixnum fixnum -> text))
(define string->text
(case-lambda
((s)
(assert-type 'string->text (string? s))
(string->text-1 s))
((s start)
(string->text s start (string-length s)))
((s start end)
(assert-type 'string->text (string? s))
(assert-type 'string->text (fixnum? start))
(assert-type 'string->text (fixnum? end))
(%check-range 'string->text s start end)
(string->text-1 (substring s start end)))))
(: vector->text ((vector-of char) #!optional fixnum fixnum -> text))
(define (vector->text v . args)
(let ((lenv (vector-length v)))
(let-optionals args ((start 0) (end lenv))
(assert-type 'vector->text (vector? v))
(assert-type 'vector->text (fixnum? start))
(assert-type 'vector->text (fixnum? end))
(unless (<= 0 start end lenv)
(bounds-exception 'vector->text "invalid range" start end v))
(text-tabulate (lambda (i) (vector-ref v (+ i start)))
(- end start)))))
(: list->text ((list-of char) #!optional fixnum fixnum -> text))
(define (list->text chars . start/end)
(assert-type 'list->text (pair-or-null? chars))
(apply string->text (list->string chars) start/end))
(: reverse-list->text ((list-of char) -> text))
(define (reverse-list->text chars)
(assert-type 'list->text (pair-or-null? chars))
(string->text (list->string (reverse chars))))
(: textual->utf8 (textual #!optional fixnum fixnum -> bytevector))
(define (textual->utf8 txt . args)
(assert-type 'textual->utf8 (textual? txt))
(let ((len (textual-length txt)))
(let-optionals args ((start 0) (end len))
(assert-type 'textual->utf8 (fixnum? start))
(assert-type 'textual->utf8 (fixnum? end))
(%check-range 'textual->utf8 txt start end)
(if (string? txt)
(string->utf8 txt start end)
(string->utf8 (textual->string (subtext txt start end)))))))
(: textual->utf16 (textual #!optional fixnum fixnum -> bytevector))
(define-textual-start-end (textual->utf16 txt start end)
(%textual->utf16 txt start end #f))
(: textual->utf16be (textual #!optional fixnum fixnum -> bytevector))
(define-textual-start-end (textual->utf16be txt start end)
(%textual->utf16 txt start end 'big))
(: textual->utf8le (textual #!optional fixnum fixnum -> bytevector))
(define-textual-start-end (textual->utf16le txt start end)
(%textual->utf16 txt start end 'little))
;;; FIXME: should this check for illegal code points?
(: %textual->utf8le (textual fixnum fixnum symbol -> bytevector))
(define (%textual->utf16 txt start end endianness)
(let* ((n (textual-fold (lambda (c n)
(cond ((< (char->integer c) #x10000)
(+ n 2))
(else
(+ n 4))))
0
txt start end))
(n (if endianness n (+ n 2)))
(result (make-bytevector n 0))
(hibits (case endianness
((big) 0)
((little) 1)
(else 0)))
(lobits (- 1 hibits)))
(if (not endianness)
(begin (bytevector-u8-set! result 0 #xfe)
(bytevector-u8-set! result 1 #xff)))
(let loop ((i start)
(j (if endianness 0 2)))
(if (= i end)
result
(let* ((c (text-ref txt i))
(cp (char->integer c)))
(cond ((< cp #x10000)
(let* ((high (quotient cp 256))
(low (- cp (* 256 high))))
(bytevector-u8-set! result (+ j hibits) high)
(bytevector-u8-set! result (+ j lobits) low))
(loop (+ i 1) (+ j 2)))
(else
(let* ((k (- cp #x10000))
(high-surrogate (+ #xd800 (quotient k 1024)))
(low-surrogate (+ #xdc00 (remainder k 1024)))
(high0 (quotient high-surrogate 256))
(low0 (- high-surrogate (* 256 high0)))
(high1 (quotient low-surrogate 256))
(low1 (- low-surrogate (* 256 high1))))
(bytevector-u8-set! result (+ j hibits) high0)
(bytevector-u8-set! result (+ j lobits) low0)
(bytevector-u8-set! result (+ j 2 hibits) high1)
(bytevector-u8-set! result (+ j 2 lobits) low1))
(loop (+ i 1) (+ j 4)))))))))
(: utf8->text (bytevector #!optional fixnum fixnum -> text))
(define utf8->text
(case-lambda
((bv)
(assert-type 'utf8->text (bytevector? bv))
(string->text (utf8->string bv)))
((bv start)
(assert-type 'utf8->text (bytevector? bv))
(assert-type 'utf8->text (fixnum? start))
(%check-bv-index 'utf8->text bv start)
(string->text (utf8->string bv start)))
((bv start end)
(assert-type 'utf8->text (bytevector? bv))
(assert-type 'utf8->text (fixnum? start))
(assert-type 'utf8->text (fixnum? end))
(%check-bv-range 'utf8->text bv start end)
(string->text (utf8->string bv start end)))))
(: utf16->text (bytevector #!optional fixnum fixnum -> text))
(define (utf16->text bv . args)
(assert-type 'utf16->text (bytevector? bv))
(let ((len (bytevector-length bv)))
(let-optionals args ((start 0) (end len))
(assert-type 'utf16->text (fixnum? start))
(assert-type 'utf16->text (fixnum? end))
(%check-bv-range 'utf16->text bv start end)
(%utf16->text bv start end #f))))
(: utf16be->text (bytevector #!optional fixnum fixnum -> text))
(define (utf16be->text bv . args)
(assert-type 'utf16be->text (bytevector? bv))
(let ((len (bytevector-length bv)))
(let-optionals args ((start 0) (end len))
(assert-type 'utf16be->text (fixnum? start))
(assert-type 'utf16be->text (fixnum? end))
(%check-bv-range 'utf16be->text bv start end)
(%utf16->text bv start end 'big))))
(: utf16le->text (bytevector #!optional fixnum fixnum -> text))
(define (utf16le->text bv . args)
(assert-type 'utf16le->text (bytevector? bv))
(let ((len (bytevector-length bv)))
(let-optionals args ((start 0) (end len))
(assert-type 'utf16le->text (fixnum? start))
(assert-type 'utf16le->text (fixnum? end))
(%check-bv-range 'utf16le->text bv start end)
(%utf16->text bv start end 'little))))
(: %utf16le->text (bytevector fixnum fixnum symbol -> text))
(define (%utf16->text bv start end endianness)
(let* ((bom (and (not endianness)
(< start end)
(let ((byte0 (bytevector-u8-ref bv start))
(byte1 (bytevector-u8-ref bv (+ start 1))))
(cond ((and (= byte0 #xfe) (= byte1 #xff))
'big)
((and (= byte1 #xfe) (= byte0 #xff))
'little)
(else #f)))))
(start (if bom (+ start 2) start))
(endianness (or endianness bom 'big))
(hibits (if (eq? endianness 'big) 0 1))
(lobits (- 1 hibits)))
(text-unfold
(lambda (i) (>= i end))
(lambda (i)
(let* ((high (bytevector-u8-ref bv (+ i hibits)))
(low (bytevector-u8-ref bv (+ i lobits)))
(cp (if (= high 0) low (+ (* 256 high) low))))
(cond ((< cp #xd800)
(integer->char cp))
((and (< cp #xdc00)
(< (+ i 2) end))
(let* ((i (+ i 2))
(high (bytevector-u8-ref bv (+ i hibits)))
(low (bytevector-u8-ref bv (+ i lobits)))
(cp2 (if (= high 0) low (+ (* 256 high) low))))
(cond ((<= #xdc00 cp2 #xdfff)
(integer->char
(+ #x10000
(* 1024 (- cp #xd800))
(- cp2 #xdc00))))
(else
(%illegal-utf16 bv (- i 2) cp cp2)))))
((< cp #x10000)
(integer->char cp))
(else
(%illegal-utf16 bv i cp)))))
(lambda (i)
(let ((cp (+ (* 256 (bytevector-u8-ref bv (+ i hibits)))
(bytevector-u8-ref bv (+ i lobits)))))
(if (or (< cp #xd800)
(<= #xe000 cp #xffff))
(+ i 2)
(+ i 4))))
start)))
(define (%illegal-utf16 loc . args)
(abort
(make-composite-condition
(make-property-condition 'exn
'location loc
'message "illegal UTF-16"
'arguments args)
(make-property-condition 'encoding))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Selection
;;;
;;; text-length, text-ref, and subtext are defined by the kernel
(: textual-length (textual --> fixnum))
(define (textual-length txt)
(assert-type 'textual-length (textual? txt))
(if (string? txt)
(string-length txt)
(text-length txt)))
(: textual-ref (textual fixnum -> char))
(define (textual-ref txt i)
(assert-type 'textual-ref (textual? txt))
(assert-type 'textual-ref (fixnum? i))
(if (string? txt)
(string-ref txt i)
(text-ref/no-checks txt i)))
(: subtextual (textual fixnum fixnum -> text))
(define-textual (subtextual txt start end)
(subtext txt start end))
;;; FIXME: could be faster, but this procedure shouldn't be used much
(: textual-copy (textual #!optional fixnum fixnum -> text))
(define-textual-start-end (textual-copy text start end)
(string->text (textual->string text start end)))
(: textual-take (textual fixnum -> text))
(define-textual (textual-take txt nchars)
(subtextual txt 0 nchars))
(: textual-drop (textual fixnum -> text))
(define-textual (textual-drop txt nchars)
(subtextual txt nchars (text-length txt)))
(: textual-take-right (textual fixnum -> text))
(define-textual (textual-take-right txt nchars)
(let ((n (text-length txt)))
(subtextual txt (- n nchars) n)))
(: textual-drop-right (textual fixnum -> text))
(define-textual (textual-drop-right txt nchars)
(let ((n (text-length txt)))
(subtextual txt 0 (- n nchars))))
(: textual-pad
(textual fixnum #!optional char fixnum fixnum -> text))
(define (textual-pad t len . args)
(let* ((txt (%textual->text t 'textual-pad t))
(old-len (text-length txt)))
(let-optionals args ((c #\space) (start 0) (end old-len))
(assert-type 'text-pad (natural-fixnum? len))
(when (pair? args)
(assert-type 'text-pad (char? c))
(assert-type 'text-pad (fixnum? start))
(assert-type 'text-pad (fixnum? end))
(%check-range 'text-pad txt start end))
(%text-pad txt len c start end))))
(: %text-pad (text fixnum char fixnum fixnum -> text))
(define (%text-pad txt len c start end)
(let* ((n (text-length txt))
(k (- end start)))
(cond ((= n k len)
txt)
((= k len)
(if (= n k)
txt
(subtext txt start end)))
((< k len)
(textual-append (make-text (- len k) c)
(if (= n k)
txt
(subtext txt start end))))
(else
(subtext txt (- end len) end)))))
(: textual-pad-right
(textual fixnum #!optional char fixnum fixnum -> text))
(define (textual-pad-right t len . args)
(let* ((txt (%textual->text t 'textual-pad-right t len))
(old-len (text-length txt)))
(let-optionals args ((c #\space) (start 0) (end old-len))
(assert-type 'text-pad-right (natural-fixnum? len))
(when (pair? args)
(assert-type 'text-pad-right (char? c))
(assert-type 'text-pad-right (fixnum? start))
(assert-type 'text-pad-right (fixnum? end))
(%check-range 'text-pad-right txt start end))
(%text-pad-right txt len c start end))))
(: %text-pad-right (text fixnum char fixnum fixnum -> text))
(define (%text-pad-right txt len c start end)
(let* ((n (text-length txt))
(k (- end start)))
(cond ((= n k len)
txt)
((= k len)
(if (= n k)
txt
(subtext txt start end)))
((< k len)
(textual-append (if (= n k)
txt
(subtext txt start end))
(make-text (- len k) c)))
(else
(subtext txt start (+ start len))))))
(: textual-trim
(textual #!optional (char -> boolean) fixnum fixnum -> text))
(define textual-trim
(case-lambda
((txt)
(textual-trim txt char-whitespace? 0))
((txt pred)
(textual-trim txt pred 0))
((txt pred start)
(let ((txt (%textual->text txt 'textual-trim txt pred start)))
(%text-trim txt pred start (text-length txt))))
((txt pred start end)
(let ((txt (%textual->text txt 'textual-trim txt pred start end)))
(%text-trim txt pred start end)))))
(: %text-trim (text (char -> boolean) fixnum fixnum -> text))
(define (%text-trim txt pred start end)
(assert-type 'textual-trim (procedure? pred))
(assert-type 'textual-trim (fixnum? start))
(assert-type 'textual-trim (fixnum? end))
(%check-range 'textual-trim txt start end)
(let loop ((i start))
(cond ((= i end)
(text))
((pred (text-ref/no-checks txt i))
(loop (+ i 1)))
(else
(subtext txt i end)))))
(: textual-trim-right
(textual #!optional (char -> boolean) fixnum fixnum -> text))
(define textual-trim-right
(case-lambda
((txt)
(textual-trim-right txt char-whitespace? 0))
((txt pred)
(textual-trim-right txt pred 0))
((txt pred start)
(let ((txt (%textual->text txt 'textual-trim-right txt pred start)))
(%text-trim-right txt pred start (text-length txt))))
((txt pred start end)
(let ((txt (%textual->text txt 'textual-trim-right txt pred start end)))
(%text-trim-right txt pred start end)))))
(: %text-trim-right (text (char -> boolean) fixnum fixnum -> text))
(define (%text-trim-right txt pred start end)
(assert-type 'textual-trim-right (procedure? pred))
(assert-type 'textual-trim-right (fixnum? start))
(assert-type 'textual-trim-right (fixnum? end))
(%check-range 'textual-trim-right txt start end)
(let loop ((i (- end 1)))
(cond ((< i start)
(text))
((pred (text-ref/no-checks txt i))
(loop (- i 1)))
(else
(subtext txt start (+ i 1))))))
(: textual-trim-both
(textual #!optional (char -> boolean) fixnum fixnum -> text))
(define textual-trim-both
(case-lambda
((txt)
(textual-trim-both txt char-whitespace? 0))
((txt pred)
(textual-trim-both txt pred 0))
((txt pred start)
(let ((txt (%textual->text txt 'textual-trim-both txt pred start)))
(%text-trim-both txt pred start (text-length txt))))
((txt pred start end)
(let ((txt (%textual->text txt 'textual-trim-both txt pred start end)))
(%text-trim-both txt pred start end)))))
;;; This is efficient because subtext is fast.
(: %text-trim-both (text (char -> boolean) fixnum fixnum -> text))
(define (%text-trim-both txt pred start end)
(assert-type 'textual-trim-both (procedure? pred))
(assert-type 'textual-trim-both (fixnum? start))
(assert-type 'textual-trim-both (fixnum? end))
(%check-range 'textual-trim-both txt start end)
(textual-trim (textual-trim-right txt pred start end) pred))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Replacement
(: textual-replace
(text text fixnum fixnum #!optional fixnum fixnum -> text))
(define textual-replace
(case-lambda
((txt1 txt2 start1 end1 start2 end2)
(assert-type 'textual-replace (textual? txt1))
(assert-type 'textual-replace (textual? txt2))
(assert-type 'textual-replace (fixnum? start1))
(assert-type 'textual-replace (fixnum? end1))
(assert-type 'textual-replace (fixnum? start2))
(assert-type 'textual-replace (fixnum? end2))
(textual-append (subtextual txt1 0 start1)
(subtextual txt2 start2 end2)
(subtextual txt1 end1 (textual-length txt1))))
((txt1 txt2 start1 end1 start2)
(assert-type 'textual-replace (textual? txt1))
(assert-type 'textual-replace (textual? txt2))
(assert-type 'textual-replace (fixnum? start1))
(assert-type 'textual-replace (fixnum? end1))
(assert-type 'textual-replace (fixnum? start2))
(textual-append (subtextual txt1 0 start1)
(subtextual txt2 start2 (textual-length txt2))
(subtextual txt1 end1 (textual-length txt1))))
((txt1 txt2 start1 end1)
(assert-type 'textual-replace (textual? txt1))
(assert-type 'textual-replace (textual? txt2))
(assert-type 'textual-replace (fixnum? start1))
(assert-type 'textual-replace (fixnum? end1))
(textual-append (subtextual txt1 0 start1)
txt2
(subtextual txt1 end1 (textual-length txt1))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Comparison
(define (make-nary-comparison name binop0)
(let ((binop (lambda (a b)
(let ((a (%textual->text a name a b))
(b (%textual->text b name a b)))
(binop0 a b)))))
(letrec ((loop (lambda (first rest)
(cond ((null? rest)
#t)
((binop first (car rest))
(loop (car rest) (cdr rest)))
(else
#f)))))
(lambda (a b . rest)
(if (null? rest)
(binop a b)
(and (binop a b)
(loop b rest)))))))
(: textual=? (textual textual #!rest textual -> boolean))
(define textual=?
(make-nary-comparison 'textual=?
(lambda (a b)
(%text-compare a b =))))
(: textual<? (textual textual #!rest textual -> boolean))
(define textual<?
(make-nary-comparison 'textual<?
(lambda (a b)
(%text-compare a b <))))
(: textual<=? (textual textual #!rest textual -> boolean))
(define textual<=?
(make-nary-comparison 'textual<=?
(lambda (a b)
(%text-compare a b <=))))
(: textual>? (textual textual #!rest textual -> boolean))
(define textual>?
(make-nary-comparison 'textual>?
(lambda (a b)
(%text-compare a b >))))
(: textual>=? (textual textual #!rest textual -> boolean))
(define textual>=?
(make-nary-comparison 'textual>=?
(lambda (a b)
(%text-compare a b >=))))
(: textual-ci=? (textual textual #!rest textual -> boolean))
(define textual-ci=?
(make-nary-comparison 'textual-ci=?
(lambda (a b)
(%text-compare-ci a b = string-ci=?))))
(: textual-ci<? (textual textual #!rest textual -> boolean))
(define textual-ci<?
(make-nary-comparison 'textual-ci<?
(lambda (a b)
(%text-compare-ci a b < string-ci<?))))
(: textual-ci<=? (textual textual #!rest textual -> boolean))
(define textual-ci<=?
(make-nary-comparison 'textual-ci<=?
(lambda (a b)
(%text-compare-ci a b <= string-ci<=?))))
(: textual-ci>? (textual textual #!rest textual -> boolean))
(define textual-ci>?
(make-nary-comparison 'textual-ci>?
(lambda (a b)
(%text-compare-ci a b > string-ci>?))))
(: textual-ci>=? (textual textual #!rest textual -> boolean))
(define textual-ci>=?
(make-nary-comparison 'textual-ci>=?
(lambda (a b)
(%text-compare-ci a b >= string-ci>=?))))
;;; Compares texts a and b.
;;; Determines whether a is less than b (-1), equal (0), or
;;; greater than b (+1), computes the boolean result by
;;; calling make-boolean on that numerical value and 0.
(: %text-compare (text text (fixnum fixnum -> boolean) -> boolean))
(define (%text-compare a b make-boolean)
(let* ((na (text-length a))
(nb (text-length b))
(n (if (<= na nb) na nb)))
(define (loop i)
(if (= i n)
(cond ((< na nb) (make-boolean -1 0))
((> na nb) (make-boolean +1 0))
(else (make-boolean 0 0)))
(let ((ca (text-ref/no-checks a i))
(cb (text-ref/no-checks b i)))
(cond ((char<? ca cb) (make-boolean -1 0))
((char>? ca cb) (make-boolean +1 0))
(else (loop (+ i 1)))))))
(loop 0)))
;;; Compares texts a and b, folding case.
;;; If either text contains non-ASCII characters, both are converted
;;; to strings and compared using string-pred.
(: %text-compare-ci
(text text (fixnum fixnum -> boolean) (string string -> boolean)
-> boolean))
(define (%text-compare-ci a b make-boolean string-pred)
(let* ((na (text-length a))
(nb (text-length b))
(n (if (<= na nb) na nb)))
(define (loop i)
(if (= i n)
(cond ((< na nb) (make-boolean -1 0))
((> na nb) (make-boolean +1 0))
(else (make-boolean 0 0)))
(let ((ca (text-ref/no-checks a i))
(cb (text-ref/no-checks b i)))
(if (or (char>? ca #\delete)
(char>? cb #\delete))
(string-pred (textual->string a)
(textual->string b))
(let ((ca (char-foldcase ca))
(cb (char-foldcase cb)))
(cond ((char<? ca cb) (make-boolean -1 0))
((char>? ca cb) (make-boolean +1 0))
(else (loop (+ i 1)))))))))
(loop 0)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Prefixes & suffixes
(define (%make-text-prefix/suffix-proc proc name)
(lambda (t1 t2 . args)
(let ((txt1 (%textual->text t1 name t1))
(len1 (textual-length t1))
(txt2 (%textual->text t2 name t2))
(len2 (textual-length t2)))
(let-optionals args ((start1 0)
(end1 len1)
(start2 0)
(end2 len2))
(when (pair? args)
(assert-type name (fixnum? start1))
(assert-type name (fixnum? end1))
(assert-type name (fixnum? start2))
(assert-type name (fixnum? end2))
(%check-range name t1 start1 end1)
(%check-range name t2 start2 end2))
(proc txt1 txt2 start1 end1 start2 end2)))))
(: textual-prefix-length
(textual textual #!optional fixnum fixnum fixnum fixnum
-> fixnum))
(define textual-prefix-length
(%make-text-prefix/suffix-proc
(lambda (txt1 txt2 start1 end1 start2 end2)
(%text-prefix-length txt1 txt2 start1 end1 start2 end2))
'textual-prefix-length))
(: textual-suffix-length
(textual textual #!optional fixnum fixnum fixnum fixnum
-> fixnum))
(define textual-suffix-length
(%make-text-prefix/suffix-proc
(lambda (txt1 txt2 start1 end1 start2 end2)
(%text-suffix-length txt1 txt2 start1 end1 start2 end2))
'textual-suffix-length))
(: textual-prefix?
(textual textual #!optional fixnum fixnum fixnum fixnum
-> boolean))
(define textual-prefix?
(%make-text-prefix/suffix-proc
(lambda (txt1 txt2 start1 end1 start2 end2)
(%text-prefix? txt1 txt2 start1 end1 start2 end2))
'textual-prefix?))
(: textual-suffix?
(textual textual #!optional fixnum fixnum fixnum fixnum
-> boolean))
(define textual-suffix?
(%make-text-prefix/suffix-proc
(lambda (txt1 txt2 start1 end1 start2 end2)
(%text-suffix? txt1 txt2 start1 end1 start2 end2))
'textual-suffix?))
;;; All error checking has already been done.
(: %text-prefix-length
(text text fixnum fixnum fixnum fixnum -> fixnum))
(define (%text-prefix-length txt1 txt2 start1 end1 start2 end2)
(let* ((k1 (- end1 start1))
(k2 (- end2 start2))
(k (min k1 k2))
(end1 (+ start1 k)))
(let loop ((i start1)