-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[CoreLib] String.splitMap #59686
base: main
Are you sure you want to change the base?
[CoreLib] String.splitMap #59686
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,104 @@ final class JSString extends Interceptor | |
return stringReplaceAllFuncUnchecked(this, from, onMatch, onNonMatch); | ||
} | ||
|
||
@notNull | ||
Iterable<T> splitMap<T>( | ||
Pattern pattern, { | ||
T Function(Match match)? onMatch, | ||
T Function(String nonMatch)? onNonMatch, | ||
}) { | ||
return stringSplitMapUnchecked(this, pattern, onMatch, onNonMatch); | ||
} | ||
|
||
Iterable<T> stringSplitMapUnchecked<T>(String receiver, Pattern pattern, | ||
T Function(Match)? onMatch, T Function(String)? onNonMatch) { | ||
onMatch ??= (match) => match[0]! as T; | ||
onNonMatch ??= (string) => string as T; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely not sound. If the parameters are optional, I'd emit nothing for a match/non-match if |
||
if (pattern is String) { | ||
return stringSplitStringMapUnchecked( | ||
receiver, pattern, onMatch, onNonMatch); | ||
} | ||
if (pattern is JSSyntaxRegExp) { | ||
return stringSplitJSRegExpMapUnchecked(receiver, pattern, onMatch, onNonMatch); | ||
} | ||
return stringSplitGeneralMapUnchecked(receiver, pattern, onMatch, onNonMatch); | ||
} | ||
|
||
Iterable<T> stringSplitStringMapUnchecked<T>(String receiver, String pattern, | ||
T Function(Match) onMatch, T Function(String) onNonMatch) { | ||
if (pattern.isEmpty) { | ||
return stringSplitEmptyMapUnchecked(receiver, onMatch, onNonMatch); | ||
} | ||
List<T> result = <T>[]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be lazy, it shouldn't build a |
||
int startIndex = 0; | ||
int patternLength = pattern.length; | ||
while (true) { | ||
int position = stringIndexOfStringUnchecked(receiver, pattern, startIndex); | ||
if (position == -1) { | ||
break; | ||
} | ||
result.add(onNonMatch(receiver.substring(startIndex, position))); | ||
result.add(onMatch(StringMatch(position, receiver, pattern))); | ||
startIndex = position + patternLength; | ||
} | ||
result.add(onNonMatch(receiver.substring(startIndex))); | ||
return result; | ||
} | ||
|
||
Iterable<T> stringSplitEmptyMapUnchecked<T>( | ||
String receiver, T Function(Match) onMatch, T Function(String) onNonMatch) { | ||
List<T> result = <T>[]; | ||
int length = receiver.length; | ||
int i = 0; | ||
result.add(onNonMatch("")); | ||
while (i < length) { | ||
result.add(onMatch(StringMatch(i, receiver, ""))); | ||
// Special case to avoid splitting a surrogate pair. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A (It'd be nice if it didn't, but then it'd be even niceer if it didn't break grapheme clusters either.) (I can see that |
||
int code = receiver.codeUnitAt(i); | ||
if ((code & ~0x3FF) == 0xD800 && length > i + 1) { | ||
// Leading surrogate; | ||
code = receiver.codeUnitAt(i + 1); | ||
if ((code & ~0x3FF) == 0xDC00) { | ||
// Matching trailing surrogate. | ||
result.add(onNonMatch(receiver.substring(i, i + 2))); | ||
i += 2; | ||
continue; | ||
} | ||
} | ||
result.add(onNonMatch(receiver[i])); | ||
i++; | ||
} | ||
result.add(onMatch(StringMatch(i, receiver, ""))); | ||
result.add(onNonMatch("")); | ||
return result; | ||
} | ||
|
||
Iterable<T> stringSplitJSRegExpMapUnchecked<T>(String receiver, | ||
JSSyntaxRegExp pattern, T Function(Match) onMatch, T Function(String) onNonMatch) { | ||
List<T> result = <T>[]; | ||
int startIndex = 0; | ||
for (Match match in pattern.allMatches(receiver)) { | ||
result.add(onNonMatch(receiver.substring(startIndex, match.start))); | ||
result.add(onMatch(match)); | ||
startIndex = match.end; | ||
} | ||
result.add(onNonMatch(receiver.substring(startIndex))); | ||
return result; | ||
} | ||
|
||
Iterable<T> stringSplitGeneralMapUnchecked<T>(String receiver, Pattern pattern, | ||
T Function(Match) onMatch, T Function(String) onNonMatch) { | ||
List<T> result = <T>[]; | ||
int startIndex = 0; | ||
for (Match match in pattern.allMatches(receiver)) { | ||
result.add(onNonMatch(receiver.substring(startIndex, match.start))); | ||
result.add(onMatch(match)); | ||
startIndex = match.end; | ||
} | ||
result.add(onNonMatch(receiver.substring(startIndex))); | ||
return result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identical to |
||
} | ||
|
||
@notNull | ||
String replaceFirst( | ||
Pattern from, | ||
|
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.
(Just style comments, not sure this is the design we'll do.)
Should be private. All helper functions should.