Skip to content

Comments

Small clean-up in std.conv.parse#5067

Merged
WalterBright merged 1 commit intodlang:masterfrom
JackStouffer:patch-21
Feb 9, 2017
Merged

Small clean-up in std.conv.parse#5067
WalterBright merged 1 commit intodlang:masterfrom
JackStouffer:patch-21

Conversation

@JackStouffer
Copy link
Contributor

Removed gotos and used enforce which IMO is more in keeping with D style.

std/conv.d Outdated
import core.checkedint : mulu, addu;
import std.exception : enforce;

enforce!ConvException(!s.empty, "s must not be empty in integral parse");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.empty is being called twice, and the logic has been moved around parse!Target(s) - is that significant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the logic because atStart was just a poor-man's range.empty. It makes much more sense to do sanity checks on input at the start of the function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the check is now before lines 2344 and 2345, whereas it used to be after.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That function also throws on empty, see line 2007

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So when parse is called, the check is done twice. I suggest moving the check back to where it was before.

std/conv.d Outdated
Target v = 0;
bool atStart = true;

for (; !s.empty; s.popFront())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing this to do..while will eliminate the redundant s.empty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will look into this

@JackStouffer
Copy link
Contributor Author

@WalterBright Done

@JackStouffer
Copy link
Contributor Author

@WalterBright Fixed

@WalterBright
Copy link
Member

Auto-merge toggled on

@WalterBright WalterBright merged commit 2935ae0 into dlang:master Feb 9, 2017
@JackStouffer JackStouffer deleted the patch-21 branch February 12, 2017 16:43
auto nextv = v.mulu(radix, overflow).addu(c - '0', overflow);
if (overflow || nextv > Target.max)
goto Loverflow;
enforce!ConvOverflowException(!overflow && nextv < Target.max, "Overflow in integral conversion");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should have been <=?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CyberShadow
Copy link
Member

to!ubyte("ff", 16); now throws ConvOverflowException

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants