Skip to content

Instantly share code, notes, and snippets.

@akkyie
Created December 2, 2019 08:16
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 akkyie/b573fc4b118b6b3af375c370cb1fb540 to your computer and use it in GitHub Desktop.
Save akkyie/b573fc4b118b6b3af375c370cb1fb540 to your computer and use it in GitHub Desktop.
func getFilename(_ string: StaticString) -> String {
guard string.hasPointerRepresentation else {
return String(string.unicodeScalar)
}
var index = string.utf8CodeUnitCount
while index > 1 {
index -= 1
let pointer = string.utf8Start + index
if UnicodeScalar(pointer.predecessor().pointee) == "/" {
return String(cString: pointer)
}
}
return String(cString: string.utf8Start)
}
print(filename("foo/bar/hoge.swift"))
print(filename("hoge.swift"))
print(filename("hoge.swift////"))
print(filename(""))
print(filename("/"))
print(filename("///"))
print(filename("あいう/えお/😆😆😆.swift"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment