Files icon indicating copy to clipboard operation
Files copied to clipboard

Location.nameExcludingExtension removes dots from filenames

Open maximkrouk opened this issue 3 years ago • 4 comments

maximkrouk avatar Jan 30 '21 13:01 maximkrouk

The dot is part of the extension. Why would you want the dot at the end???

jflow avatar Jan 30 '21 19:01 jflow

alternatively, use

(self as NSString).deletingPathExtension

honghaoz avatar Sep 25 '22 00:09 honghaoz

@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 🌚

maximkrouk avatar Sep 25 '22 02:09 maximkrouk

@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 😅)

maximkrouk avatar Sep 25 '22 02:09 maximkrouk