Last active
April 15, 2022 19:50
-
-
Save krzyzanowskim/a289ea3ad1afadba45a942b06dcb2a9f to your computer and use it in GitHub Desktop.
Stride String
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
extension String { | |
func stride(by distance: String.IndexDistance, substring: (String.SubSequence) -> Void) { | |
guard distance > 0, distance < count else { | |
substring(self[...]) | |
return | |
} | |
var i = index(startIndex, offsetBy: distance, limitedBy: endIndex) ?? endIndex | |
var previ = startIndex | |
while i < endIndex { | |
substring(self[previ..<i]) | |
previ = i | |
i = index(i, offsetBy: distance, limitedBy: endIndex) ?? endIndex | |
} | |
substring(self[previ..<i]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment