Repository navigation
Saturate a String's number past an Int when a Dynamic is taken as an Int - #514
Open
dimensionscape wants to merge 1 commit into
Open
dimensionscape wants to merge 1 commit into
dimensionscape wants to merge 1 commit into
Conversation
A String in a Dynamic, taken as an Int (var i:Int = value, an Int argument, field or array element, or an Int return), converted through the C library's atoi, whose result for a number past the Int range is undefined. The C libraries differ there: MSVC's saturates, while glibc's and Apple's read into a 64-bit long and truncate it, so "10000000000" gave 2147483647 on Windows and 1410065408 on Linux and macOS, and "4294967296" gave 0. Compiled code, the cppia interpreter and the JIT all reach the same conversion, so each platform agreed with itself and not with the others. The conversion reads what atoi reads, the spaces isspace has in the C locale, a sign and decimal digits, and gives 0 when there are none, so every number in the range and every String with no number converts as before. A number past the range saturates to it now, on every platform, as a decimal number already does in Std.parseInt. Leading zeros do not count toward the range. DynamicStringToInt takes Strings as Ints five ways, as a local, an argument, a field, an array element and a return, in compiled code and as a script with the JIT off and on, and checks they agree: numbers in the range, atoi's spaces, signs and trailing text, no hexadecimal, numbers past the range either way, leading zeros, Strings made from large numbers, and UTF-16 Strings. On Windows its cases passed before this as well, since MSVC's atoi saturates; CI on Linux and macOS showed the wrapped values in libhxcpp #500's CppiaJitDynamicNumbers. Tests: DynamicStringToInt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A String in a Dynamic, taken as an Int, went through the C library's
atoi, whose result for a number past the Int range is undefined. The C libraries differ there: MSVC's saturates, while glibc's and Apple's read into a 64-bitlongand truncate it. So"10000000000"gave 2147483647 on Windows and 1410065408 on Linux and macOS, and"4294967296"gave 0. Compiled code, the cppia interpreter and the JIT all reach the sameStringData::__ToInt, so each platform agreed with itself and not with the others. It showed up in #500, where CppiaJitDynamicNumbers printed different values on CI's Linux and macOS jobs than on Windows.The conversion now reads what
atoireads: the spacesisspacehas in the C locale (whatever the process locale is), a sign and decimal digits, and 0 when there are none. A number past the range saturates to it on every platform, as a decimal number already does inStd.parseInt. Leading zeros do not count toward the range. Every number in the range and every String with no number converts as before, so the only results that change are out-of-range ones, and only off Windows.Tests
New
DynamicStringToInttakes Strings as Ints five ways (a local, an argument, a field, an array element and a return), in compiled code and as a script with the JIT off and on, and checks they agree. Its cases cover numbers in the range, atoi's spaces, signs and trailing text, no hexadecimal, numbers past the range either way, leading zeros, Strings made from large numbers, and UTF-16 Strings.On Windows its cases passed before this as well, since MSVC's
atoisaturates. Breaking the new conversion on purpose four ways each fails it: wrapping through a 64-bit integer as glibc does, accumulating without the guard past the range, skipping only plain spaces, and saturating negatives one short.On Windows: DynamicStringToInt, test/haxe (2,880) and test/std (696). Other platforms are left to CI.