Skip to content

Commit

Permalink
Editorial: refactor IsStringPrefix and String.prototype.split to …
Browse files Browse the repository at this point in the history
…use `StringIndexOf`; remove `SplitMatch` (tc39#2144)

Co-authored-by: Alexey Shvayka <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>
  • Loading branch information
2 people authored and mathiasbynens committed Oct 18, 2021
1 parent c28b267 commit 86df176
Showing 1 changed file with 24 additions and 51 deletions.
75 changes: 24 additions & 51 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -5877,10 +5877,11 @@ <h1>
<dd>It determines if _p_ is a prefix of _q_.</dd>
</dl>
<emu-alg>
1. If _q_ can be the string-concatenation of _p_ and some other String _r_, return *true*. Otherwise, return *false*.
1. If ! StringIndexOf(_q_, _p_, 0) is 0, return *true*.
1. Else, return *false*.
</emu-alg>
<emu-note>
<p>Any String is a prefix of itself, because _r_ may be the empty String.</p>
<p>Any String is a prefix of itself.</p>
</emu-note>
</emu-clause>

Expand Down Expand Up @@ -33885,37 +33886,30 @@ <h1>String.prototype.split ( _separator_, _limit_ )</h1>
1. If _splitter_ is not *undefined*, then
1. Return ? Call(_splitter_, _separator_, &laquo; _O_, _limit_ &raquo;).
1. Let _S_ be ? ToString(_O_).
1. Let _A_ be ! ArrayCreate(0).
1. Let _lengthA_ be 0.
1. If _limit_ is *undefined*, let _lim_ be 2<sup>32</sup> - 1; else let _lim_ be ℝ(? ToUint32(_limit_)).
1. Let _R_ be ? ToString(_separator_).
1. If _lim_ = 0, return _A_.
1. If _lim_ = 0, then
1. Return ! CreateArrayFromList(&laquo; &raquo;).
1. If _separator_ is *undefined*, then
1. Perform ! CreateDataPropertyOrThrow(_A_, *"0"*, _S_).
1. Return _A_.
1. Let _s_ be the length of _S_.
1. If _s_ = 0, then
1. If _R_ is not the empty String, then
1. Perform ! CreateDataPropertyOrThrow(_A_, *"0"*, _S_).
1. Return _A_.
1. Let _p_ be 0.
1. Let _q_ be _p_.
1. Repeat, while _q_ &ne; _s_,
1. Let _e_ be SplitMatch(_S_, _q_, _R_).
1. If _e_ is ~not-matched~, set _q_ to _q_ + 1.
1. Else,
1. Assert: _e_ is a non-negative integer &le; _s_.
1. If _e_ = _p_, set _q_ to _q_ + 1.
1. Else,
1. Let _T_ be the substring of _S_ from _p_ to _q_.
1. Perform ! CreateDataPropertyOrThrow(_A_, ! ToString(𝔽(_lengthA_)), _T_).
1. Set _lengthA_ to _lengthA_ + 1.
1. If _lengthA_ = _lim_, return _A_.
1. Set _p_ to _e_.
1. Set _q_ to _p_.
1. Let _T_ be the substring of _S_ from _p_ to _s_.
1. Perform ! CreateDataPropertyOrThrow(_A_, ! ToString(𝔽(_lengthA_)), _T_).
1. Return _A_.
1. Return ! CreateArrayFromList(&laquo; _S_ &raquo;).
1. Let _separatorLength_ be the length of _R_.
1. If _separatorLength_ is 0, then
1. Let _head_ be the substring of _S_ from 0 to _lim_.
1. Let _codeUnits_ be a List consisting of the sequence of code units that are the elements of _head_.
1. Return ! CreateArrayFromList(_codeUnits_).
1. If _S_ is the empty String, return ! CreateArrayFromList(&laquo; _S_ &raquo;).
1. Let _substrings_ be a new empty List.
1. Let _i_ be 0.
1. Let _j_ be ! StringIndexOf(_S_, _R_, 0).
1. Repeat, while _j_ is not -1,
1. Let _T_ be the substring of _S_ from _i_ to _j_.
1. Append _T_ as the last element of _substrings_.
1. If the number of elements of _substrings_ is _lim_, return ! CreateArrayFromList(_substrings_).
1. Set _i_ to _j_ + _separatorLength_.
1. Set _j_ to ! StringIndexOf(_S_, _R_, _i_).
1. Let _T_ be the substring of _S_ from _i_.
1. Append _T_ to _substrings_.
1. Return ! CreateArrayFromList(_substrings_).
</emu-alg>
<emu-note>
<p>The value of _separator_ may be an empty String. In this case, _separator_ does not match the empty <emu-not-ref>substring</emu-not-ref> at the beginning or end of the input String, nor does it match the empty <emu-not-ref>substring</emu-not-ref> at the end of the previous separator match. If _separator_ is the empty String, the String is split up into individual code unit elements; the length of the result array equals the length of the String, and each <emu-not-ref>substring</emu-not-ref> contains one code unit.</p>
Expand All @@ -33925,27 +33919,6 @@ <h1>String.prototype.split ( _separator_, _limit_ )</h1>
<emu-note>
<p>The `split` function is intentionally generic; it does not require that its *this* value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method.</p>
</emu-note>

<emu-clause id="sec-splitmatch" type="abstract operation">
<h1>
SplitMatch (
_S_: a String,
_q_: a non-negative integer,
_R_: a String,
)
</h1>
<dl class="header">
<dt>description</dt>
<dd>It returns either ~not-matched~ or the end index of a match.</dd>
</dl>
<emu-alg>
1. Let _r_ be the number of code units in _R_.
1. Let _s_ be the number of code units in _S_.
1. If _q_ + _r_ &gt; _s_, return ~not-matched~.
1. If there exists an integer _i_ between 0 (inclusive) and _r_ (exclusive) such that the code unit at index _q_ + _i_ within _S_ is different from the code unit at index _i_ within _R_, return ~not-matched~.
1. Return _q_ + _r_.
</emu-alg>
</emu-clause>
</emu-clause>

<emu-clause id="sec-string.prototype.startswith">
Expand Down

0 comments on commit 86df176

Please sign in to comment.