]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/integers.lisp
Implement Date Occurrences with lexical validation and CXTM canonicalization
[topic-maps.git] / src / tmdm / integers.lisp
1 (in-package #:topic-maps.tmdm)
2
3 (defun integer-datatype () "http://www.w3.org/2001/XMLSchema#integer")
4
5 (defun integer-lexical-bounds (value)
6 "XSD 3.3.13.1/4.3.6: ASCII sign/digits after XML whitespace collapse.
7 Internal whitespace cannot be part of an integer, so trimming the XML whitespace
8 at the two ends is sufficient to validate this datatype's collapsed form."
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 (when (and digits (< digits end)
14 (loop for i from digits below end always (find (char value i) "0123456789")))
15 (values digits end negative))))
16
17 (defun integer-lexical-p (value)
18 "Whether VALUE is an xsd:integer lexical form (including its whitespace facet).
19 This does not change the string stored in any TMDM information item."
20 (check-type value string)
21 (not (null (integer-lexical-bounds value))))
22
23 (defun canonical-integer-lexical-form (value)
24 "Return the canonical xsd:integer string, or NIL for an invalid lexical form.
25 Pure character processing: no machine-word bounds, Lisp reader, or model mutation.
26 XSD 3.3.13.2: no plus/leading zeroes; the zero value is represented as 0."
27 (check-type value string)
28 (multiple-value-bind (start end negative) (integer-lexical-bounds value)
29 (when start
30 (let ((nonzero (position-if-not (lambda (c) (char= c #\0)) value :start start :end end)))
31 (cond ((null nonzero) "0")
32 (negative (concatenate 'string "-" (subseq value nonzero end)))
33 (t (subseq value nonzero end)))))))