Open maximkrouk opened 3 years ago
alternatively, use
(self as NSString).deletingPathExtension
@jflow, didn't see your comment at the time 😅
Not in the end, but in the middle
For example R.generated.swift
for Rswift - the extension is swift
, the name is R.generated
, but the current implementation of nameExcludingExtension
returns Rgenerated
, and an empty string for hidden files like .swiftlint
🌚
@honghaoz, not sure that casting to NSString
is a better option, as for the library it might be better to use something like
extension String {
var nameExcludingExtension: String {
guard
let index = lastIndex(of: "."),
index != startIndex
else { return self }
return String(self[startIndex..<index])
}
}
That should work faster, so you don't have to go through the whole string to find the dots, but only iterate from the end to the first dot, if my assumption about
lastIndex(of:)
implementation is correct (yeaah, microoptimisations 😎)
And also this implementation will handle hidden files correctly like .swiftlint
(=> ".swiftlint" instead of ""), I may update the PR if @JohnSundell is interested (a bunch of PRs are here kinda unnoticed btw, so maybe it's time to review them 😅)
The dot is part of the extension. Why would you want the dot at the end???