]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/decimals.lisp
Implement XTM association and role core with original conformance evidence
[topic-maps.git] / src / tmdm / decimals.lisp
1 (in-package #:topic-maps.tmdm)
2
3 (defun decimal-datatype () "http://www.w3.org/2001/XMLSchema#decimal")
4
5 (defun decimal-lexical-bounds (value)
6 "XSD 3.2.3.1/4.3.6: optional sign, ASCII digits and at most one decimal point.
7 At least one digit is required. XML whitespace is collapsed for interpretation;
8 internal whitespace remains invalid. No numeric conversion or model mutation."
9 (let* ((start (position-if-not (lambda (c) (find c '(#\Space #\Tab #\Return #\Newline))) value))
10 (end (when start (1+ (position-if-not (lambda (c) (find c '(#\Space #\Tab #\Return #\Newline))) value :from-end t))))
11 (negative (and start (char= (char value start) #\-)))
12 (digits (when start (if (find (char value start) "+-") (1+ start) start)))
13 (point (when digits (position #\. value :start digits :end end))))
14 (when (and digits (< digits end)
15 (> (- end digits (if point 1 0)) 0)
16 (loop for i from digits below end
17 always (or (and point (= i point)) (find (char value i) "0123456789"))))
18 (values digits end point negative))))
19
20 (defun decimal-lexical-p (value)
21 "Whether VALUE is an xsd:decimal lexical form, including XML whitespace.
22 XSD permits integer-looking forms and an empty fractional component, unlike
23 CTM's bare-decimal production. The CTM lexer still decides token boundaries."
24 (check-type value string)
25 (not (null (decimal-lexical-bounds value))))
26
27 (defun canonical-decimal-lexical-form (value)
28 "Pure character-based XSD 3.2.3.2 canonical form, or NIL for an invalid value.
29 No plus/unnecessary zeros; required integer/fraction digits; zero is 0.0.
30 Arbitrary finite input length needs no floating-point or machine-word value."
31 (check-type value string)
32 (multiple-value-bind (start end point negative) (decimal-lexical-bounds value)
33 (when start
34 (let* ((integer-end (or point end))
35 (integer-start (position-if-not (lambda (c) (char= c #\0)) value :start start :end integer-end))
36 (fraction-start (if point (1+ point) end))
37 (fraction-end (position-if-not (lambda (c) (char= c #\0)) value :start fraction-start :end end :from-end t)))
38 (concatenate 'string
39 (if (and negative (or integer-start fraction-end)) "-" "")
40 (if integer-start (subseq value integer-start integer-end) "0") "."
41 (if fraction-end (subseq value fraction-start (1+ fraction-end)) "0"))))))