-
Notifications
You must be signed in to change notification settings - Fork 49
Add a string-specific search algorithm #715
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2fadbbe
Add a string-specific search algorithm
natecook1000 714c2a0
...
natecook1000 ded493c
Add string-specific implementation for replacing
natecook1000 c06263a
Add CollectionSearcher conformance to new search
natecook1000 aa38dea
Substring search: iterative rather than recursive
natecook1000 649f4c5
Dispatch string splitting to new searcher
natecook1000 f57b5e4
Remove generic on SubstringSearcher
natecook1000 474601f
Remove unnecessary inlining annotations
natecook1000 b1ebf74
Update string algorithms tests
natecook1000 8ff1237
Verify string/substring dispatch in algorithms
natecook1000 0e01cf4
Add tests for string.replacing maxReplacements
natecook1000 9b9cd8a
Add fallback to naive search for small patterns
natecook1000 58ee711
Improve some comments/formatting in the string search
natecook1000 f5a6bee
Merge branch 'main' into string-search-algorithm
natecook1000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,32 @@ | |
|
||
// MARK: `CollectionSearcher` algorithms | ||
|
||
extension Substring { | ||
func _replacingSubstring( | ||
_ other: Substring, | ||
with replacement: Substring, | ||
maxReplacements: Int = .max | ||
) -> String { | ||
precondition(maxReplacements >= 0) | ||
|
||
var result = String() | ||
var index = startIndex | ||
|
||
var rangeIterator = SubstringSearcher(text: self, pattern: other) | ||
var replacementCount = 0 | ||
while replacementCount < maxReplacements, let range = rangeIterator.next() { | ||
result.append(contentsOf: self[index..<range.lowerBound]) | ||
result.append(contentsOf: replacement) | ||
index = range.upperBound | ||
|
||
replacementCount += 1 | ||
} | ||
|
||
result.append(contentsOf: self[index...]) | ||
return result | ||
} | ||
} | ||
|
||
extension RangeReplaceableCollection { | ||
func _replacing<Ranges: Collection, Replacement: Collection>( | ||
_ ranges: Ranges, | ||
|
@@ -35,19 +61,6 @@ extension RangeReplaceableCollection { | |
result.append(contentsOf: self[index...]) | ||
return result | ||
} | ||
|
||
mutating func _replace< | ||
Ranges: Collection, Replacement: Collection | ||
>( | ||
_ ranges: Ranges, | ||
with replacement: Replacement, | ||
maxReplacements: Int = .max | ||
) where Ranges.Element == Range<Index>, Replacement.Element == Element { | ||
self = _replacing( | ||
ranges, | ||
with: replacement, | ||
maxReplacements: maxReplacements) | ||
} | ||
} | ||
|
||
// MARK: Fixed pattern algorithms | ||
|
@@ -70,10 +83,40 @@ extension RangeReplaceableCollection where Element: Equatable { | |
subrange: Range<Index>, | ||
maxReplacements: Int = .max | ||
) -> Self where C.Element == Element, Replacement.Element == Element { | ||
_replacing( | ||
self[subrange]._ranges(of: other), | ||
with: replacement, | ||
maxReplacements: maxReplacements) | ||
switch (self, other, replacement) { | ||
case (let str as String, let other as String, let repl as String): | ||
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. This is a bit painful but I don't see a better way to do it |
||
return str[...]._replacingSubstring(other[...], with: repl[...], | ||
maxReplacements: maxReplacements) as! Self | ||
case (let str as Substring, let other as Substring, let repl as Substring): | ||
return str._replacingSubstring(other, with: repl, | ||
maxReplacements: maxReplacements)[...] as! Self | ||
|
||
case (let str as Substring, let other as String, let repl as String): | ||
return str[...]._replacingSubstring(other[...], with: repl[...], | ||
maxReplacements: maxReplacements)[...] as! Self | ||
case (let str as String, let other as Substring, let repl as String): | ||
return str[...]._replacingSubstring(other[...], with: repl[...], | ||
maxReplacements: maxReplacements) as! Self | ||
case (let str as String, let other as String, let repl as Substring): | ||
return str[...]._replacingSubstring(other[...], with: repl[...], | ||
maxReplacements: maxReplacements) as! Self | ||
|
||
case (let str as String, let other as Substring, let repl as Substring): | ||
return str[...]._replacingSubstring(other[...], with: repl[...], | ||
maxReplacements: maxReplacements) as! Self | ||
case (let str as Substring, let other as String, let repl as Substring): | ||
return str[...]._replacingSubstring(other[...], with: repl[...], | ||
maxReplacements: maxReplacements)[...] as! Self | ||
case (let str as Substring, let other as Substring, let repl as String): | ||
return str[...]._replacingSubstring(other[...], with: repl[...], | ||
maxReplacements: maxReplacements)[...] as! Self | ||
|
||
default: | ||
return _replacing( | ||
self[subrange]._ranges(of: other), | ||
with: replacement, | ||
maxReplacements: maxReplacements) | ||
} | ||
} | ||
|
||
/// Returns a new collection in which all occurrences of a target sequence | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 was about to comment about the extra Array allocation then saw there was already a fixme about it