From e7f942bac2c9f37529b4537994a9d940697800f4 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 6 Oct 2021 21:50:16 -0700 Subject: [PATCH] squash: Editorial: refactor `IsStringPrefix` and `String.prototype.split` to use `StringIndexOf`; remove `SplitMatch` (#2144) --- spec.html | 95 +++++++++++++------------------------------------------ 1 file changed, 22 insertions(+), 73 deletions(-) diff --git a/spec.html b/spec.html index 0aa4a68b826..76758c039e9 100644 --- a/spec.html +++ b/spec.html @@ -1058,28 +1058,6 @@

This algorithm always returns -1 if _fromIndex_ > the length of _string_.

- - -

Runtime Semantics: StringIncludesAt ( _string_, _substring_, _atIndex_ )

-

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:

- - 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_ ≤ _len_, return *true*. - 1. Let _substringLen_ be the length of _substring_. - 1. If _atIndex_ + _substringLen_ > _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*. - - -

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.

-
- -

This algorithm always returns *false* if _atIndex_ > the length of _string_.

-
-
@@ -5899,7 +5877,7 @@

It determines if _p_ is a prefix of _q_.
- 1. Return ! StringIncludesAt(_q_, _p_, 0). + 1. Return ! SameValue(! StringIndexOf(_q_, _p_, 0), 0).

Any String is a prefix of itself.

@@ -33907,37 +33885,29 @@

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 _separator_ is *undefined*, then - 1. Perform ! CreateDataPropertyOrThrow(_A_, *"0"*, _S_). - 1. Return _A_. + 1. If _limit_ is *undefined*, let _lim_ be 232 - 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_ ≠ _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. 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(« _S_ »). + 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_).

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.

@@ -33947,27 +33917,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_. - -