util.lisp
  1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package:CL-WHO; Base: 10 -*-
2 ;;; $Header: /usr/local/cvsrep/cl-who/util.lisp,v 1.4 2009/01/26 11:10:49 edi Exp $
3
4 ;;; Copyright (c) 2003-2009, Dr. Edmund Weitz. All rights reserved.
5
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
17
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 (in-package :cl-who)
31
32 #+:lispworks
33 (eval-when (:compile-toplevel :load-toplevel :execute)
34 (import 'lw:with-unique-names))
35
36 #-:lispworks
37 (defmacro with-unique-names ((&rest bindings) &body body)
38 "Syntax: WITH-UNIQUE-NAMES ( { var | (var x) }* ) declaration* form*
39
40 Executes a series of forms with each VAR bound to a fresh,
41 uninterned symbol. The uninterned symbol is as if returned by a call
42 to GENSYM with the string denoted by X - or, if X is not supplied, the
43 string denoted by VAR - as argument.
44
45 The variable bindings created are lexical unless special declarations
46 are specified. The scopes of the name bindings and declarations do not
47 include the Xs.
48
49 The forms are evaluated in order, and the values of all but the last
50 are discarded \(that is, the body is an implicit PROGN)."
51 ;; reference implementation posted to comp.lang.lisp as
52 ;; <cy3bshuf30f.fsf@ljosa.com> by Vebjorn Ljosa - see also
53 ;; <http://www.cliki.net/Common%20Lisp%20Utilities>
54 `(let ,(mapcar #'(lambda (binding)
55 (check-type binding (or cons symbol))
56 (if (consp binding)
57 (destructuring-bind (var x) binding
58 (check-type var symbol)
59 `(,var (gensym ,(etypecase x
60 (symbol (symbol-name x))
61 (character (string x))
62 (string x)))))
63 `(,binding (gensym ,(symbol-name binding)))))
64 bindings)
65 ,@body))
66
67 #+:lispworks
68 (eval-when (:compile-toplevel :load-toplevel :execute)
69 (setf (macro-function 'with-rebinding)
70 (macro-function 'lw:rebinding)))
71
72 #-:lispworks
73 (defmacro with-rebinding (bindings &body body)
74 "WITH-REBINDING ( { var | (var prefix) }* ) form*
75
76 Evaluates a series of forms in the lexical environment that is
77 formed by adding the binding of each VAR to a fresh, uninterned
78 symbol, and the binding of that fresh, uninterned symbol to VAR's
79 original value, i.e., its value in the current lexical environment.
80
81 The uninterned symbol is created as if by a call to GENSYM with the
82 string denoted by PREFIX - or, if PREFIX is not supplied, the string
83 denoted by VAR - as argument.
84
85 The forms are evaluated in order, and the values of all but the last
86 are discarded \(that is, the body is an implicit PROGN)."
87 ;; reference implementation posted to comp.lang.lisp as
88 ;; <cy3wv0fya0p.fsf@ljosa.com> by Vebjorn Ljosa - see also
89 ;; <http://www.cliki.net/Common%20Lisp%20Utilities>
90 (loop for binding in bindings
91 for var = (if (consp binding) (car binding) binding)
92 for name = (gensym)
93 collect `(,name ,var) into renames
94 collect ``(,,var ,,name) into temps
95 finally (return `(let ,renames
96 (with-unique-names ,bindings
97 `(let (,,@temps)
98 ,,@body))))))
99
100 ;; TODO...
101 #+(or)
102 (defun apply-to-tree (function test tree)
103 (declare (optimize speed space))
104 (declare (type function function test))
105 "Applies FUNCTION recursively to all elements of the tree TREE \(not
106 only leaves) which pass TEST."
107 (cond
108 ((funcall test tree)
109 (funcall function tree))
110 ((consp tree)
111 (cons
112 (apply-to-tree function test (car tree))
113 (apply-to-tree function test (cdr tree))))
114 (t tree)))
115
116 (defmacro n-spaces (n)
117 "A string with N spaces - used by indentation."
118 `(make-array ,n
119 :element-type 'base-char
120 :displaced-to +spaces+
121 :displaced-index-offset 0))
122
123 (declaim (inline escape-char))
124 (defun escape-char (char &key (test *escape-char-p*))
125 (declare (optimize speed))
126 "Returns an escaped version of the character CHAR if CHAR satisfies
127 the predicate TEST. Always returns a string."
128 (if (funcall test char)
129 (case char
130 (#\< "&lt;")
131 (#\> "&gt;")
132 (#\& "&amp;")
133 (#\' "&#039;")
134 (#\" "&quot;")
135 (t (format nil (if (eq *html-mode* :xml) "&#x~x;" "&#~d;")
136 (char-code char))))
137 (make-string 1 :initial-element char)))
138
139 (defun escape-string (string &key (test *escape-char-p*))
140 (declare (optimize speed))
141 "Escape all characters in STRING which pass TEST. This function is
142 not guaranteed to return a fresh string. Note that you can pass NIL
143 for STRING which'll just be returned."
144 (let ((first-pos (position-if test string))
145 (format-string (if (eq *html-mode* :xml) "&#x~x;" "&#~d;")))
146 (if (not first-pos)
147 ;; nothing to do, just return STRING
148 string
149 (with-output-to-string (s)
150 (loop with len = (length string)
151 for old-pos = 0 then (1+ pos)
152 for pos = first-pos
153 then (position-if test string :start old-pos)
154 ;; now the characters from OLD-POS to (excluding) POS
155 ;; don't have to be escaped while the next character has to
156 for char = (and pos (char string pos))
157 while pos
158 do (write-sequence string s :start old-pos :end pos)
159 (case char
160 ((#\<)
161 (write-sequence "&lt;" s))
162 ((#\>)
163 (write-sequence "&gt;" s))
164 ((#\&)
165 (write-sequence "&amp;" s))
166 ((#\')
167 (write-sequence "&#039;" s))
168 ((#\")
169 (write-sequence "&quot;" s))
170 (otherwise
171 (format s format-string (char-code char))))
172 while (< (1+ pos) len)
173 finally (unless pos
174 (write-sequence string s :start old-pos)))))))
175
176 (defun minimal-escape-char-p (char)
177 "Helper function for the ESCAPE-FOO-MINIMAL functions to determine
178 whether CHAR must be escaped."
179 (find char "<>&"))
180
181 (defun escape-char-minimal (char)
182 "Escapes only #\<, #\>, and #\& characters."
183 (escape-char char :test #'minimal-escape-char-p))
184
185 (defun escape-string-minimal (string)
186 "Escapes only #\<, #\>, and #\& in STRING."
187 (escape-string string :test #'minimal-escape-char-p))
188
189 (defun minimal-plus-quotes-escape-char-p (char)
190 "Helper function for the ESCAPE-FOO-MINIMAL-PLUS-QUOTES functions to
191 determine whether CHAR must be escaped."
192 (find char "<>&'\""))
193
194 (defun escape-char-minimal-plus-quotes (char)
195 "Like ESCAPE-CHAR-MINIMAL but also escapes quotes."
196 (escape-char char :test #'minimal-plus-quotes-escape-char-p))
197
198 (defun escape-string-minimal-plus-quotes (string)
199 "Like ESCAPE-STRING-MINIMAL but also escapes quotes."
200 (escape-string string :test #'minimal-plus-quotes-escape-char-p))
201
202 (defun iso-8859-1-escape-char-p (char)
203 "Helper function for the ESCAPE-FOO-ISO-8859-1 functions to
204 determine whether CHAR must be escaped."
205 (or (find char "<>&'\"")
206 (> (char-code char) 255)))
207
208 (defun escape-char-iso-8859-1 (char)
209 "Escapes characters that aren't defined in ISO-8859-9."
210 (escape-char char :test #'iso-8859-1-escape-char-p))
211
212 (defun escape-string-iso-8859-1 (string)
213 "Escapes all characters in STRING which aren't defined in ISO-8859-1."
214 (escape-string string :test #'iso-8859-1-escape-char-p))
215
216 (defun non-7bit-ascii-escape-char-p (char)
217 "Helper function for the ESCAPE-FOO-ISO-8859-1 functions to
218 determine whether CHAR must be escaped."
219 (or (find char "<>&'\"")
220 (> (char-code char) 127)))
221
222 (defun escape-char-all (char)
223 "Escapes characters which aren't in the 7-bit ASCII character set."
224 (escape-char char :test #'non-7bit-ascii-escape-char-p))
225
226 (defun escape-string-all (string)
227 "Escapes all characters in STRING which aren't in the 7-bit ASCII
228 character set."
229 (escape-string string :test #'non-7bit-ascii-escape-char-p))
230
231 (defun extract-declarations (forms)
232 "Given a FORM, the declarations - if any - will be extracted
233 from the head of the FORM, and will return two values the declarations,
234 and the remaining of FORM"
235 (loop with declarations
236 for forms on forms
237 for form = (first forms)
238 while (and (consp form)
239 (eql (first form) 'cl:declare))
240 do (push form declarations)
241 finally (return (values (nreverse declarations) forms))))