1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
<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, <ndw@nwalsh.com>
</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>
|