Is there a float equivalent to parse-integer? I can't find any reference to
one but thought I'd check before writing my own.
--
Ray Drew
CIA MediaSystems
London
·····@cia-group.com
"Ray Drew" <·····@cia-group.com> wrote:
>Is there a float equivalent to parse-integer? I can't find any reference to
>one but thought I'd check before writing my own.
>
>--
read would work in many cases - use something like this
(parse-float (str)
(handler-case
(read-from-string str nil)
(error (c)
;; return whatever you want here if str does not parse to a float
)))
that's my .02 - I've already used code like this in my application.
(for better or for worse :-)
hope this helps!
>Ray Drew
>CIA MediaSystems
>London
>·····@cia-group.com
···@no_spam.mindspring.com (Michael Tuchman) writes:
> "Ray Drew" <·····@cia-group.com> wrote:
>
> >Is there a float equivalent to parse-integer? I can't find any reference to
> >one but thought I'd check before writing my own.
>
> read would work in many cases - use something like this
>
> (parse-float (str)
> (handler-case
> (read-from-string str nil)
> (error (c)
> ;; return whatever you want here if str does not parse to a float
> )))
I recommend at least testing if the string is plausibly a float
to avoid trojan horses and/or result type errors.
e.g., something like the following might do. (By the way, I only
tested on a couple of examples and it might be buggy... use at your
own risk.)
(defun parse-float (string &key (start 0) end)
(check-type start fixnum "a string start position")
(check-type end (or null fixnum) "a string stop position")
(let ((start start) (stop (or end (length string))))
(declare (fixnum start stop))
(or (when (and (> (- stop start) 0)
(find (char string start) "+-.01234556789")
(loop for i from (the fixnum (1+ start)) below stop
always (find (char string i) "0123456789.efsdl")))
(let ((result (read-from-string string nil nil :start start :end stop)))
(if (floatp result)
result nil)))
(error "Not float syntax: ~S" string))))
You might or might not want to check that (< (count #\. string) 2) and
(loop for i from start below stop thereis (digit-char-p (char string i)))
but since read-from-string will redundantly test that, I didn't bother.
It just risks some extra consing if read-from-string does it because it
will likely intern a symbol in many such cases, slightly bloating memory
in a non-GC-able way for each such error. (If you were paranoid about
this, you could bind package to a throwaway package and unintern any symbol
results.)
Btw, I agree that computationally this is really gonna slow a lot of things down.
(And probably you should signal an error of an appropriate condition class, not
just call error with an error message string.)
FWIW, I'm firmly aware that this is all gross. I've had to do it for
several things I've worked on and it makes me horribly sick to my
stomach every time I do it. A built-in parse-float (especially since the
code is there and just needs an entry point!) is on my to-request list
for next round standardization.
* Ray Drew
| Is there a float equivalent to parse-integer? I can't find any reference to
| one but thought I'd check before writing my own.
I'd recommend implementing your own based on `read-from-string'.
unfortunately, there's no hook (standard or otherwise, to my knowledge)
into the decision process after a string of characters has been read as a
token (of constituent characters), so it's hard to bail out before it is
interpreted as a symbol if it is not a number, so you might want to check
the set of characters that this string consists of before you call
`read-from-string'.
#\Erik
--
If you think this year is number 97, | Help fight MULE in GNU Emacs 20!
_you_ are not "Year 2000 Compliant". | http://sourcery.naggum.no/emacs/
Ray Drew (·····@cia-group.com) wrote:
: Is there a float equivalent to parse-integer? I can't find any reference to
: one but thought I'd check before writing my own.
read-from-string
P.