-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
fromString
roughly symmetrical to toString
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,60 +20,54 @@ | |
</style> | ||
<emu-clause id="sec-bigint-fromstring-string-radix"> | ||
<h1>BigInt.fromString ( _string_, _radix_ )</h1> | ||
<p>The `BigInt.fromString` function produces an integer value dictated by interpretation of the contents of the _string_ argument according to the specified _radix_. Leading white space in _string_ is ignored. If _radix_ is *undefined* or 0, it is assumed to be 10.</p> | ||
<p>The `BigInt.fromString` function produces an numeric value dictated by interpretation of the contents of the _string_ argument according to the specified _radix_. If _radix_ is *undefined*, it is assumed to be 10.</p> | ||
<p>When the `BigInt.fromString` function is called, the following steps are taken:</p> | ||
<emu-alg> | ||
1. Let _inputString_ be ? ToString(_string_). | ||
1. Let _S_ be a newly created substring of _inputString_ consisting of the first code unit that is not a |StrWhiteSpaceChar| and all code units following that code unit. (In other words, remove leading white space.) If _inputString_ does not contain any such code unit, let _S_ be the empty string. | ||
1. Let _S_ be ? ToString(_string_). | ||
1. If _S_ is empty, throw a *SyntaxError* exception. | ||
1. Let _sign_ be 1. | ||
1. If _S_ is not empty and the first code unit of _S_ is the code unit 0x002D (HYPHEN-MINUS), let _sign_ be -1. | ||
1. If _S_ is not empty and the first code unit of _S_ is the code unit 0x002B (PLUS SIGN) or the code unit 0x002D (HYPHEN-MINUS), remove the first code unit from _S_. | ||
1. If the first code unit of _S_ is the code unit 0x002D (HYPHEN-MINUS), then | ||
1. Let _sign_ be -1. | ||
1. Remove the first code unit from _S_. | ||
1. If _S_ is empty, throw a *SyntaxError* exception. | ||
1. If _R_ = 0, throw a *RangeError* exception. | ||
1. Let _R_ be ? ToInt32(_radix_). | ||
1. If _R_ = 0, then | ||
1. Let _R_ be 10. | ||
1. Else, | ||
1. Assert: _R_ ≠ 0. | ||
1. If _R_ < 2 or _R_ > 36, throw a *RangeError* exception. | ||
1. If _S_ contains a code unit that is not a radix-_R_ digit, then | ||
1. Let _Z_ be the substring of _S_ consisting of all code units before the first such code unit. | ||
1. Else, | ||
1. Let _Z_ be _S_. | ||
1. If _Z_ is empty, throw a *SyntaxError* exception. | ||
1. Let _mathInt_ be the mathematical integer value that is represented by _Z_ in radix-_R_ notation, using the letters <b>A</b>-<b>Z</b> and <b>a</b>-<b>z</b> for digits with values 10 through 35. | ||
1. If _S_ represents a mathematical integer value in radix-_R_ notation, using the letters <b>A</b>-<b>Z</b> and <b>a</b>-<b>z</b> for digits with values 10 through 35, then | ||
1. Let _mathInt_ be that mathematical integer value. | ||
1. Else, throw a *SyntaxError* exception. | ||
1. Let _number_ be _sign_ × _mathInt_. | ||
1. Return the BigInt value for _number_. | ||
</emu-alg> | ||
<emu-note> | ||
<p>`BigInt.fromString` may interpret only a leading portion of _string_ as an integer value; it ignores any code units that cannot be interpreted as part of the notation of an integer, and no indication is given that any such code units were ignored.</p> | ||
</emu-note> | ||
</emu-clause> | ||
<hr> | ||
<emu-clause id="sec-number-fromstring-string-radix"> | ||
<h1>Number.fromString ( _string_, _radix_ )</h1> | ||
<p>The `Number.fromString` function produces an integer value dictated by interpretation of the contents of the _string_ argument according to the specified _radix_. Leading white space in _string_ is ignored. If _radix_ is *undefined* or 0, it is assumed to be 10.</p> | ||
<p>The `Number.fromString` function produces an integer value dictated by interpretation of the contents of the _string_ argument according to the specified _radix_. If _radix_ is *undefined*, it is assumed to be 10.</p> | ||
<p>When the `Number.fromString` function is called, the following steps are taken:</p> | ||
<emu-alg> | ||
1. Let _inputString_ be ? ToString(_string_). | ||
1. Let _S_ be a newly created substring of _inputString_ consisting of the first code unit that is not a |StrWhiteSpaceChar| and all code units following that code unit. (In other words, remove leading white space.) If _inputString_ does not contain any such code unit, let _S_ be the empty string. | ||
1. Let _S_ be ? ToString(_string_). | ||
1. If _S_ is empty, throw a *SyntaxError* exception. | ||
1. Let _sign_ be 1. | ||
1. If _S_ is not empty and the first code unit of _S_ is the code unit 0x002D (HYPHEN-MINUS), let _sign_ be -1. | ||
1. If _S_ is not empty and the first code unit of _S_ is the code unit 0x002B (PLUS SIGN) or the code unit 0x002D (HYPHEN-MINUS), remove the first code unit from _S_. | ||
1. If the first code unit of _S_ is the code unit 0x002D (HYPHEN-MINUS), then | ||
1. Let _sign_ be -1. | ||
1. Remove the first code unit from _S_. | ||
1. If _S_ is empty, throw a *SyntaxError* exception. | ||
1. If _R_ = 0, throw a *RangeError* exception. | ||
1. Let _R_ be ? ToInt32(_radix_). | ||
1. If _R_ = 0, then | ||
1. Let _R_ be 10. | ||
1. Else, | ||
1. Assert: _R_ ≠ 0. | ||
1. If _R_ < 2 or _R_ > 36, throw a *RangeError* exception. | ||
1. If _S_ contains a code unit that is not a radix-_R_ digit, then | ||
1. Let _Z_ be the substring of _S_ consisting of all code units before the first such code unit. | ||
1. Else, | ||
1. Let _Z_ be _S_. | ||
1. If _Z_ is empty, throw a *SyntaxError* exception. | ||
1. Let _mathInt_ be the mathematical integer value that is represented by _Z_ in radix-_R_ notation, using the letters <b>A</b>-<b>Z</b> and <b>a</b>-<b>z</b> for digits with values 10 through 35. (However, if _R_ is 10 and _Z_ contains more than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; and if _R_ is not 2, 4, 8, 10, 16, or 32, then _mathInt_ may be an implementation-dependent approximation to the mathematical integer value that is represented by _Z_ in radix-_R_ notation.) | ||
1. Let _number_ be _sign_ × _mathInt_. | ||
1. If _S_ represents a mathematical number value in radix-_R_ notation, using the letters <b>A</b>-<b>Z</b> and <b>a</b>-<b>z</b> for digits with values 10 through 35, then | ||
1. Let _mathNum_ be that mathematical number value. | ||
1. Else, throw a *SyntaxError* exception. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mathiasbynens
Author
Member
|
||
1. Let _number_ be _sign_ × _mathNum_. | ||
1. Return the Number value for _number_. | ||
This comment has been minimized.
Sorry, something went wrong.
littledan
Member
|
||
</emu-alg> | ||
<emu-note> | ||
<p>`Number.fromString` may interpret only a leading portion of _string_ as an integer value; it ignores any code units that cannot be interpreted as part of the notation of an integer, and no indication is given that any such code units were ignored.</p> | ||
</emu-note> | ||
</emu-clause> |
Minor editorial: I like the semantics here, and specifically listing the relevant code units, but I sort of liked the old wording better, where you'd first ask if there is an errant code unit, and only after that interpret it as a number. As worded now, I'm wondering, "is there any string that contains only the appropriate code units but does not represent an appropriate mathematical number value"? Might also be good to call out digits 0-9 as the lower digit code units.