Skip to content

Commit

Permalink
squash: Editorial: refactor IsStringPrefix and `String.prototype.sp…
Browse files Browse the repository at this point in the history
…lit` to use `StringIndexOf`; remove `SplitMatch` (tc39#2144)
  • Loading branch information
ljharb committed Oct 7, 2021
1 parent 64bf5e5 commit 8aec5d4
Showing 1 changed file with 22 additions and 74 deletions.
96 changes: 22 additions & 74 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -1044,28 +1044,6 @@ <h1>
<p>This algorithm always returns -1 if _fromIndex_ &gt; the length of _string_.</p>
</emu-note>
</emu-clause>

<emu-clause id="sec-stringincludesat" aoid="StringIncludesAt">
<h1>Runtime Semantics: StringIncludesAt ( _string_, _substring_, _atIndex_ )</h1>
<p>The abstract operation StringIncludesAt takes arguments _string_ (a String), _substring_ (a String), and _atIndex_ (a non-negative integer). It performs the following steps when called:</p>
<emu-alg>
1. Assert: Type(_string_) is String.
1. Assert: Type(_substring_) is String.
1. Assert: ! IsNonNegativeInteger(_atIndex_) is *true*.
1. Let _len_ be the length of _string_.
1. If _substring_ is the empty String and _atIndex_ &le; _len_, return *true*.
1. Let _substringLen_ be the length of _substring_.
1. If _atIndex_ + _substringLen_ &gt; _len_, return *false*.
1. If for all non-negative integers _j_ less than _substringLen_, the code unit at index _atIndex_ + _j_ within _string_ is the same as the code unit at index _j_ within _substring_, return *true*.
1. Otherwise, return *false*.
</emu-alg>
<emu-note>
<p>If _substring_ is the empty String and _atIndex_ is less than or equal to the length of _string_, this algorithm returns *true*. The empty String is effectively found at every position within a string, including after the last code unit.</p>
</emu-note>
<emu-note>
<p>This algorithm always returns *false* if _atIndex_ &gt; the length of _string_.</p>
</emu-note>
</emu-clause>
</emu-clause>

<emu-clause id="sec-ecmascript-language-types-symbol-type">
Expand Down Expand Up @@ -5885,7 +5863,7 @@ <h1>
<dd>It determines if _p_ is a prefix of _q_.</dd>
</dl>
<emu-alg>
1. Return ! StringIncludesAt(_q_, _p_, 0).
1. Return ! SameValue(! StringIndexOf(_q_, _p_, 0), 0).
</emu-alg>
<emu-note>
<p>Any String is a prefix of itself.</p>
Expand Down Expand Up @@ -33896,37 +33874,29 @@ <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 _separator_ is *undefined*, then
1. Perform ! CreateDataPropertyOrThrow(_A_, *"0"*, _S_).
1. Return _A_.
1. If _limit_ is *undefined*, let _lim_ be 2<sup>32</sup> - 1; else let _lim_ be ℝ(? ToUint32(_limit_)).
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. 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 0, 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. Let _lengthA_ be 0.
1. Repeat, while _j_ is not -1,
1. Let _T_ be the substring of _S_ from _i_ to _j_.
1. Append _T_ to _substrings_.
1. Set _lengthA_ to _lengthA_ + 1.
1. If _lengthA_ = _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 @@ -33937,28 +33907,6 @@ <h1>String.prototype.split ( _separator_, _limit_ )</h1>
<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">
<h1>String.prototype.startsWith ( _searchString_ [ , _position_ ] )</h1>
<p>The following steps are taken:</p>
Expand Down

0 comments on commit 8aec5d4

Please sign in to comment.