Skip to content

Commit 85be039

Browse files
committed
Add new AO for canonicalizing digit strings
Use the new AO in AOs that count fractional/significant digits.
1 parent a2850a1 commit 85be039

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

spec.emu

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,36 @@ location: https://github.com/tc39/proposal-measure/
7474
<emu-clause id="sec-amount-abstract-ops-decimal-digit-strings">
7575
<h1>Operations on Decimal Digit Strings</h1>
7676

77+
<emu-clause id="sec-amount-canonicalizedigitstring" type="abstract operation">
78+
<h1>CanonicalizeDigitString(
79+
_s_: a decimal digit String
80+
): a decimal digit String
81+
</h1>
82+
<dl class="header">
83+
<dt>description</dt>
84+
<dd>It ensures that a decimal digit string adheres to a restricted syntax in which there are no redundant leading zeroes, no leading or trailing decimal points, and ensures that any *"E"* (indicating exponential notation) is downcased.</dd>
85+
</dl>
86+
<emu-alg>
87+
1. If _s_ contains an occurrence of *"E"*, then
88+
1. Let _beforeE_ be the substring of _s_ before the first occurrence of *"E"*.
89+
1. Let _afterE_ be the substring of _s_ after the first occurrence of *"E"*.
90+
1. Set _s_ to the string concatenation of _beforeE, *"e"*, and _afterE_.
91+
1. If _s_ contains an occurrence of *"e"*, then
92+
1. Let _beforeE_ be the substring of _s_ before the first occurrence of *"e"*.
93+
1. Let _afterE_ be the substring of _s_ after the first occurrence of *"E"*.
94+
1. Let _canonicalizedBeforeE_ be CanonicalizeDigitString(_beforeE_).
95+
1. Let _canonicalizedAfterE_ be CanonicalizeDigitString(_afterE_).
96+
1. Return the string concatenation of _canonicalizedBeforeE_, *"e"*, and _canonicalizedAfterE_.
97+
1. If _s_ begins with an occurrence of *"00"*, then
98+
1. Let _afterDoubleZero_ be the substring of _s_ after the first occurrence of *"00"*.
99+
1. Let _oneFewerZero_ be the string concatenation of *"0"* and _afterDoubleZero_.
100+
1. Return CanonicalizeDigitString(_oneFewerZero_).
101+
1. If _s_ begins with an occurrence of *"."*, return the string concatenation of *"0"* and _s_.
102+
1. IF _s_ ends with an occurrence of *"."*, return the substring of _s_ from 0 to the index of the final occurrence of *"."* in _s_.
103+
1. Return _s_.
104+
</emu-alg>
105+
</emu-clause>
106+
77107
<emu-clause id="sec-amount-countsignificantdigits" type="abstract operation">
78108
<h1>CountSignificantDigits(
79109
_s_: a decimal digit String
@@ -84,6 +114,7 @@ location: https://github.com/tc39/proposal-measure/
84114
<dd>It computes the number of significant digits in a given <emu-xref href="#dfn-decimal-digit-string">decimal digit String</emu-xref>.</dd>
85115
</dl>
86116
<emu-alg>
117+
1. Set _s_ to CanonicalizeDigitString(_s_).
87118
1. Let _digitsToCount_ be _s_.
88119
1. If _digitsToCount_ contains an *e* character, set _digitsToCount_ be the substring of _s_ before the first occurence of *"e"*.
89120
1. If _digitsToCount_ contains a *"."* character, set _digitsToCount_ to the result of replacing all occurrences of *"."* in _digitsToCount_ by *""*.
@@ -102,6 +133,7 @@ location: https://github.com/tc39/proposal-measure/
102133
<dd>It computes the number of fractional digits in a given <emu-xref href="#dfn-decimal-digit-string">decimal digit String</emu-xref>.</dd>
103134
</dl>
104135
<emu-alg>
136+
1. Set _s_ to CanonicalizeDigitString(_s_).
105137
1. If _s_ contains a *"."* character, then
106138
1. Let _fractionDigits_ be the substring of _s_ after the first *"."* character up to the first occurrence of *"e"* or *"E"* in _s_, if any.
107139
1. Let _l_ be the length of _fractionDigits_.
@@ -282,9 +314,9 @@ location: https://github.com/tc39/proposal-measure/
282314
1. If _toParse_ is not a String, throw a *TypeError* exception.
283315
1. If _toParse_ is in « *"NaN"*, *"Infinity"*, *"-Infinity"* », throw a *RangeError* exception.
284316
1. Let _parseResult_ be ParseText(_toParse_, |StrDecimalLiteral|).
285-
1. Let _validatedOpts_ be ? GetAmountOptions(_opts_).
286317
1. If _parseResult_ is a List of errors, throw a *SyntaxError* exception.
287318
1. Let _amountValue_ be ? StringDecimalValue of _parseResult_.
319+
1. Let _validatedOpts_ be ? GetAmountOptions(_opts_).
288320
1. Let _fractionDigits_ be _validatedOpts_.[[FractionDigits]].
289321
1. Let _roundingMode_ be _validatedOpts_.[[RoundingMode]].
290322
1. Let _significantDigits_ be _validatedOpts_.[[SignificantDigits]].

0 commit comments

Comments
 (0)