(parse-measurement measure) |
Returns a two-item list of the magnitude and units components of measure. If measure is not correctly formed, one or both of the magnitude and units will be returned as #f.
(define (parse-measurement measure)
;; Parses measurement and returns '(magnitude units). Either may be #f
;; if measure is not a correctly formatted measurement. Leading and
;; trailing spaces are skipped.
(let* ((magstart (find-first-char measure " " "0123456789."))
(unitstart (find-first-char measure " 0123456789." ""))
(unitend (find-first-char measure "" " " unitstart))
(magnitude (if (< magstart 0)
#f
(if (< unitstart 0)
(substring measure
magstart
(string-length measure))
(substring measure magstart unitstart))))
(unit (if (< unitstart 0)
#f
(if (< unitend 0)
(substring measure
unitstart
(string-length measure))
(substring measure unitstart unitend)))))
(list magnitude unit)) |