xsl:number fixes: no prefix and suffix when NaN is printed
[xuriella.git] / number.lisp
blob38bb797d33e5e3725f8c56b611bdc4c1250c5b22
1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
3 ;;; Copyright (c) 2007,2008 David Lichteblau, Ivan Shvedunov.
4 ;;; All rights reserved.
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.
30 (in-package :xuriella)
32 (define-instruction xsl:number (args env)
33 (destructuring-bind (&key level count from value format lang letter-value
34 grouping-separator grouping-size)
35 args
36 (let ((count (and count (compile-pattern count env)))
37 (from (and from (compile-pattern from env)))
38 (value (and value (compile-xpath value env)))
39 (format (compile-avt (or format "1") env))
40 (lang (compile-avt (or lang "") env))
41 (letter-value (compile-avt (or letter-value "foo") env))
42 (grouping-separator
43 (and grouping-separator (compile-avt grouping-separator env)))
44 (grouping-size (and grouping-size (compile-avt grouping-size env))))
45 (lambda (ctx)
46 (let ((value (when value
47 (let ((x
48 (xpath:number-value
49 (funcall value ctx))))
50 (if (xpath::nan-p x)
52 (round x)))))
53 (format (funcall format ctx))
54 (lang (funcall lang ctx))
55 (letter-value (funcall letter-value ctx))
56 (grouping-separator (when grouping-separator
57 (funcall grouping-separator ctx)))
58 (grouping-size (when grouping-size
59 (xpath:number-value
60 (funcall grouping-size ctx)))))
61 (write-text
62 (format-number-list
63 (if value
64 (list value)
65 (compute-number-list (or level "single")
66 (xpath::context-node ctx)
67 count
68 from))
69 format
70 lang
71 letter-value
72 grouping-separator
73 grouping-size)))))))
75 (defun compile-pattern (str env)
76 (compile-xpath
77 `(xpath:xpath
78 (:union
79 ,@(mapcar #'naive-pattern-expression (parse-pattern str))))
80 env))
82 (defun pattern-thunk-matches-p (pattern-thunk node)
83 (find node
84 (xpath:all-nodes (funcall pattern-thunk (xpath:make-context node)))))
86 (defun ancestors-using-count-and-from (node count from)
87 (let ((ancestors
88 (xpath::force
89 (funcall (xpath::axis-function :ancestor-or-self) node))))
90 (remove-if-not (lambda (ancestor)
91 (pattern-thunk-matches-p count ancestor))
92 (if from
93 (loop
94 for a in ancestors
95 when (pattern-thunk-matches-p from a)
96 do (return result)
97 collect a into result
98 finally (return ancestors))
99 ancestors))))
101 (defun node-position-among-siblings (node count)
103 (count-if (lambda (sibling)
104 (pattern-thunk-matches-p count sibling))
105 (xpath::force
106 (funcall (xpath::axis-function :preceding-sibling) node)))))
108 (defun compute-number-list (level node count from)
109 (unless count
110 (setf count
111 (let ((uri (xpath-protocol:namespace-uri node))
112 (lname (xpath-protocol:local-name node)))
113 (lambda (ctx)
114 (let ((node (xpath:context-node ctx)))
115 (xpath-sys:make-node-set
116 (if (and (xpath-protocol:node-type-p node :element)
117 (equal (xpath-protocol:namespace-uri node) uri)
118 (equal (xpath-protocol:local-name node) lname))
119 (list node)
120 nil)))))))
121 (cond
122 ((equal level "single")
123 (let ((ancestor (car (ancestors-using-count-and-from node count from))))
124 (if ancestor
125 (list (node-position-among-siblings ancestor count))
126 nil)))
127 ((equal level "multiple")
128 (mapcar (lambda (ancestor)
129 (node-position-among-siblings ancestor count))
130 (reverse
131 (ancestors-using-count-and-from node count from))))
132 ((equal level "any")
133 (destructuring-bind (root)
134 (xpath::force (funcall (xpath::axis-function :root) node))
135 (let ((nodes (xpath::force
136 (xpath::append-pipes
137 (xpath::subpipe-before
138 node
139 (funcall (xpath::axis-function :descendant-or-self) root))
140 (list node)))))
141 (when from
142 (loop
143 for (current . rest) on nodes
144 when (pattern-thunk-matches-p from current)
146 (setf nodes rest)))
147 (list
148 (loop
149 for n in nodes
150 count (pattern-thunk-matches-p count n))))))
152 (xslt-error "invalid number level: ~A" level))))
154 (xpath::deflexer (format-lexer :ignore-whitespace nil)
155 ;; zzz just enough unicode "support" here to pass the tests
156 (#.(format nil "([a-zA-Z0-9~A]+)" (code-char 945)) (x) (values :format x))
157 (#.(format nil "([^a-zA-Z0-9~A]+)" (code-char 945)) (x) (values :text x)))
159 (defun format-number-token (str n)
160 (cond
161 ((or (equal str "a")
162 (equal str "A")
163 ;; zzz just enough unicode "support" here to pass the tests
164 (equal str #.(string (code-char 945))))
165 (let ((start (char-code (elt str 0))))
166 (when (zerop n)
167 (xslt-error "cannot format zero"))
168 (nreverse
169 (with-output-to-string (r)
170 (loop
171 for m = n then rest
172 for (rest digit) = (multiple-value-list (truncate m 26))
174 (cond
175 ((plusp rest)
176 (write-char (code-char (+ start digit)) r))
178 (write-char (code-char (+ start digit -1)) r)
179 (return))))))))
180 ((equal str "i")
181 (if (zerop n)
183 (format nil "~(~@R~)" n)))
184 ((equal str "I")
185 (if (zerop n)
187 (format nil "~@R" n)))
189 (unless (cl-ppcre:all-matches "^0*1$" str)
190 ;; unsupported format
191 (setf str "1"))
192 (format nil "~v,'0D" (length str) n))))
194 (defun group-numbers (str separator size stream)
195 (loop
196 for c across str
197 for i from (1- (length str)) downto 0
199 (write-char c stream)
200 (when (and (zerop (mod i size)) (plusp i))
201 (write-string separator stream))))
203 ;;; fixme: unicode support
204 (defun format-number-list
205 (list format lang letter-value grouping-separator grouping-size)
206 (declare (ignore lang letter-value))
207 (if (some #'xpath::nan-p list)
208 "NaN"
209 (multiple-value-bind (prefix pairs suffix)
210 (parse-number-format format)
211 (with-output-to-string (s)
212 (write-string prefix s)
213 (loop
214 for (separator . subformat) in pairs
215 for n in list
216 for formatted = (format-number-token subformat n)
218 (when separator
219 (write-string separator s))
220 (if (and grouping-separator
221 grouping-size)
222 (group-numbers formatted
223 grouping-separator
224 grouping-size
226 (write-string formatted s)))
227 (write-string suffix s)))))
229 (defun parse-number-format (format)
230 (let ((lexer (format-lexer format))
231 (prefix "")
232 (conses '())
233 (suffix "")
234 (current-text nil))
235 (loop
236 (multiple-value-bind (type str) (funcall lexer)
237 (ecase type
238 ((nil :eof)
239 (return))
240 (:text
241 (if conses
242 (setf current-text str)
243 (setf prefix str)))
244 (:format
245 (push (cons (if conses
246 (or current-text ".")
247 nil)
248 str)
249 conses)
250 (setf current-text nil)))))
251 (when current-text
252 (setf suffix current-text))
253 (unless conses
254 (setf conses (list (cons nil "1"))))
255 (let* ((tail-cons (car conses))
256 (tail (if (car tail-cons)
257 conses
258 (push (cons "." (cdr tail-cons)) conses))))
259 (setf conses (nreverse conses))
260 (setf (cdr tail) tail))
261 (values prefix conses suffix)))