Skip to content

Instantly share code, notes, and snippets.

@marcelofabri
Created December 26, 2016 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelofabri/d0bbc47ddaba8f580bf630f84a23c272 to your computer and use it in GitHub Desktop.
Save marcelofabri/d0bbc47ddaba8f580bf630f84a23c272 to your computer and use it in GitHub Desktop.
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