Re: any STL/string function available for determing string contains numeric
On Apr 5, 2:28 pm, Jacek Dziedzic
<jacek.dziedzic.n.o.s.p....@gmail.com> wrote:
James Kanze wrote:
Personally, I'd go with something like:
static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;
That's clever. I _think_ this should be extended with a
"Dd", though -- isn't it a valid exponent delimiter for double
precision values? Like 2.34D+56?
Not in C++. On the other hand, you can have suffixes which
specify the type more precisely: U, L or LL for integers, and F
or L for floating point. (Lower case is also accepted, and U
can combine with L or LL.) Thus, 3.14: double, 3.14F: float and
3.14L: long double. I don't know whether the original poster
wanted to accept these or not.
Note that it might be clearer if you specified some of the parts
as constant strings, and build the strings up, rather than
repeating things like "[Ee][+-]?[0-9]+" umpteam times. And
also, that my knowledge of regular expressions was acquired
many, many years ago, and hasn't been updated; I think that
Boost (and some others) accept things like \d for [0-9], \w for
[:space:], etc. (But don't forget that for regex to see the in a string literal, you have to double it.) Before using
something like this, I would strongly recommend reading the
documentation for the regular expression package you use.
Conversely, for the string "123" num may go eof
after reading and hence become invalid, while "123"
is a valid number.
The eofbit will be set after reading "123", but this doesn't
mean that the stream has failed. It only means that any further
attempt to read the stream will fail. (Note that std::ws is a
manipulator, which never "fails", i.e. it never sets failbit.)
Thanks for clarifying that, I was of the impression that
eof() automatically triggers fail().
Only at the start of conversion. Otherwise, think of the
problems: reading "123" into an int would fail.
The conditions in ios (the base class of istream and ostream)
aren't all that clear: the function good(), for example, does
consider eof(), so something like "if ( stream.good() )" after
an attempted read can fail, even though the read succeeded.
This is one of the reasons why one normally just uses the
conversion to bool (actualy, void*): it tests what you have to
test to know whether the conversion succeeded, and it doesn't
test anything else.
On top of that, the rules for unformatted input are somewhat
different, and the fact that the read "fails" doesn't mean that
it really failed. It only really failed if "! stream &&
stream.gcount() == 0".
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34