forked from rtoy/ansi-cl-tests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchar-aux.lsp
333 lines (301 loc) · 11.7 KB
/
char-aux.lsp
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
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Sat Oct 5 20:15:55 2002
;;;; Contains: Auxiliary functions for character tests
(in-package :cl-test)
(defun is-ordered-by (seq fn)
(declare (type function fn))
(let ((n (length seq)))
(loop for i from 0 below (1- n)
for e = (elt seq i)
always
(loop for j from (1+ i) below n
always (funcall fn e (elt seq j))))))
(defun is-antisymmetrically-ordered-by (seq fn)
(declare (type function fn))
(and (is-ordered-by seq fn)
(is-ordered-by (reverse seq) (complement fn))))
(defun is-case-insensitive (fn)
(when (symbolp fn)
(assert (fboundp fn))
(setf fn (symbol-function fn)))
(assert (typep fn 'function))
(locally
(declare (type function fn))
(loop for c across +code-chars+
for c1 = (char-upcase c)
for c2 = (if (eql c c1) (char-downcase c) c1)
always
(loop for d across +code-chars+
for d1 = (char-upcase d)
for d2 = (if (eql d d1) (char-downcase d) d1)
always (equiv (funcall fn c d)
(funcall fn c2 d)
(funcall fn c d2)
(funcall fn c2 d2))))))
(defun equiv (&rest args)
(declare (dynamic-extent args))
(cond
((null args) t)
((car args)
(loop for e in (cdr args) always e))
(t (loop for e in (cdr args) never e))))
;;; From character.lsp
(defun char-type-error-check (fn)
(when (symbolp fn)
(assert (fboundp fn))
(setf fn (symbol-function fn)))
(assert (typep fn 'function))
(locally
(declare (type function fn))
(loop for x in *universe*
always (or (characterp x)
;; FIXME -- catch the type error and check that datum
;; is eql to x (and that datum is not in the expected type)
(eqt (catch-type-error (funcall fn x)) 'type-error)))))
(defun standard-char.5.body ()
(loop for i from 0 below (min 65536 char-code-limit) for c = (code-char i)
unless (not (and (typep c 'standard-char)
(not (standard-char-p c))))
collect (char-name c)))
(defun extended-char.3.body ()
(loop for i from 0 below (min 65536 char-code-limit) for c = (code-char i)
unless (not (and (typep c 'base-char)
(typep c 'extended-char)))
collect (char-name c)))
(defun character.1.body ()
(loop for i from 0 below (min 65536 char-code-limit) for c = (code-char i)
unless (or (null c)
(let ((s (string c)))
(and
(eqlt (character c) c)
(eqlt (character s) c)
(eqlt (character (make-symbol s)) c))))
collect (char-name c)))
(defun character.2.body ()
(loop for x in *universe*
when (not (or (characterp x)
(and (stringp x) (eqlt (length x) 1))
(and (symbolp x) (eqlt (length (symbol-name x)) 1))
(let ((c (catch-type-error (character x))))
(or (eqlt c 'type-error)
(let ((s (catch-type-error (string x))))
(and (stringp s) (eqlt (my-aref s 0) c)))))))
do (return x)))
(defun characterp.2.body ()
(loop for i from 0 below (min 65536 char-code-limit) for c = (code-char i)
unless (or (null c) (characterp c))
collect (char-name c)))
(defun characterp.3.body ()
(loop for x in *universe*
unless (let ((p (characterp x))
(q (typep x 'character)))
(if p (notnot q) (not q)))
collect x))
(defun alphanumericp.4.body ()
(loop for x in *universe*
unless (or (not (characterp x))
(if (or (digit-char-p x) (alpha-char-p x))
(alphanumericp x)
;; The hyperspec has an example that claims alphanumeric ==
;; digit-char-p or alpha-char-p, but the text seems to suggest
;; that there can be numeric characters for which digit-char-p
;; returns NIL. Therefore, I've weakened the next line
;; (not (alphanumericp x))
t))
collect x))
(defun alphanumericp.5.body ()
(loop for i from 0 below (min 65536 char-code-limit)
for x = (code-char i)
unless (or (not (characterp x))
(if (or (digit-char-p x) (alpha-char-p x))
(alphanumericp x)
;; The hyperspec has an example that claims alphanumeric ==
;; digit-char-p or alpha-char-p, but the text seems to suggest
;; that there can be numeric characters for which digit-char-p
;; returns NIL. Therefore, I've weakened the next line
;; (not (alphanumericp x))
t))
collect (char-name x)))
(defun digit-char.1.body.old ()
(loop for r from 2 to 36 always
(loop for i from 0 to 36
always (let* ((c (digit-char i r))
(result
(if (>= i r) (null c)
(eqlt c (char +extended-digit-chars+ i)))))
(unless result
(format t "~A ~A ~A~%" r i c))
result))))
(defun digit-char.1.body ()
(loop for r from 2 to 36 nconc
(loop for i from 0 to 36
for c = (digit-char i r)
unless (if (>= i r) (null c)
(eqlt c (char +extended-digit-chars+ i)))
collect (list r i c))))
(defun digit-char-p.1.body ()
(loop for x in *universe*
unless (not (and (characterp x)
(not (alphanumericp x))
(digit-char-p x)))
collect x))
(defun digit-char-p.2.body ()
(loop for i from 0 below (min 65536 char-code-limit)
for x = (code-char i)
unless (or (not x)
(not (and (not (alphanumericp x))
(digit-char-p x))))
collect (char-name x)))
(defun digit-char-p.3.body ()
(loop for r from 2 to 35
for bad =
(loop for i from r to 35
for c = (char +extended-digit-chars+ i)
when (or (digit-char-p c r)
(digit-char-p (char-downcase c) r))
collect i)
when bad collect (cons r bad)))
(defun digit-char-p.4.body ()
(loop for r from 2 to 35
for bad =
(loop for i from 0 below r
for c = (char +extended-digit-chars+ i)
unless (and (eqlt (digit-char-p c r) i)
(eqlt (digit-char-p (char-downcase c) r) i))
collect i)
when bad collect (cons r bad)))
(defun standard-char-p.2.body ()
(loop for x in *universe*
unless (or (not (characterp x))
(find x +standard-chars+)
(not (standard-char-p x)))
collect x))
(defun standard-char-p.2a.body ()
(loop for i from 0 below (min 65536 char-code-limit)
for x = (code-char i)
unless (or (not (characterp x))
(find x +standard-chars+)
(not (standard-char-p x)))
collect (char-name x)))
(defun char-upcase.1.body ()
(loop for x in *universe*
unless (or (not (characterp x))
(let ((u (char-upcase x)))
(and
(or (lower-case-p x) (eqlt u x))
(eqlt u (char-upcase u)))))
collect (char-name x)))
(defun char-upcase.2.body ()
(loop for i from 0 below (min 65536 char-code-limit)
for x = (code-char i)
unless (or (not x)
(let ((u (char-upcase x)))
(and
(or (lower-case-p x) (eqlt u x))
(eqlt u (char-upcase u)))))
collect (char-name x)))
(defun char-downcase.1.body ()
(loop for x in *universe*
unless (or (not (characterp x))
(let ((u (char-downcase x)))
(and
(or (upper-case-p x) (eqlt u x))
(eqlt u (char-downcase u)))))
collect (char-name x)))
(defun char-downcase.2.body ()
(loop for i from 0 below (min 65536 char-code-limit)
for x = (code-char i)
unless (or (not x)
(let ((u (char-downcase x)))
(and
(or (upper-case-p x) (eqlt u x))
(eqlt u (char-downcase u)))))
collect (char-name x)))
(defun both-case-p.1.body ()
(loop for x in *universe*
unless (or (not (characterp x))
(if (both-case-p x)
(and (graphic-char-p x)
(or (upper-case-p x)
(lower-case-p x)))
(not (or (upper-case-p x)
(lower-case-p x)))))
collect (char-name x)))
(defun both-case-p.2.body ()
(loop for i from 0 below (min 65536 char-code-limit)
for x = (code-char i)
unless (or (not (characterp x))
(if (both-case-p x)
(and (graphic-char-p x)
(or (upper-case-p x)
(lower-case-p x)))
(not (or (upper-case-p x)
(lower-case-p x)))))
collect (char-name x)))
(defun char-code.2.body ()
(loop for i from 0 below (min 65536 char-code-limit)
for c = (code-char i)
unless (or (not c)
(eqlt (char-code c) i))
collect (char-name c)))
(defun char-int.2.fn ()
(declare (optimize (safety 3) (speed 1) (space 1)))
(let ((c->i (make-hash-table :test #'equal))
(i->c (make-hash-table :test #'eql)))
(flet ((%insert
(c)
(or (not (characterp c))
(let* ((i (char-int c))
(j (gethash c c->i))
(d (gethash i i->c)))
(and
(or (null j) (eqlt j i))
(or (null d) (char= c d))
(progn
(setf (gethash c c->i) i)
(setf (gethash i i->c) c)
t))))))
(or
(loop for i from 0 below (min (ash 1 16) char-code-limit)
unless (%insert (code-char i))
collect i)
(loop for i = (random char-code-limit)
repeat 1000
unless (%insert (code-char i))
collect i)
(find-if-not #'%insert +standard-chars+)
(find-if-not #'%insert *universe*)))))
(defun char-name.1.fn ()
(declare (optimize (safety 3) (speed 1) (space 1)))
(flet ((%check
(c)
(or (not (characterp c))
(let ((name (char-name c)))
(or (null name)
(and (stringp name)
(eqlt c (name-char name))))))))
(and
(loop for i from 0 below (min (ash 1 16) char-code-limit)
always (%check (code-char i)))
(every #'%check +standard-chars+)
(every #'%check *universe*)
t)))
(defun name-char.1.body ()
(declare (optimize (safety 3)))
(loop for x in *universe*
for s = (catch-type-error (string x))
unless
(or (eqlt s 'type-error)
(let ((c (name-char x)))
(or (not c)
(characterp c)
;; FIXME The rest of this wasn't reachable
#|
(let ((name (char-name c)))
(declare (type (or null string) name))
(and name
(string-equal name s)))
|#
)))
collect x))