DSSSL Library Reference: Part of the Modular DocBook Stylesheet distribution
Prev |  | Next

# parse-measurement

## Name

parse-measurement -- Parse a string containing a measurement and return the magnitude and units

## Synopsis

    (parse-measurement measure)

## Description

Parse a string containing a measurement, e.g., `"3pi"` or `"2.5in"`, and return the magnitude and units: `(3 "pi")` or `(2.5 "in")`.

Either element of the list may be `#f` if the string cannot reasonably be parsed as a measurement. Leading and trailing spaces are ignored.

## Author

Norman Walsh, <ndw@nwalsh.com>

## Source Code

    (define (parse-measurement measure)
      ;; Parse a string containing a measurement and return the magnitude and units
      (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)))

Prev | Home | Next
find-first-char | Up | unit-conversion-alist