Skip to content
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

Editorial: Generalize ToZeroPaddedDecimalString to support hexadecimal output #3305

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -29232,8 +29232,7 @@ <h1>
1. Set _k_ to _k_ + _cp_.[[CodeUnitCount]].
1. Let _Octets_ be the List of octets resulting by applying the UTF-8 transformation to _cp_.[[CodePoint]].
1. For each element _octet_ of _Octets_, do
1. Let _hex_ be the String representation of _octet_, formatted as an uppercase hexadecimal number.
1. Set _R_ to the string-concatenation of _R_, *"%"*, and StringPad(_hex_, 2, *"0"*, ~start~).
1. Set _R_ to the string-concatenation of _R_, *"%"*, and ZeroPad(_octet_, 2, ~hex-upper~).
1. Return _R_.
</emu-alg>
<emu-note>
Expand Down Expand Up @@ -34026,9 +34025,9 @@ <h1>
<dl class="header">
</dl>
<emu-alg>
1. Let _hour_ be ToZeroPaddedDecimalString(ℝ(HourFromTime(_tv_)), 2).
1. Let _minute_ be ToZeroPaddedDecimalString(ℝ(MinFromTime(_tv_)), 2).
1. Let _second_ be ToZeroPaddedDecimalString(ℝ(SecFromTime(_tv_)), 2).
1. Let _hour_ be ZeroPad(ℝ(HourFromTime(_tv_)), 2, ~decimal~).
1. Let _minute_ be ZeroPad(ℝ(MinFromTime(_tv_)), 2, ~decimal~).
1. Let _second_ be ZeroPad(ℝ(SecFromTime(_tv_)), 2, ~decimal~).
1. Return the string-concatenation of _hour_, *":"*, _minute_, *":"*, _second_, the code unit 0x0020 (SPACE), and *"GMT"*.
</emu-alg>
</emu-clause>
Expand All @@ -34044,10 +34043,10 @@ <h1>
<emu-alg>
1. Let _weekday_ be the Name of the entry in <emu-xref href="#sec-todatestring-day-names"></emu-xref> with the Number WeekDay(_tv_).
1. Let _month_ be the Name of the entry in <emu-xref href="#sec-todatestring-month-names"></emu-xref> with the Number MonthFromTime(_tv_).
1. Let _day_ be ToZeroPaddedDecimalString(ℝ(DateFromTime(_tv_)), 2).
1. Let _day_ be ZeroPad(ℝ(DateFromTime(_tv_)), 2, ~decimal~).
1. Let _yv_ be YearFromTime(_tv_).
1. If _yv_ is *+0*<sub>𝔽</sub> or _yv_ > *+0*<sub>𝔽</sub>, let _yearSign_ be the empty String; otherwise, let _yearSign_ be *"-"*.
1. Let _paddedYear_ be ToZeroPaddedDecimalString(abs(ℝ(_yv_)), 4).
1. Let _paddedYear_ be ZeroPad(abs(ℝ(_yv_)), 4, ~decimal~).
1. Return the string-concatenation of _weekday_, the code unit 0x0020 (SPACE), _month_, the code unit 0x0020 (SPACE), _day_, the code unit 0x0020 (SPACE), _yearSign_, and _paddedYear_.
</emu-alg>
<emu-table id="sec-todatestring-day-names" caption="Names of days of the week">
Expand Down Expand Up @@ -34249,8 +34248,8 @@ <h1>
1. Else,
1. Let _offsetSign_ be *"-"*.
1. Let _absOffset_ be -_offset_.
1. Let _offsetMin_ be ToZeroPaddedDecimalString(ℝ(MinFromTime(_absOffset_)), 2).
1. Let _offsetHour_ be ToZeroPaddedDecimalString(ℝ(HourFromTime(_absOffset_)), 2).
1. Let _offsetMin_ be ZeroPad(ℝ(MinFromTime(_absOffset_)), 2, ~decimal~).
1. Let _offsetHour_ be ZeroPad(ℝ(HourFromTime(_absOffset_)), 2, ~decimal~).
1. Let _tzName_ be an implementation-defined string that is either the empty String or the string-concatenation of the code unit 0x0020 (SPACE), the code unit 0x0028 (LEFT PARENTHESIS), an implementation-defined timezone name, and the code unit 0x0029 (RIGHT PARENTHESIS).
1. Return the string-concatenation of _offsetSign_, _offsetHour_, _offsetMin_, and _tzName_.
</emu-alg>
Expand Down Expand Up @@ -34296,10 +34295,10 @@ <h1>Date.prototype.toUTCString ( )</h1>
1. If _tv_ is *NaN*, return *"Invalid Date"*.
1. Let _weekday_ be the Name of the entry in <emu-xref href="#sec-todatestring-day-names"></emu-xref> with the Number WeekDay(_tv_).
1. Let _month_ be the Name of the entry in <emu-xref href="#sec-todatestring-month-names"></emu-xref> with the Number MonthFromTime(_tv_).
1. Let _day_ be ToZeroPaddedDecimalString(ℝ(DateFromTime(_tv_)), 2).
1. Let _day_ be ZeroPad(ℝ(DateFromTime(_tv_)), 2, ~decimal~).
1. Let _yv_ be YearFromTime(_tv_).
1. If _yv_ is *+0*<sub>𝔽</sub> or _yv_ > *+0*<sub>𝔽</sub>, let _yearSign_ be the empty String; otherwise, let _yearSign_ be *"-"*.
1. Let _paddedYear_ be ToZeroPaddedDecimalString(abs(ℝ(_yv_)), 4).
1. Let _paddedYear_ be ZeroPad(abs(ℝ(_yv_)), 4, ~decimal~).
1. Return the string-concatenation of _weekday_, *","*, the code unit 0x0020 (SPACE), _day_, the code unit 0x0020 (SPACE), _month_, the code unit 0x0020 (SPACE), _yearSign_, _paddedYear_, the code unit 0x0020 (SPACE), and TimeString(_tv_).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -34849,17 +34848,20 @@ <h1>
</emu-note>
</emu-clause>

