-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgraham_strsub.lisp
167 lines (147 loc) · 4.66 KB
/
pgraham_strsub.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
(defun fsub (path search replace)
(with-open-file (f path :direction :input)
(let ((in-match 0)
(slen (length search))
(buffer nil))
(do ((c (read-char f nil 'eof) (read-char f nil 'eof)))
((eql c 'eof))
(push c buffer)
(if (eql c (aref search in-match))
(if (eql (+ in-match 1) slen)
(progn
(princ replace)
(setf buffer nil)
(setf in-match 0))
(incf in-match))
(progn
(setf in-match 0)
(princ (list-to-str (reverse buffer)))
(setf buffer nil))))
(unless (null buffer)
(princ (list-to-str (reverse buffer)))))))
(defun strsub (subject search replace)
(list-to-str (sub (seq-to-list subject)
(seq-to-list search)
(seq-to-list replace))))
(defun seq-to-list (seq)
(do* ((i (length seq) (- i 1))
(lst nil (push (aref seq i) lst)))
((= i 0) lst)))
(defun list-to-str (lst)
(let ((str (make-string (length lst))))
(dotimes (i (length str))
(setf (aref str i) (car lst))
(setf lst (cdr lst)))
str))
(defun sub (subject search replace)
(if (null subject)
nil
(multiple-value-bind (rest found) (match subject search)
(if found
(append replace (sub rest search replace))
(cons (car subject) (sub (cdr subject) search replace))))))
(defun match (subject search)
(cond ((null search)
(values subject t))
((null subject)
(values nil nil))
((eql (car subject) (car search))
(match (cdr subject) (cdr search)))
(t (values subject nil))))
;; -----------
(defstruct buf
vec (start -1) (used -1) (new -1) (end -1))
(defun bref (buf n)
(svref (buf-vec buf)
(mod n (length (buf-vec buf)))))
(defun (setf bref) (val buf n)
(setf (svref (buf-vec buf)
(mod n (length (buf-vec buf))))
val))
(defun new-buf (len)
(make-buf :vec (make-array len)))
(defun buf-insert (x b)
(setf (bref b (incf (buf-end b))) x))
(defun buf-pop (b)
(prog1
(bref b (incf (buf-start b)))
(setf (buf-used b) (buf-start b)
(buf-new b) (buf-end b))))
(defun buf-next (b)
(when (< (buf-used b) (buf-new b))
(bref b (incf (buf-used b)))))
(defun buf-reset (b)
(setf (buf-used b) (buf-start b)
(buf-new b) (buf-end b)))
(defun buf-clear (b)
(setf (buf-start b) -1 (buf-used b) -1
(buf-new b) -1 (buf-end b) -1))
(defun buf-flush (b str)
(do ((i (1+ (buf-used b)) (1+ i)))
((> i (buf-end b)))
(princ (bref b i) str)))
(defun file-subst (search replace inpath outpath)
(with-open-file (instr inpath)
(with-open-file (outstr outpath :direction :output :if-exists :supersede)
(stream-subst search replace instr outstr))))
;; my take
(defun stream-subst (search replace instr outstr)
(let* ((slen (length search))
(buf (new-buf slen))
(i 0))
(do ((c (read-char instr nil 'eof) (read-char instr nil 'eof)))
((eql c 'eof))
(buf-insert c buf)
(if (eql c (aref search i))
(if (eql (+ i 1) slen)
(progn
(princ replace outstr)
(buf-clear buf)
(setf i 0))
(incf i))
(progn
(buf-flush buf outstr)
(buf-clear buf)
(setf i 0))))
(buf-flush buf outstr)))
;; pg's
(defun stream-subst (old new in out)
(let* ((pos 0)
(len (length old))
(buf (new-buf len))
(from-buf nil))
(do ((c (read-char in nil :eof)
(or (setf from-buf (buf-next buf))
(read-char in nil :eof))))
((eql c :eof))
(cond ((match-char (nth pos old) c)
(incf pos)
(cond ((= pos len) ; 3
(princ new out)
(setf pos 0)
(buf-clear buf))
((not from-buf) ; 2
(buf-insert c buf))))
((zerop pos) ; 1
(princ c out)
(when from-buf
(buf-pop buf)
(buf-reset buf)))
(t ; 4
(unless from-buf
(buf-insert c buf))
(princ (buf-pop buf) out)
(buf-reset buf)
(setf pos 0))))
(buf-flush buf out)))
(defun match-char (a b)
(cond ((typep a 'character)
(char= a b))
((eql a 'd)
(digit-char-p b))
((eql a 'a)
(alphanumericp b))
((eql a '+)
t)
(t
nil)))