<RefEntry id="parse-measurement">
<!-- This file is generated automatically from the DSSSL source. -->
<!-- Do not edit this file! -->
<?html-filename parse-measurement.html>

<RefMeta>
  <RefEntryTitle>parse-measurement</RefEntryTitle>
  <RefMiscInfo Role="file">dblib.dsl</RefMiscInfo>
</RefMeta>

<RefNameDiv>
  <RefName>parse-measurement</RefName>
  <RefPurpose>Parse a string containing a measurement and return the magnitude and units</RefPurpose>
</RefNameDiv>

<RefSynopsisDiv><Title>Synopsis</Title>
<Synopsis>
(parse-measurement measure)
</Synopsis>
</RefSynopsisDiv>

<RefSect1><Title>Description</Title>

<para>
Parse a string containing a measurement, e.g., <literal>"3pi"</literal> or <literal>"2.5in"</literal>,
and return the magnitude and units: <literal>(3 "pi")</literal> or <literal>(2.5 "in")</literal>.
</para>
<para>
Either element of the list may be <literal>#f</literal> if the string cannot reasonably
be parsed as a measurement.  Leading and trailing spaces are ignored.</para>


</RefSect1>

<RefSect1><Title>Author</Title>

<para>
Norman Walsh, &lt;ndw@nwalsh.com&gt;
</para>
</RefSect1>
<RefSect1><Title>Source Code</Title>

<ProgramListing>
(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)))
</ProgramListing>
</RefSect1>

</RefEntry>
