Peter Silie Peter Silie - 1 year ago 105
Swift Question

Swift 3: Fast file path separation

I need to split parts of a file path for thousand of files. So I need a fast function. I wrote this by my own, but it seems to run very slow:

// find string in "str", split at the position, delivers left and right side

func revFindSplit_(str : String, searchString : String) -> (String, String) {
let strr = String(str.characters.reversed()) // reverse main string
let searchStringr = String(searchString.characters.reversed()) // reverse search string too

if let strrindex = strr.range(of: searchStringr) { // now we can search from back to front

let rangeOfS2 = strr.characters.startIndex..<strrindex.lowerBound // we got search result: part 1 (front)
let rangeOfS1 = strrindex.lowerBound..<strr.characters.endIndex // part 2 (back)
let S2 = String(strr[rangeOfS2].characters.reversed()) // put together and reverse again
let S1 = String(strr[rangeOfS1].characters.reversed())

// here we have to remove search string
let S1M1 = String(S1.characters.prefix(S1.characters.count - searchString.characters.count))

return (S1M1, S2)
else {
return (str, "") // without splitting

// split path of filename into all 3 part: directory name, filename without extension, extension (without dot)
// in one call (faster than the 3 functions separatly)
func splitFilename_(str: String) -> (directory: String, filenameOnly: String, ext: String) {
let url = NSURL(fileURLWithPath: str)
let path = url.path!

// split path and filename
let (directory, filenameExt) = revFindSplit_(str: path, searchString: "/")
// split filename in filenameOnly and extension
let (filenameOnly, ext) = revFindSplit_(str: filenameExt, searchString: ".")

return (directory, filenameOnly, ext )

Is there any way to speed it up dramatically? (maybe using library functions)

Answer Source

You can reduce your entire code:

func splitFilename(str: String) -> (directory: String, filenameOnly: String, ext: String) {
  let url = URL(fileURLWithPath: str)
  return (url.deletingLastPathComponent().path, url.deletingPathExtension().lastPathComponent, url.pathExtension)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download