-
-
Save aciidgh/598de079d8ced0891f7a 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
extension String { | |
func trimBackTo(boundary: Character) -> String { | |
if isEmpty {return ""} | |
if self[endIndex.predecessor()] == boundary { return "" } | |
return characters.split(boundary).map(String.init).last! | |
} | |
func trimUpTo(boundary: Character) -> String { | |
if isEmpty { return "" } | |
if self[startIndex] == boundary {return String(self[startIndex])} | |
return characters.split(boundary, maxSplit: 1, allowEmptySlices: false).map(String.init).first! | |
} | |
} |
erica
commented
Dec 28, 2015
extension String {
/// Retake on "lastPathComponent" and "pathExtension"
/// but a little more general in behavior
///
/// - Author: aciidb
func snipBackTo(boundary: Character) -> String {
if isEmpty || self[endIndex.predecessor()] == boundary { return "" }
return characters.split(boundary).map(String.init).last ?? self
}
/// Snip forward to boundary
///
/// - Author: aciidb
func snipUpTo(boundary: Character) -> String {
if isEmpty || self[startIndex] == boundary {return ""}
return characters.split(boundary, maxSplit: 1, allowEmptySlices: false).map(String.init).first ?? self
}
/// Trim back from end
func trimBackTo(boundary: Character) -> String {
if isEmpty {return ""}
if self[endIndex.predecessor()] == boundary {return self[startIndex...endIndex.predecessor().predecessor()]}
let last = characters.split(boundary).map(String.init).last ?? ""
if last == self {return self}
return String(characters.dropLast(last.characters.count + 1)) ?? self
}
/// Trim forward to boundary
func trimUpTo(boundary: Character) -> String {
if isEmpty { return "" }
if self[startIndex] == boundary { return self[startIndex.successor()...endIndex.predecessor()] }
let first = characters.split(boundary).map(String.init).first ?? ""
if first == self {return self}
return String(characters.dropFirst(first.characters.count + 1)) ?? self
}
}
var path = "/Users/ericasadun/Desktop/feefs.txt/filename.fext"
path.snipBackTo("Z") == path
path.snipBackTo("/") == "filename.fext"
path.snipBackTo(".") == "fext"
path.snipUpTo("Z") == path
path.snipUpTo("/") == ""
path.snipUpTo(".") == "/Users/ericasadun/Desktop/feefs"
path.trimBackTo("Z") == path
path.trimBackTo("/") == "/Users/ericasadun/Desktop/feefs.txt"
path.trimBackTo(".") == "/Users/ericasadun/Desktop/feefs.txt/filename"
path.trimBackTo("t") == "/Users/ericasadun/Desktop/feefs.txt/filename.fex"
path.trimUpTo("Z") == path
path.trimUpTo("/") == "Users/ericasadun/Desktop/feefs.txt/filename.fext"
path.trimUpTo(".") == "txt/filename.fext"
path = "x"
path.snipBackTo("Z") == "x"
path.snipUpTo("Z") == "x"
path.trimUpTo("Z") == "x"
path.trimBackTo("Z") == "x"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment