Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Location.nameExcludingExtension removes dots from filenames #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

maximkrouk
Copy link

No description provided.

@jflow
Copy link

jflow commented Jan 30, 2021

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

@honghaoz
Copy link

alternatively, use

(self as NSString).deletingPathExtension

@maximkrouk
Copy link
Author

maximkrouk commented Sep 25, 2022

@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
Copy link
Author

maximkrouk commented Sep 25, 2022

@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 maximkrouk changed the title fix: Location.nameExcludingExtension removes dots from filenames Location.nameExcludingExtension removes dots from filenames Sep 25, 2022
@maximkrouk maximkrouk mentioned this pull request Sep 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants