From: Gisle Aas Date: Fri, 14 Feb 1997 17:08:52 +0000 (+0100) Subject: Better looks_like_number() function [sv.c] X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8dbaa58ee2aba7cc22d84199a674c58bbf108b46;p=p5sagit%2Fp5-mst-13.2.git Better looks_like_number() function [sv.c] The old looks_like_number() function was too forgiving. It classified strings like these as numbers: "+" "+e+" "e3" "." With this patch perl -lwe 'print int(".")' prints: Argument "." isn't numeric in int at -e line 1. 0 as it should and perl still passes all its tests. p5p-msgid: <199702141708.SAA17546@bergen.sn.no> --- diff --git a/sv.c b/sv.c index 528afd9..2ea1990 100644 --- a/sv.c +++ b/sv.c @@ -1504,7 +1504,7 @@ SV *sv; register char *s; register char *send; register char *sbegin; - I32 numtype = 1; + I32 numtype; STRLEN len; if (SvPOK(sv)) { @@ -1520,31 +1520,53 @@ SV *sv; s = sbegin; while (isSPACE(*s)) s++; - if (s >= send) - return 0; if (*s == '+' || *s == '-') s++; - while (isDIGIT(*s)) - s++; - if (s == send) - return numtype; - if (*s == '.') { - numtype = 1; - s++; + + /* next must be digit or '.' */ + if (isDIGIT(*s)) { + do { + s++; + } while (isDIGIT(*s)); + if (*s == '.') { + s++; + while (isDIGIT(*s)) /* optional digits after "." */ + s++; + } } - else if (s == SvPVX(sv)) - return 0; - while (isDIGIT(*s)) - s++; - if (s == send) - return numtype; + else if (*s == '.') { + s++; + /* no digits before '.' means we need digits after it */ + if (isDIGIT(*s)) { + do { + s++; + } while (isDIGIT(*s)); + } + else + return 0; + } + else + return 0; + + /* + * we return 1 if the number can be converted to _integer_ with atol() + * and 2 if you need (int)atof(). + */ + numtype = 1; + + /* we can have an optional exponent part */ if (*s == 'e' || *s == 'E') { numtype = 2; s++; if (*s == '+' || *s == '-') s++; - while (isDIGIT(*s)) - s++; + if (isDIGIT(*s)) { + do { + s++; + } while (isDIGIT(*s)); + } + else + return 0; } while (isSPACE(*s)) s++;