digitalmars.D.learn - Does string.isNumeric mean that parse!int will not throw?
- Cooler (6/6) Feb 20 2014 The code:
- Stanislav Blinov (3/9) Feb 20 2014 No. s may still contain data that is not convertible to int. For
- Cooler (4/14) Feb 20 2014 Is there any way to know that a string is convertible to a number
- bearophile (5/7) Feb 20 2014 I suggested to add it:
- Cooler (2/9) Feb 20 2014 Yeap. Thanks. Will wait for the enhancment.
- w0rp (16/33) Feb 20 2014 You could do this.
- Stanislav Blinov (6/10) Feb 20 2014 And what about +/- and U/L suffixes? Or, say, different base
- w0rp (5/15) Feb 20 2014 True, this doesn't cover those. Returning a
- Jesse Phillips (3/7) Feb 20 2014 Nope, integer overflow exception. Though I didn't check if it
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
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
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:Is there any way to know that a string is convertible to a number without throwing?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
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
On Thursday, 20 February 2014 at 19:37:08 UTC, bearophile wrote:Cooler:Yeap. Thanks. Will wait for the enhancment.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
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: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); }On Thursday, 20 February 2014 at 19:11:55 UTC, Cooler wrote:Is there any way to know that a string is convertible to a number without throwing?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
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
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:True, this doesn't cover those. Returning a Maybe/Option/Nullable/Whatever type from another function may be better.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
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