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: refactor IsStringPrefix and String.prototype.split to use StringIndexOf; remove SplitMatch #2144

Merged
merged 1 commit into from
Oct 15, 2021

Conversation

shvaikalesh
Copy link
Member

This PR merges IsStringPrefix and SplitMatch abstract ops into single well-defined StringIncludesAt helper, improving:

We can't use StringIndexOf instead, since it searches until the end of string rather than matching substring only at index.

spec.html Outdated
@@ -5624,19 +5646,6 @@ <h1>IsRegExp ( _argument_ )</h1>
</emu-alg>
</emu-clause>

<emu-clause id="sec-isstringprefix" aoid="IsStringPrefix">
<h1>IsStringPrefix ( _p_, _q_ )</h1>
Copy link
Member

Choose a reason for hiding this comment

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

For readability, we could leave this with an implementation that just passes 0 as the third parameter to StringIncludesAt. Not sure if it's worth it.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's maybe just for me, but parameter order of IsStringPrefix isn't immediately clear: is first parameter a string or a prefix?

Copy link
Member

Choose a reason for hiding this comment

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

I feel the same way about StringIncludesAt.

Copy link
Member Author

Choose a reason for hiding this comment

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

It would be great if we came up with a better name. StringMatch?

Copy link
Member

Choose a reason for hiding this comment

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

I don't really think the name will ever be sufficient. I will always need to take a quick look at the parameter names or the summary. Or infer it from context at the usage site.

spec.html Outdated Show resolved Hide resolved
spec.html Outdated Show resolved Hide resolved
@shvaikalesh shvaikalesh force-pushed the string-includes-at branch 2 times, most recently from dfdd25c to 8a80eee Compare August 19, 2020 20:22
ljharb
ljharb previously requested changes Aug 19, 2020
spec.html Outdated
Comment on lines 5708 to 5709
1. If ! StringIncludesAt(_px_, _py_, 0) is *true*, return *false*.
1. If ! StringIncludesAt(_py_, _px_, 0) is *true*, return *true*.
Copy link
Member

Choose a reason for hiding this comment

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

i don't find this change clearer :-/

spec.html Outdated
Comment on lines 31043 to 31044
1. If ! StringIncludesAt(_S_, _R_, _q_) is *false*, then set _q_ to _q_ + 1.
1. Else,
Copy link
Member

Choose a reason for hiding this comment

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

since the "else" is multiline, i think the "if" needs to be too.

Copy link
Collaborator

@jmdyck jmdyck Aug 20, 2020

Choose a reason for hiding this comment

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

The spec has about 37 examples of an 'inline' If followed by a 'multiline' Else. And about 18 examples of a multiline If followed by an inline Else.

Copy link
Member

Choose a reason for hiding this comment

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

Compared to how many examples are both multiline or both inline, those seem like cases we should make consistent.

@ljharb ljharb added editor call to be discussed in the next editor call editorial change labels Aug 19, 2020
@ljharb
Copy link
Member

ljharb commented Aug 19, 2020

Discussed this on the editor call; let's please leave IsStringPrefix and its callsites untouched (but update its implementation to call into StringIncludesAt). I'm not personally convinced that StringIncludesAt is the best name but don't have a better suggestion at this time.

@ljharb ljharb removed the editor call to be discussed in the next editor call label Aug 19, 2020
Copy link
Contributor

@gibson042 gibson042 left a comment

Choose a reason for hiding this comment

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

I'm in favor of removing redundant operations, but not clear on why it's necessary to introduce a new one rather than using StringIndexOf. IsStringPrefix(_possiblePrefix_, _string_) is equivalent to StringIndexOf(_string_, _possiblePrefix_, 0) is 0, and String.prototype.split can be refactored as well:

1. Let _s_ be the length of _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 0, return ! CreateArrayFromList(&laquo; _S_ &raquo;).
1. Let _substrings_ be a new empty List.
1. Let _i_ be 0.
1. Let _j_ = 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_ 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_).

spec.html Outdated
@@ -1038,6 +1038,28 @@ <h1>Runtime Semantics: StringIndexOf ( _string_, _searchValue_, _fromIndex_ )</h
<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">
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
<emu-clause id="sec-stringincludesat" aoid="StringIncludesAt">
<emu-clause id="sec-stringincludesat" aoid="StringIncludesAt" oldids="IsStringPrefix,SplitMatch">