<emu-clause id="sec-tozeropaddeddecimalstring" type="abstract operation">
<emu-clause id="sec-zeropad" type="abstract operation" oldids="sec-tozeropaddeddecimalstring">
<h1>
ToZeroPaddedDecimalString (
ZeroPad (
_n_: a non-negative integer,
_minLength_: a non-negative integer,
_alphabet_: ~decimal~, ~hex-lower~, or ~hex-upper~,
): a String
</h1>
<dl class="header">
</dl>
<emu-alg>
1. Let _S_ be the String representation of _n_, formatted as a decimal number.
1. If _alphabet_ is ~hex-lower~, let _S_ be the String representation of _n_, formatted as a lowercase hexadecimal number.
1. Else if _alphabet_ is ~hex-upper~, let _S_ be the String representation of _n_, formatted as an uppercase hexadecimal number.
1. Else, let _S_ be the String representation of _n_, formatted as a decimal number.
Comment on lines +34862 to +34864
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This could also use the approach of proposal-regex-escaping and replace prose with AO calls, e.g.

Suggested change
1. If _alphabet_ is ~hex-lower~, let _S_ be the String representation of _n_, formatted as a lowercase hexadecimal number.
1. Else if _alphabet_ is ~hex-upper~, let _S_ be the String representation of _n_, formatted as an uppercase hexadecimal number.
1. Else, let _S_ be the String representation of _n_, formatted as a decimal number.
1. If _alphabet_ is ~decimal~, let _S_ be BigInt::toString(ℤ(_n_), 10).
1. Else if _alphabet_ is ~hex-lower~, let _S_ be BigInt::toString(ℤ(_n_), 16).
1. Else, let _S_ be the ASCII-uppercase of BigInt::toString(ℤ(_n_), 16).

(importing ECMA-402's ASCII-uppercase, as is already planned for Temporal)

1. Return StringPad(_S_, _minLength_, *"0"*, ~start~).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -45312,8 +45314,7 @@ <h1>
<emu-alg>
1. Let _n_ be the numeric value of _C_.
1. Assert: _n_ ≤ 0xFFFF.
1. Let _hex_ be the String representation of _n_, formatted as a lowercase hexadecimal number.
1. Return the string-concatenation of the code unit 0x005C (REVERSE SOLIDUS), *"u"*, and StringPad(_hex_, 4, *"0"*, ~start~).
1. Return the string-concatenation of the code unit 0x005C (REVERSE SOLIDUS), *"u"*, and ZeroPad(_n_, 4, ~hex-lower~).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -50108,11 +50109,9 @@ <h1>escape ( _string_ )</h1>
1. Else,
1. Let _n_ be the numeric value of _C_.
1. If _n_ &lt; 256, then
1. Let _hex_ be the String representation of _n_, formatted as an uppercase hexadecimal number.
1. Let _S_ be the string-concatenation of *"%"* and StringPad(_hex_, 2, *"0"*, ~start~).
1. Let _S_ be the string-concatenation of *"%"* and ZeroPad(_n_, 2, ~hex-upper~).
1. Else,
1. Let _hex_ be the String representation of _n_, formatted as an uppercase hexadecimal number.
1. Let _S_ be the string-concatenation of *"%u"* and StringPad(_hex_, 4, *"0"*, ~start~).
1. Let _S_ be the string-concatenation of *"%u"* and ZeroPad(_n_, 4, ~hex-upper~).
1. Set _R_ to the string-concatenation of _R_ and _S_.
1. Set _k_ to _k_ + 1.
1. Return _R_.
Expand Down
Loading