Created
December 2, 2019 08:16
-
-
Save akkyie/b573fc4b118b6b3af375c370cb1fb540 to your computer and use it in GitHub Desktop.
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
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