@ljharb ljharb force-pushed the master branch 3 times, most recently from 3d0c24c to 7a79833 Compare June 29, 2021 02:21
ljharb added a commit to shvaikalesh/ecma262 that referenced this pull request Oct 7, 2021
…lit` to use `StringIndexOf`; remove `SplitMatch` (tc39#2144)
@ljharb ljharb force-pushed the string-includes-at branch from 8a80eee to 8aec5d4 Compare October 7, 2021 04:50
@ljharb
Copy link
Member

ljharb commented Oct 7, 2021

I've rebased this; the first commit implements #2144 (comment); the second commit reverts that and implements #2144 (review), per one editor's preference on Matrix.

Happy to drop the second, or squash the two, as preferred.

The only unaccounted-for piece in the second commit is adding an oldid somewhere for SplitMatch - I'm not sure where it would go.

@ljharb ljharb dismissed their stale review October 7, 2021 04:52

updated

@ljharb ljharb requested review from syg, bakkot and a team October 7, 2021 04:52
ljharb added a commit to shvaikalesh/ecma262 that referenced this pull request Oct 7, 2021
…lit` to use `StringIndexOf`; remove `SplitMatch` (tc39#2144)
@ljharb ljharb force-pushed the string-includes-at branch from 8aec5d4 to 166ef10 Compare October 7, 2021 04:53
spec.html Outdated Show resolved Hide resolved
spec.html Outdated Show resolved Hide resolved
spec.html Outdated Show resolved Hide resolved
spec.html Outdated Show resolved Hide resolved
spec.html Show resolved Hide resolved
ljharb added a commit to shvaikalesh/ecma262 that referenced this pull request Oct 15, 2021
…lit` to use `StringIndexOf`; remove `SplitMatch` (tc39#2144)
@ljharb ljharb force-pushed the string-includes-at branch from 166ef10 to 5d7fa09 Compare October 15, 2021 06:42
spec.html Show resolved Hide resolved
Copy link
Contributor

@bakkot bakkot left a comment

Choose a reason for hiding this comment

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

LGTM. The new version of split is much clearer. The old one is opaque enough that it's hard for me to be 100% certain they're identical, but I'm pretty confident they are.

spec.html Show resolved Hide resolved
@michaelficarra michaelficarra added the ready to merge Editors believe this PR needs no further reviews, and is ready to land. label Oct 15, 2021
…use `StringIndexOf`; remove `SplitMatch` (tc39#2144)

Co-authored-by: Alexey Shvayka <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>
@ljharb ljharb force-pushed the string-includes-at branch from a0aa602 to 82438c3 Compare October 15, 2021 17:48
@ljharb ljharb changed the title Editorial: Merge IsStringPrefix and SplitMatch abstract ops Editorial: refactor IsStringPrefix and String.prototype.split to use StringIndexOf; remove SplitMatch Oct 15, 2021
@ljharb ljharb merged commit 82438c3 into tc39:master Oct 15, 2021
mathiasbynens pushed a commit to mathiasbynens/ecma262 that referenced this pull request Oct 18, 2021
…use `StringIndexOf`; remove `SplitMatch` (tc39#2144)

Co-authored-by: Alexey Shvayka <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>
ljharb added a commit to ljharb/es-abstract that referenced this pull request May 2, 2022
Renamed:
 - `ExecuteModule` → `ExecuteAsyncModule`
 - `Abstract Equality Comparison` → `IsLooselyEqual`
 - `Abstract Relational Comparison` → `IsLessThan`
 - `Strict Equality Comparison` → `IsStrictlyEqual`

Added:
 - `CreateNonEnumerableDataPropertyOrThrow`
 - `DefineField`
 - `DefineMethodProperty`
 - `GatherAvailableAncestors`
 - `IfAbruptCloseIterator`
 - `InitializeInstanceElements`
 - `InstallErrorCause`
 - `IsPrivateReference`
 - `IsStringWellFormedUnicode`
 - `MakePrivateReference`
 - `NewPrivateEnvironment`
 - `PrivateElementFind`
 - `PrivateFieldAdd`
 - `PrivateGet`
 - `PrivateMethodOrAccessorAdd`
 - `PrivateSet`
 - `RegExpHasFlag`
 - `ResolvePrivateIdentifier`
 - `RoundMVResult`
 - `SortIndexedProperties`
 - `StringToNumber`
 - `TypedArrayElementSize`
 - `TypedArrayElementType`

Removed:
 - `InitializeEnvironment`
 - `SplitMatch` removed; `IsStringPrefix` refactored: tc39/ecma262#2144
ljharb added a commit to ljharb/es-abstract that referenced this pull request May 5, 2022
Renamed:
 - `ExecuteModule` → `ExecuteAsyncModule`
 - `Abstract Equality Comparison` → `IsLooselyEqual`
 - `Abstract Relational Comparison` → `IsLessThan`
 - `Strict Equality Comparison` → `IsStrictlyEqual`

Added:
 - `CreateNonEnumerableDataPropertyOrThrow`
 - `DefineField`
 - `DefineMethodProperty`
 - `GatherAvailableAncestors`
 - `IfAbruptCloseIterator`
 - `InitializeInstanceElements`
 - `InstallErrorCause`
 - `IsPrivateReference`
 - `IsStringWellFormedUnicode`
 - `MakePrivateReference`
 - `NewPrivateEnvironment`
 - `PrivateElementFind`
 - `PrivateFieldAdd`
 - `PrivateGet`
 - `PrivateMethodOrAccessorAdd`
 - `PrivateSet`
 - `RegExpHasFlag`
 - `ResolvePrivateIdentifier`
 - `RoundMVResult`
 - `SortIndexedProperties`
 - `StringToNumber`
 - `TypedArrayElementSize`
 - `TypedArrayElementType`

Removed:
 - `InitializeEnvironment`
 - `SplitMatch` removed; `IsStringPrefix` refactored: tc39/ecma262#2144
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editorial change ready to merge Editors believe this PR needs no further reviews, and is ready to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants