From 86df176ffd50839b323c88c299cec5b41b10745e Mon Sep 17 00:00:00 2001 From: Alexey Shvayka Date: Fri, 15 Oct 2021 10:48:13 -0700 Subject: [PATCH] Editorial: refactor `IsStringPrefix` and `String.prototype.split` to use `StringIndexOf`; remove `SplitMatch` (#2144) Co-authored-by: Alexey Shvayka Co-authored-by: Jordan Harband --- spec.html | 75 ++++++++++++++++++------------------------------------- 1 file changed, 24 insertions(+), 51 deletions(-) diff --git a/spec.html b/spec.html index 4f37193bd5d..1775acfbba0 100644 --- a/spec.html +++ b/spec.html @@ -5877,10 +5877,11 @@

It determines if _p_ is a prefix of _q_.
- 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*. -

Any String is a prefix of itself, because _r_ may be the empty String.

+

Any String is a prefix of itself.

@@ -33885,37 +33886,30 @@

String.prototype.split ( _separator_, _limit_ )

1. If _splitter_ is not *undefined*, then 1. Return ? Call(_splitter_, _separator_, « _O_, _limit_ »). 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 232 - 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(« »). 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_ ≠ _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 ≤ _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(« _S_ »). + 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(« _S_ »). + 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_).

The value of _separator_ may be an empty String. In this case, _separator_ does not match the empty substring at the beginning or end of the input String, nor does it match the empty substring 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 substring contains one code unit.

@@ -33925,27 +33919,6 @@

String.prototype.split ( _separator_, _limit_ )

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.

- - -

- SplitMatch ( - _S_: a String, - _q_: a non-negative integer, - _R_: a String, - ) -

-
-
description
-
It returns either ~not-matched~ or the end index of a match.
-
- - 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_ > _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_. - -