New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String.toInt produces "Ok NaN" for some inputs #919

Closed
TobiDo opened this Issue Nov 13, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@TobiDo

TobiDo commented Nov 13, 2017

String.toIntresults to Ok NaN for the arguments "+", "-", "0x" respectively.

See Ellie: https://ellie-app.com/mjSxjQHdka1/1

Here is the implementation of toInt from Elm/Kernel/String.js:

function _String_toInt(s)
{
	var len = s.length;

	// if empty
	if (len === 0)
	{
		return _String_intErr(s);
	}

	// if hex
	var c = s[0];
	if (c === '0' && s[1] === 'x')
	{
		for (var i = 2; i < len; ++i)
		{
			var c = s[i];
			if (('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f'))
			{
				continue;
			}
			return _String_intErr(s);
		}
		return __Result_Ok(parseInt(s, 16));
	}

	// is decimal
	if (c > '9' || (c < '0' && ((c !== '-' && c !== '+') || len === 1)))
	{
		return _String_intErr(s);
	}
	for (var i = 1; i < len; ++i)
	{
		var c = s[i];
		if (c < '0' || '9' < c)
		{
			return _String_intErr(s);
		}
	}

	return __Result_Ok(parseInt(s, 10));
}

I think the best solution would be to check whether parseInt returned NaN before returning an Ok-Result. One could, of course, also handle the three aforementioned cases explicitly.

@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Nov 13, 2017

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot commented Nov 13, 2017

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@zwilias

This comment has been minimized.

Show comment
Hide comment
@zwilias

zwilias Nov 13, 2017

Member

+ and - are covered by #834; 0x is one I hadn't heard about; but also seems fixable. That toInt implementation is starting to become pretty gnarly.

Member

zwilias commented Nov 13, 2017

+ and - are covered by #834; 0x is one I hadn't heard about; but also seems fixable. That toInt implementation is starting to become pretty gnarly.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Mar 7, 2018

Member

@zwilias, it sounds like some of this is resolved. If there is any left, open a new one with the relevant SSCCEs so I can figure out what is actually broken at the moment!

Member

evancz commented Mar 7, 2018

@zwilias, it sounds like some of this is resolved. If there is any left, open a new one with the relevant SSCCEs so I can figure out what is actually broken at the moment!

@evancz evancz closed this Mar 7, 2018

@zwilias

This comment has been minimized.

Show comment
Hide comment
@zwilias
Member

zwilias commented Mar 7, 2018

https://github.com/elm-lang/core/issues/949 SSCCE/overview created 👍

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