-
-
Save JannikArndt/22c7eaf0cb49a9406a478110c4b655ae to your computer and use it in GitHub Desktop.
Function that "sanitizes" string to safe filename string that can be used on Mac, Linux and Windows.
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
var str = "2018/12/06 12:28 \\ - Ourdoor Run: Making a habbit.fgworkout" | |
extension String { | |
func sanitized() -> String { | |
// see for ressoning on charachrer sets https://superuser.com/a/358861 | |
let invalidCharacters = CharacterSet(charactersIn: "\\/:*?\"<>|") | |
.union(.newlines) | |
.union(.illegalCharacters) | |
.union(.controlCharacters) | |
return self | |
.components(separatedBy: invalidCharacters) | |
.joined(separator: "") | |
} | |
mutating func sanitize() -> Void { | |
self = self.sanitized() | |
} | |
func whitespaceCondensed() -> String { | |
return self.components(separatedBy: .whitespacesAndNewlines) | |
.filter { !$0.isEmpty } | |
.joined(separator: " ") | |
} | |
mutating func condenseWhitespace() -> Void { | |
self = self.whitespaceCondensed() | |
} | |
} | |
str.sanitized().whitespaceCondensed() // "20181206 1228 - Ourdoor Run Making a habbit.fgworkout" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment