Replace Foundation replacingOccurrences(of:with:) and percent encoding#176
Open
madsodgaard wants to merge 2 commits intoapple:mainfrom
Open
Replace Foundation replacingOccurrences(of:with:) and percent encoding#176madsodgaard wants to merge 2 commits intoapple:mainfrom
replacingOccurrences(of:with:) and percent encoding#176madsodgaard wants to merge 2 commits intoapple:mainfrom
Conversation
9 tasks
replacingOccurrences(of:with:) and percent encodingreplacingOccurrences(of:with:) and percent encoding
czechboy0
reviewed
Feb 27, 2026
Contributor
czechboy0
left a comment
There was a problem hiding this comment.
Thanks @madsodgaard - got some suggestions on this one
Comment on lines
+37
to
+76
| @inlinable func replacingOccurrences<Target: StringProtocol, Replacement: StringProtocol>( | ||
| of target: Target, | ||
| with replacement: Replacement, | ||
| maxReplacements: Int = .max | ||
| ) -> String { | ||
| guard !target.isEmpty, maxReplacements > 0 else { return String(self) } | ||
|
|
||
| var result = "" | ||
| result.reserveCapacity(self.count) | ||
|
|
||
| var currentIndex = self.startIndex | ||
| let end = self.endIndex | ||
| var replacementsCount = 0 | ||
|
|
||
| // Keeps track of the last un-appended chunk | ||
| var chunkStartIndex = currentIndex | ||
|
|
||
| while currentIndex < end { | ||
| if replacementsCount == maxReplacements { break } | ||
|
|
||
| let remainder = self[currentIndex...] | ||
|
|
||
| if remainder.starts(with: target) { | ||
| result.append(contentsOf: self[chunkStartIndex..<currentIndex]) | ||
| result.append(contentsOf: replacement) | ||
|
|
||
| currentIndex = self.index(currentIndex, offsetBy: target.count) | ||
| chunkStartIndex = currentIndex | ||
|
|
||
| replacementsCount += 1 | ||
| } else { | ||
| currentIndex = self.index(after: currentIndex) | ||
| } | ||
| } | ||
|
|
||
| // Append any remaining characters after the final match | ||
| if chunkStartIndex < end { result.append(contentsOf: self[chunkStartIndex...]) } | ||
|
|
||
| return result | ||
| } |
Contributor
There was a problem hiding this comment.
Suggested change
| @inlinable func replacingOccurrences<Target: StringProtocol, Replacement: StringProtocol>( | |
| of target: Target, | |
| with replacement: Replacement, | |
| maxReplacements: Int = .max | |
| ) -> String { | |
| guard !target.isEmpty, maxReplacements > 0 else { return String(self) } | |
| var result = "" | |
| result.reserveCapacity(self.count) | |
| var currentIndex = self.startIndex | |
| let end = self.endIndex | |
| var replacementsCount = 0 | |
| // Keeps track of the last un-appended chunk | |
| var chunkStartIndex = currentIndex | |
| while currentIndex < end { | |
| if replacementsCount == maxReplacements { break } | |
| let remainder = self[currentIndex...] | |
| if remainder.starts(with: target) { | |
| result.append(contentsOf: self[chunkStartIndex..<currentIndex]) | |
| result.append(contentsOf: replacement) | |
| currentIndex = self.index(currentIndex, offsetBy: target.count) | |
| chunkStartIndex = currentIndex | |
| replacementsCount += 1 | |
| } else { | |
| currentIndex = self.index(after: currentIndex) | |
| } | |
| } | |
| // Append any remaining characters after the final match | |
| if chunkStartIndex < end { result.append(contentsOf: self[chunkStartIndex...]) } | |
| return result | |
| } | |
| @inlinable func replacingOccurrences<Replacement: StringProtocol>( | |
| of target: String, | |
| with replacement: Replacement, | |
| maxReplacements: Int = .max | |
| ) -> String { | |
| guard !target.isEmpty, maxReplacements > 0 else { return String(self) } | |
| var result = "" | |
| result.reserveCapacity(self.count) | |
| var searchStart = self.startIndex | |
| var replacements = 0 | |
| while | |
| replacements < maxReplacements, | |
| let foundRange = self.range(of: target, range: searchStart..<self.endIndex) | |
| { | |
| result.append(contentsOf: self[searchStart..<foundRange.lowerBound]) | |
| result.append(contentsOf: replacement) | |
| searchStart = foundRange.upperBound | |
| replacements += 1 | |
| } | |
| result.append(contentsOf: self[searchStart..<self.endIndex]) | |
| return result | |
| } |
I couldn't convince myself that currentIndex = self.index(currentIndex, offsetBy: target.count) was actually always going to be correct, so how about this alternative that sidesteps that problem, and only ever uses indexes with the string that originated them?
Contributor
Author
There was a problem hiding this comment.
Whoops, I was a bit too quick here. range(of:) is not in FoundationEssentials. When are you worried that the above code would be incorrect?
czechboy0
reviewed
Feb 27, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Part of apple/swift-openapi-generator#868
Introduces a Swift impl of
replacingOccurrences(of:with:),addingPercentEncodingandremovingPercentEncoding.The percent encoding were inspired by impl in
swift-foundation: https://github.com/swiftlang/swift-foundation/blob/aee1d337039b5b2a1f0c3b7f83baa950dac3a84e/Sources/FoundationEssentials/URL/URLParser.swiftI also added tests to verify behaviour with Foundation.