Check apply-templates child nodes
[xuriella.git] / format-number.lisp
blobd9fe32a610bbaada81d589e476f352ed467e5d93
1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
3 ;;; Copyright (c) 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 (defstruct (decimal-format (:conc-name "DF/"))
33 ;; picture string and output syntax:
34 (decimal-separator #\.) ;active
35 (grouping-separator #\,) ;active
36 (zero-digit #\0) ;active
37 (percent #\%)
38 (per-mille (code-char #x2030))
40 ;; picture string syntax only
41 (digit #\#) ;active
42 (pattern-separator #\;) ;active
44 ;; output syntax only:
45 (infinity "Infinity")
46 (nan "NaN")
47 (minus-sign #\-))
49 (defstruct (picture (:conc-name "PIC/"))
50 percentp
51 per-mille-p
52 prefix
53 suffix
54 integer-part-grouping-positions
55 minimum-integer-part-size
56 fractional-part-grouping-positions
57 minimum-fractional-part-size
58 maximum-fractional-part-size)
60 (defun df/active-characters (df)
61 (format nil "~C~C~C~C~C"
62 (df/decimal-separator df)
63 (df/grouping-separator df)
64 (df/zero-digit df)
65 (df/digit df)
66 (df/pattern-separator df)))
68 (defun df/digits (df)
69 (let ((result (make-array 10))
70 (start (char-code (df/zero-digit df))))
71 (loop
72 for i from 0 below 10
74 (setf (elt result i) (code-char (+ start i))))
75 result))
77 (defun find-decimal-format (lname uri stylesheet &optional (errorp t))
78 (or (gethash (cons lname uri)
79 (stylesheet-decimal-formats stylesheet))
80 (when errorp
81 (xslt-error "decimal format not found: ~A/~A" lname uri))))
83 (defun (setf find-decimal-format) (newval lname uri stylesheet)
84 (setf (gethash (cons lname uri)
85 (stylesheet-decimal-formats stylesheet))
86 newval))
88 (xpath-sys:define-xpath-function/lazy
89 xslt :format-number
90 (value picture &optional format-name)
91 (let ((namespaces *namespaces*))
92 (lambda (ctx)
93 (let ((df
94 (if format-name
95 (let ((qname (funcall format-name ctx)))
96 (multiple-value-bind (local-name uri)
97 (decode-qname/runtime qname namespaces nil)
98 (find-decimal-format local-name
99 (or uri "")
100 *stylesheet*)))
101 (find-decimal-format "" "" *stylesheet*))))
102 (multiple-value-bind (pos neg)
103 (parse-picture (xpath:string-value (funcall picture ctx)) df)
104 (format-number (float (xpath:number-value (funcall value ctx))
105 1.0d0)
108 df))))))
110 (defun test-format-number (value picture)
111 (let ((df (make-decimal-format)))
112 (multiple-value-bind (pos neg)
113 (parse-picture picture df)
114 (format-number value pos neg df))))
116 (defun parse-picture (picture df)
117 (destructuring-bind (&optional positive negative &rest erroneous)
118 (split-sequence:split-sequence
119 (df/pattern-separator df)
120 picture)
121 (unless (and positive (not erroneous))
122 (xpath:xpath-error "invalid pattern separators"))
123 (unless negative
124 (setf negative (concatenate 'string
125 (string (df/minus-sign df))
126 positive)))
127 (values (parse-sub-picture positive df)
128 (parse-sub-picture negative df))))
130 (defmacro df/case (df form &rest clauses)
131 `(let ((.form ,form)
132 (.df ,df))
133 (cond
134 ,@(loop
135 for (accessor . body) in clauses
136 collect `((eql (,accessor .df) .form) ,@body)))))
138 (defun parse-integer-picture (picture df start end)
139 (let ((integer-part-grouping-positions '())
140 (minimum-integer-part-size 0))
141 (loop
142 for i from start below end
143 for c = (elt picture i)
144 until (eql c (df/decimal-separator df))
146 (df/case df c
147 (df/grouping-separator
148 (push 0 integer-part-grouping-positions))
149 (df/digit
150 (when integer-part-grouping-positions
151 (incf (car integer-part-grouping-positions))))
152 (df/zero-digit
153 (when integer-part-grouping-positions
154 (incf (car integer-part-grouping-positions)))
155 (incf minimum-integer-part-size)))
156 finally
157 (when integer-part-grouping-positions
158 ;; zzz I wrote the above algorithm based on the XSLT 2.0 spec,
159 ;; only to find out that the test suite doesn't want
160 ;; multiple INTEGER-PART-GROUPING-POSITIONS. Sun says
161 ;; that only the last one is used:
162 ;; http://java.sun.com/j2se/1.3/docs/api/java/text/DecimalFormat.html
163 (setf integer-part-grouping-positions
164 (list (car integer-part-grouping-positions))))
165 (return (values i
166 (loop
167 for pos in integer-part-grouping-positions
168 for accum = pos then (+ accum pos)
169 collect accum)
170 minimum-integer-part-size)))))
172 (defun parse-fractional-picture (picture df start end)
173 (let ((fractional-part-grouping-positions '())
174 (minimum-fractional-part-size 0)
175 (maximum-fractional-part-size 0)
176 (current-grouping 0))
177 (loop
178 for i from start below end
179 for c = (elt picture i)
181 (df/case df c
182 (df/grouping-separator
183 (push current-grouping fractional-part-grouping-positions))
184 (df/digit
185 (incf current-grouping)
186 (incf maximum-fractional-part-size))
187 (df/zero-digit
188 (incf current-grouping)
189 (incf minimum-fractional-part-size)
190 (incf maximum-fractional-part-size))
191 (df/decimal-separator))
192 finally
193 (return (values (nreverse fractional-part-grouping-positions)
194 minimum-fractional-part-size
195 maximum-fractional-part-size)))))
197 (defun parse-sub-picture (picture df)
198 (let ((active (df/active-characters df)))
199 (flet ((activep (x) (find x active)))
200 (let ((start (position-if #'activep picture))
201 (last (position-if #'activep picture :from-end t)))
202 (unless start
203 (xpath:xpath-error "no digit-sign or zero-digit sign found"))
204 (let* ((end (1+ last))
205 (result (make-picture
206 :percentp (find (df/percent df) picture)
207 :per-mille-p (find (df/per-mille df) picture)
208 :prefix (subseq picture 0 start)
209 :suffix (subseq picture end))))
210 (setf (values start
211 (pic/integer-part-grouping-positions result)
212 (pic/minimum-integer-part-size result))
213 (parse-integer-picture picture df start end))
214 (setf (values (pic/fractional-part-grouping-positions result)
215 (pic/minimum-fractional-part-size result)
216 (pic/maximum-fractional-part-size result))
217 (parse-fractional-picture picture df start end))
218 result)))))
220 (defun format-number (value positive-picture negative-picture df)
221 (if (xpath::nan-p value)
222 (df/nan df)
223 (let ((picture (if (minusp value) negative-picture positive-picture)))
224 (if (xpath::inf-p value)
225 (concatenate 'string
226 (pic/prefix picture)
227 (df/infinity df)
228 (pic/suffix picture))
229 (format-ordinary-number value picture df)))))
231 (defun format-number-~f (number picture df)
232 (let* ((str (format nil "~,vF"
233 (pic/maximum-fractional-part-size picture)
234 number))
235 (str (string-trim (string (df/zero-digit df)) str)) ;for 0.0
236 (digits (df/digits df)))
237 (map 'string
238 (lambda (x)
239 (if (eql x #\.)
240 (df/decimal-separator df)
241 (elt digits (- (char-code x) #.(char-code #\0)))))
242 str)))
244 (defun make-grouping-test (positions)
245 (if (and positions
246 (let ((first (car positions)))
247 (loop
248 for expected = first then (+ expected first)
249 for pos in positions
250 always (eql pos expected))))
251 (let ((first (car positions)))
252 (lambda (x)
253 (and (plusp x) (zerop (mod x first)))))
254 (lambda (x)
255 (and (plusp x) (find x positions)))))
257 (defun format-ordinary-number (value picture df)
258 (let* ((adjusted-number
259 (cond
260 ((pic/percentp picture)
261 (* value 100))
262 ((pic/per-mille-p picture)
263 (* value 1000))
265 value)))
266 (str (format-number-~f (abs adjusted-number) picture df))
267 (left (position (df/decimal-separator df) str))
268 (right (1- (- (length str) left)))
269 (wanted-left (max left (pic/minimum-integer-part-size picture)))
270 (wanted-right (max right (pic/minimum-fractional-part-size picture)))
271 (zero (df/zero-digit df))
272 (left-test (make-grouping-test
273 (pic/integer-part-grouping-positions picture)))
274 (right-test (make-grouping-test
275 (pic/fractional-part-grouping-positions picture))))
276 (with-output-to-string (s)
277 (write-string (pic/prefix picture) s)
278 (loop
279 for i from (1- wanted-left) downto 0
280 for index from (- left wanted-left)
282 (if (< i left)
283 (write-char (elt str index) s)
284 (write-char zero s))
285 (when (funcall left-test i)
286 (write-char (df/grouping-separator df) s)))
287 (when (plusp wanted-right)
288 (write-char (df/decimal-separator df) s)
289 (loop
290 for i from 0 below wanted-right
291 for index from (+ left 1)
293 (when (funcall right-test i)
294 (write-char (df/grouping-separator df) s))
295 (if (< i right)
296 (write-char (elt str index) s)
297 (write-char zero s))))
298 (write-string (pic/suffix picture) s))))