Coverage report: /home/ellis/.stash/quicklisp/dists/ultralisp/software/edicl-flexi-streams-20240429143708/util.lisp
Kind | Covered | All | % |
expression | 33 | 190 | 17.4 |
branch | 0 | 22 | 0.0 |
Key
Not instrumented
Conditionalized out
Executed
Not executed
Both branches taken
One branch taken
Neither branch taken
1
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: FLEXI-STREAMS; Base: 10 -*-
2
;;; $Header: /usr/local/cvsrep/flexi-streams/util.lisp,v 1.24 2008/05/25 21:26:12 edi Exp $
4
;;; Copyright (c) 2005-2008, Dr. Edmund Weitz. All rights reserved.
6
;;; Redistribution and use in source and binary forms, with or without
7
;;; modification, are permitted provided that the following conditions
10
;;; * Redistributions of source code must retain the above copyright
11
;;; notice, this list of conditions and the following disclaimer.
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.
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.
30
(in-package :flexi-streams)
33
(eval-when (:compile-toplevel :load-toplevel :execute)
34
(import '(lw:with-unique-names lw:when-let)))
37
(defmacro when-let ((var form) &body body)
38
"Evaluates FORM and binds VAR to the result, then executes BODY
39
if VAR has a true value."
44
(defmacro with-unique-names ((&rest bindings) &body body)
45
"Syntax: WITH-UNIQUE-NAMES ( { var | (var x) }* ) declaration* form*
47
Executes a series of forms with each VAR bound to a fresh,
48
uninterned symbol. The uninterned symbol is as if returned by a call
49
to GENSYM with the string denoted by X - or, if X is not supplied, the
50
string denoted by VAR - as argument.
52
The variable bindings created are lexical unless special declarations
53
are specified. The scopes of the name bindings and declarations do not
56
The forms are evaluated in order, and the values of all but the last
57
are discarded \(that is, the body is an implicit PROGN)."
58
;; reference implementation posted to comp.lang.lisp as
59
;; <cy3bshuf30f.fsf@ljosa.com> by Vebjorn Ljosa - see also
60
;; <http://www.cliki.net/Common%20Lisp%20Utilities>
61
`(let ,(mapcar #'(lambda (binding)
62
(check-type binding (or cons symbol))
64
(destructuring-bind (var x) binding
65
(check-type var symbol)
66
`(,var (gensym ,(etypecase x
67
(symbol (symbol-name x))
68
(character (string x))
70
`(,binding (gensym ,(symbol-name binding)))))
75
(eval-when (:compile-toplevel :load-toplevel :execute)
76
(setf (macro-function 'with-rebinding)
77
(macro-function 'lw:rebinding)))
80
(defmacro with-rebinding (bindings &body body)
81
"WITH-REBINDING ( { var | (var prefix) }* ) form*
83
Evaluates a series of forms in the lexical environment that is
84
formed by adding the binding of each VAR to a fresh, uninterned
85
symbol, and the binding of that fresh, uninterned symbol to VAR's
86
original value, i.e., its value in the current lexical environment.
88
The uninterned symbol is created as if by a call to GENSYM with the
89
string denoted by PREFIX - or, if PREFIX is not supplied, the string
90
denoted by VAR - as argument.
92
The forms are evaluated in order, and the values of all but the last
93
are discarded \(that is, the body is an implicit PROGN)."
94
;; reference implementation posted to comp.lang.lisp as
95
;; <cy3wv0fya0p.fsf@ljosa.com> by Vebjorn Ljosa - see also
96
;; <http://www.cliki.net/Common%20Lisp%20Utilities>
97
(loop for binding in bindings
98
for var = (if (consp binding) (car binding) binding)
100
collect `(,name ,var) into renames
101
collect ``(,,var ,,name) into temps
102
finally (return `(let ,renames
103
(with-unique-names ,bindings
107
(defun normalize-external-format-name (name)
108
"Converts NAME \(a symbol) to a `canonical' name for an
109
external format, e.g. :LATIN1 will be converted to :ISO-8859-1.
110
Also checks if there is an external format with that name and
111
signals an error otherwise."
112
(let ((real-name (cdr (find name flex::+name-map+
113
:test (lambda (item pair)
114
(or (string-equal item (cdr pair))
115
(string-equal item (car pair))))))))
117
(error 'external-format-error
118
:format-control "~S is not known to be a name for an external format."
119
:format-arguments (list name)))
122
(defun ascii-name-p (name)
123
"Checks whether NAME is the keyword :ASCII."
126
(defun koi8-r-name-p (name)
127
"Checks whether NAME is the keyword :KOI8-R."
130
(defun mac-roman-name-p (name)
131
"Checks whether NAME is the keyword :MAC-ROMAN."
132
(eq name :mac-roman))
134
(defun code-page-name-p (name)
135
"Checks whether NAME is the keyword :CODE-PAGE."
136
(eq name :code-page))
138
(defun iso-8859-name-p (name)
139
"Checks whether NAME \(a keyword) names one of the known
141
(find name +iso-8859-tables+ :key #'car))
143
(defun known-code-page-id-p (id)
144
"Checks whether ID \(a number) denotes one of the known Windows
146
(and (find id +code-page-tables+ :key #'car)
150
(defun sans (plist &rest keys)
151
"Returns PLIST with keyword arguments from KEYS removed."
152
(sys::remove-properties plist keys))
155
(defun sans (plist &rest keys)
156
"Returns PLIST with keyword arguments from KEYS removed."
157
;; stolen from Usenet posting <3247672165664225@naggum.no> by Erik
161
(let ((tail (nth-value 2 (get-properties plist keys))))
162
;; this is how it ends
164
(return (nreconc sans plist)))
165
;; copy all the unmatched keys
166
(loop until (eq plist tail) do
167
(push (pop plist) sans)
168
(push (pop plist) sans))
169
;; skip the matched key
170
(setq plist (cddr plist))))))
173
(defmacro with-accessors (slot-entries instance &body body)
174
"For LispWorks, we prefer SLOT-VALUE over accessors for better
176
;; note that we assume that the variables have the same names as the
178
`(with-slots ,(mapcar #'car slot-entries)
182
(defun make-octet-buffer (&optional (size +buffer-size+))
183
"Creates and returns a fresh buffer \(a specialized array) of size
184
+BUFFER-SIZE+ to hold octets."
185
(declare #.*standard-optimize-settings*)
186
(make-array size :element-type 'octet))
188
(defun type-equal (type1 type2)
189
"Whether TYPE1 and TYPE2 denote the same type."
190
(declare #.*standard-optimize-settings*)
191
(and (subtypep type1 type2)
192
(subtypep type2 type1)))
194
(defun maybe-rewind (stream octets)
195
"Tries to `rewind' the \(binary) stream STREAM by OCTETS octets.
196
Returns T if it succeeds, otherwise NIL."
197
(when-let (position (file-position stream))
198
(if (file-position stream (- position octets)) t nil)))
200
(defmacro logand* (x y)
201
"Solely for optimization purposes. Some Lisps need it, some don't."
202
`(the fixnum (logand ,x ,y)))
204
(defmacro logior* (x y)
205
"Solely for optimization purposes. Some Lisps need it, some don't."
206
`(the fixnum (logior ,x ,y)))
208
(defmacro ash* (integer count)
209
"Solely for optimization purposes. Some Lisps need it, some don't."
210
`(the fixnum (ash ,integer ,count)))
211
(defun get-multibyte-mapper (table code)
212
"this function is borrowed from sbcl's source file \"/src/code/external-formats/mb-util.lisp\",
213
it search char code in \"table\" with specified \"code\""
215
(declare (optimize speed (safety 0))
216
(type (array * (* 2)) table)
218
(labels ((recur (start end)
219
(declare (type fixnum start end))
220
(let* ((m (ash (+ start end) -1))
221
(x (aref table m 0)))
222
(declare (type fixnum m x))
225
((and (< x code) (< m end))
227
((and (> x code) (> m start))
228
(recur start (1- m)))))))
229
(recur 0 (1- (array-dimension table 0)))))