-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
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> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
dfdd25c
to
8a80eee
Compare
spec.html
Outdated
1. If ! StringIncludesAt(_px_, _py_, 0) is *true*, return *false*. | ||
1. If ! StringIncludesAt(_py_, _px_, 0) is *true*, return *true*. |
There was a problem hiding this comment.
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
1. If ! StringIncludesAt(_S_, _R_, _q_) is *false*, then set _q_ to _q_ + 1. | ||
1. Else, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
Discussed this on the editor call; let's please leave |
There was a problem hiding this 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(« _S_ »).
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_ > the length of _string_.</p> | |||
</emu-note> | |||
</emu-clause> | |||
|
|||
<emu-clause id="sec-stringincludesat" aoid="StringIncludesAt"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<emu-clause id="sec-stringincludesat" aoid="StringIncludesAt"> | |
<emu-clause id="sec-stringincludesat" aoid="StringIncludesAt" oldids="IsStringPrefix,SplitMatch"> |
3d0c24c
to
7a79833
Compare
…lit` to use `StringIndexOf`; remove `SplitMatch` (tc39#2144)
8a80eee
to
8aec5d4
Compare
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. |
…lit` to use `StringIndexOf`; remove `SplitMatch` (tc39#2144)
8aec5d4
to
166ef10
Compare
…lit` to use `StringIndexOf`; remove `SplitMatch` (tc39#2144)
166ef10
to
5d7fa09
Compare
There was a problem hiding this 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.
…use `StringIndexOf`; remove `SplitMatch` (tc39#2144) Co-authored-by: Alexey Shvayka <[email protected]> Co-authored-by: Jordan Harband <[email protected]>
a0aa602
to
82438c3
Compare
IsStringPrefix
and String.prototype.split
to use StringIndexOf
; remove SplitMatch
…use `StringIndexOf`; remove `SplitMatch` (tc39#2144) Co-authored-by: Alexey Shvayka <[email protected]> Co-authored-by: Jordan Harband <[email protected]>
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
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
This PR merges
IsStringPrefix
andSplitMatch
abstract ops into single well-definedStringIncludesAt
helper, improving:IsStringPrefix
;SplitMatch
.We can't use
StringIndexOf
instead, since it searches until the end of string rather than matching substring only at index.