Created
December 26, 2016 16:28
-
-
Save marcelofabri/d0bbc47ddaba8f580bf630f84a23c272 to your computer and use it in GitHub Desktop.
This file contains 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
private func split(_ str: String, _ length: Int) -> [String] { | |
// based on http://stackoverflow.com/a/38980231/1777634 | |
return stride(from: 0, to: str.characters.count, by: length).map { idx -> String in | |
let startIndex = str.index(str.startIndex, offsetBy: idx) | |
let endIndex = str.index(startIndex, offsetBy: length, | |
limitedBy: str.endIndex) ?? str.endIndex | |
return str[startIndex..<endIndex] | |
} | |
} | |
extension CharacterSet { | |
init(fixedCharactersIn string: String) { | |
#if os(Linux) | |
// workaround for https://bugs.swift.org/browse/SR-3215 and | |
// https://bugs.swift.org/browse/SR-3282 | |
let sets = split(string, 63).map { CharacterSet(charactersIn: $0) } | |
let finalSet = sets.reduce(CharacterSet()) { (acc, set) -> CharacterSet in | |
acc.union(set) | |
} | |
self = finalSet | |
#else | |
self.init(charactersIn: string) | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment