www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does string.isNumeric mean that parse!int will not throw?

reply "Cooler" <kulkin hotbox.ru> writes:
The code:

string s = "...";
if(s.isNumeric){
     auto x = parse!int(s); // Can I be sure here that parse will
not throw?
}
Feb 20 2014
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Thursday, 20 February 2014 at 19:11:55 UTC, Cooler wrote:
 The code:

 string s = "...";
 if(s.isNumeric){
     auto x = parse!int(s); // Can I be sure here that parse will
 not throw?
 }
No. s may still contain data that is not convertible to int. For example, "nan".
Feb 20 2014
parent reply "Cooler" <kulkin hotbox.ru> writes:
On Thursday, 20 February 2014 at 19:18:15 UTC, Stanislav Blinov
wrote:
 On Thursday, 20 February 2014 at 19:11:55 UTC, Cooler wrote:
 The code:

 string s = "...";
 if(s.isNumeric){
    auto x = parse!int(s); // Can I be sure here that parse will
 not throw?
 }
No. s may still contain data that is not convertible to int. For example, "nan".
Is there any way to know that a string is convertible to a number without throwing?
Feb 20 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Cooler:

 Is there any way to know that a string is convertible to a 
 number without throwing?
I suggested to add it: https://d.puremagic.com/issues/show_bug.cgi?id=6840 Bye, bearophile
Feb 20 2014
parent "Cooler" <kulkin hotbox.ru> writes:
On Thursday, 20 February 2014 at 19:37:08 UTC, bearophile wrote:
 Cooler:

 Is there any way to know that a string is convertible to a 
 number without throwing?
I suggested to add it: https://d.puremagic.com/issues/show_bug.cgi?id=6840 Bye, bearophile
Yeap. Thanks. Will wait for the enhancment.
Feb 20 2014
prev sibling parent reply "w0rp" <devw0rp gmail.com> writes:
On Thursday, 20 February 2014 at 19:23:28 UTC, Cooler wrote:
 On Thursday, 20 February 2014 at 19:18:15 UTC, Stanislav Blinov
 wrote:
 On Thursday, 20 February 2014 at 19:11:55 UTC, Cooler wrote:
 The code:

 string s = "...";
 if(s.isNumeric){
   auto x = parse!int(s); // Can I be sure here that parse will
 not throw?
 }
No. s may still contain data that is not convertible to int. For example, "nan".
Is there any way to know that a string is convertible to a number without throwing?
You could do this. import std.stdio; import std.algorithm: all; import std.ascii: isDigit; void main(string[] args) { writeln("123".all!isDigit); // true writeln("12x".all!isDigit); // false } Combine the "all" algorithm which returns true if everything in a range matches a predicate and use "isDigit" as the predicate which returns true if the character is an ascii digit. if (s.length > 0 && s.all!isDigit) { // Never throws now. auto x = parse!int(s); }
Feb 20 2014
next sibling parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Thursday, 20 February 2014 at 19:46:35 UTC, w0rp wrote:

 if (s.length > 0 && s.all!isDigit) {
      // Never throws now.
      auto x = parse!int(s);
 }
And what about +/- and U/L suffixes? Or, say, different base (i.e. hexadecimal)? It would be way more beneficial if Phobos' parse (or some additional function) returned some kind of Optional. Maybe a D implementation of Andrei's Expected<T>?
Feb 20 2014
parent "w0rp" <devw0rp gmail.com> writes:
On Thursday, 20 February 2014 at 19:58:10 UTC, Stanislav Blinov
wrote:
 On Thursday, 20 February 2014 at 19:46:35 UTC, w0rp wrote:

 if (s.length > 0 && s.all!isDigit) {
     // Never throws now.
     auto x = parse!int(s);
 }
And what about +/- and U/L suffixes? Or, say, different base (i.e. hexadecimal)? It would be way more beneficial if Phobos' parse (or some additional function) returned some kind of Optional. Maybe a D implementation of Andrei's Expected<T>?
True, this doesn't cover those. Returning a Maybe/Option/Nullable/Whatever type from another function may be better.
Feb 20 2014
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Thursday, 20 February 2014 at 19:46:35 UTC, w0rp wrote:
 if (s.length > 0 && s.all!isDigit) {
      // Never throws now.
      auto x = parse!int(s);
 }
Nope, integer overflow exception. Though I didn't check if it would actually through that, but it should.
Feb 20 2014