forked from dbmcclain/regex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charsets.lisp
227 lines (197 loc) · 5.8 KB
/
charsets.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
;; -*- Mode:Lisp; Package:USER; Base:10 -*-
;;
;; charsets.lsp -- sets of characters
;;
;; DM/HMSC 10/97
;; -------------------------------------------------------------------
(in-package "CHARSETS")
(defclass charset ()
((bits :accessor charset-bits
:initarg :bits
:initform (make-array 256
:element-type 'bit
:initial-element 0))))
(defun make-charset (&optional str)
(let ((cs (make-instance 'charset)))
(if str
(charset-add-chars cs str)
cs)))
(defmethod print-object ((cs charset) stream)
(format stream "~&#<~S ~A>"
(type-of cs)
(charset-printform cs))
cs)
(defmacro sbit-zerop (bits ix)
`(zerop (sbit ,bits ,ix)))
(defun number-of-ones (cs)
(let ((bits (charset-bits cs)))
(- 256 (loop for i from 0 below 256 count (sbit-zerop bits i)))))
(defun number-of-zeros (cs)
(let ((bits (charset-bits cs)))
(loop for i from 0 below 256 count (sbit-zerop bits i))))
(defun charset-empty-p (cs)
(zerop (number-of-ones cs)))
(defun charset-p (obj)
(eq (class-of obj) (find-class 'charset)))
(defun charset-char-printform (ch)
(case ch
( #\newline "\\\\r")
( #\linefeed "\\\\n")
( #\tab "\\\\t")
( #\page "\\\\f")
( #\backspace "\\\\b")
( #\escape "\\\\e")
( #\] "\\]" )
( #\\ "\\\\" )
( #\- "\\-" )
(otherwise
(if (graphic-char-p ch)
ch
(let ((*print-base* 16))
(format nil "\\\\x~A" (char-int ch)))))
))
#+LISPWORKS
(defun concatenate-strings (&rest strs)
(apply #'um:mkstr strs))
(defun charset-printform (cs)
(let* ((n1 (number-of-ones cs))
(n0 (- 256 n1))
(chars '("[") )
(notform (> n1 n0))
(start 0)
(state :start)
(bits (charset-bits cs)))
(if notform
(push "^" chars))
(loop for i from 0 below 256 do
(let ((have (or
(and notform
(sbit-zerop bits i))
(and (not notform)
(not (sbit-zerop bits i))))))
(case state
( :start
(when have
(setf state :collecting
start i)
(push (charset-char-printform
(code-char i))
chars)))
( :collecting
(unless have
(setf state :start)
(unless (= 1 (- i start))
(if (> (- i start) 2)
(push "-" chars))
(push (charset-char-printform
(code-char (1- i)))
chars))))
)))
(push "]" chars)
(apply 'concatenate-strings (reverse chars))
))
(defmethod charset-add-chars ((cs charset) (ch character))
(setf (sbit (charset-bits cs) (char-int ch)) 1)
cs)
(defmethod charset-remove-chars ((cs charset) (ch character))
(setf (sbit (charset-bits cs) (char-int ch)) 0)
cs)
(defun make-ccls (str)
;;
;; Start of a range operation.
;; Generate a bit-vector that has one bit per possible character
;; and then on each character or range, set the possible bits.
;;
;; If the first character is carat then invert the set.
;;
(let ((slen (length str))
(pos 1))
(labels
((escaped ()
(if (>= pos slen)
#\\
(let ((ch (char str pos)))
(incf pos)
(case ch
(#\n #\Linefeed)
(#\r #\Newline)
(#\f #\Page)
(#\t #\Tab)
(#\e #.(code-char #o33))
(#\b #\Backspace)
(t
(if (digit-char-p ch)
(do ((start pos))
((or (>= pos slen)
(not (digit-char-p (char str pos))))
(code-char (read-from-string
(subseq str (1- start) pos))))
(incf pos))
ch))
)))))
;; (unless (and (char= (char str (1- slen)) #\])
;; (char/= (char str (- slen 2)) #\\))
;; (error "Invalid charset syntax"))
(let* ((invert (char= (char str pos) #\^))
(bitstring (make-array 256
:element-type 'bit
:initial-element
(if invert 1 0)))
(set-char (if invert 0 1)))
(if invert (incf pos))
(do ()
((or (and (>= pos slen)
(error "Invalid charset syntax"))
(char= (char str pos) #\]))
bitstring)
(cond ((and (char= (char str (1+ pos)) #\-)
(char/= (char str (+ pos 2)) #\]))
(if (>= (char-int (char str pos))
(char-int (char str (+ 2 pos))))
(error "Invalid range \"~A-~A\".
Ranges must be in acending order"
(char str pos) (char str (+ 2 pos))))
(do ((j (char-int (char str pos)) (1+ j)))
((> j (char-int (char str (+ 2 pos))))
(incf pos 2))
(setf (sbit bitstring j) set-char)))
(t
(let ((ch (char str pos)))
;;
;; If the character is quoted then find out what
;; it should have been
;;
(incf pos)
(if (char= ch #\\ )
(setf ch (escaped)))
(setf (sbit bitstring (char-int ch)) set-char)))
))
))))
(defmethod charset-add-chars ((cs charset) (str string))
(if (char= (char str 0) #\[)
(charset-add-chars cs (make-ccls str))
(loop for ch across str do
(charset-add-chars cs ch)))
cs)
(defmethod charset-remove-chars ((cs charset) (str string))
(if (char= (char str 0) #\[)
(charset-remove-chars cs (make-ccls str))
(loop for ch across str do
(charset-remove-chars cs ch)))
cs)
(defmethod charset-add-chars ((cs charset) (bits bit-vector))
(bit-ior (charset-bits cs) bits t)
cs)
(defmethod charset-remove-chars ((cs charset) (bits bit-vector))
(bit-andc2 (charset-bits cs) bits t)
cs)
(defmethod charset-add-chars ((cs charset) (cs2 charset))
(bit-ior (charset-bits cs) (charset-bits cs2) t)
cs)
(defmethod charset-remove-chars ((cs charset) (cs2 charset))
(bit-andc2 (charset-bits cs) (charset-bits cs2) t)
cs)
(defun charset-contains-char-p (cs ch)
(not (sbit-zerop (charset-bits cs) (char-int ch))))
(provide "CHARSETS")
;; --- end of charsets.lisp --- ;;