       directories = []
       print("Error parsing file system item: \(error)")
     }
-    
+
     if behavior.includesFilesFromRootOfGlobstar {
       // Check the base directory for the glob star as well.
-      directories.insert(firstPart, atIndex: 0)
-      
+      directories.insert(firstPart, at: 0)
+
       // Include the globstar root directory ("dir/") in a pattern like "dir/**" or "dir/**/"
       if lastPart.isEmpty {
         results.append(firstPart)
       }
     }
-    
+
     if lastPart.isEmpty {
       lastPart = "*"
     }
     for directory in directories {
-      let partiallyResolvedPattern = NSString(string: directory).stringByAppendingPathComponent(lastPart)
-      results.appendContentsOf(expandGlobstar(partiallyResolvedPattern))
-    }
-    
+      let partiallyResolvedPattern = NSString(string: directory).appendingPathComponent(lastPart)
+      results.append(contentsOf: expandGlobstar(pattern: partiallyResolvedPattern))
+    }
+
     return results
   }
-  
+
   private func isDirectory(path: String) -> Bool {
     var isDirectory = isDirectoryCache[path]
     if let isDirectory = isDirectory {
       return isDirectory
     }
-    
+
     var isDirectoryBool = ObjCBool(false)
-    isDirectory = NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDirectoryBool) && isDirectoryBool
+    isDirectory = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectoryBool) && isDirectoryBool.boolValue
     isDirectoryCache[path] = isDirectory!
-    
+
     return isDirectory!
   }
-  
+
   private func clearCaches() {
     isDirectoryCache.removeAll()
   }
-  
+
   private func populateFiles(gt: glob_t, includeFiles: Bool) {
     let includeDirectories = behavior.includesDirectoriesInResults
-    
+
     for i in 0..<Int(gt.gl_matchc) {
-      if let path = String.fromCString(gt.gl_pathv[i]) {
+      if let path = String(validatingUTF8: gt.gl_pathv[i]!) {
         if !includeFiles || !includeDirectories {
-          let isDirectory = self.isDirectory(path)
+          let isDirectory = self.isDirectory(path: path)
           if (!includeFiles && !isDirectory) || (!includeDirectories && isDirectory) {
             continue
           }
         }
-        
+
         paths.append(path)
       }
     }
   }
-  
-  // MARK: Subscript Support
-  
-  subscript(i: Int) -> String {
-    return paths[i]
-  }
 }

        adjustedPattern += "*"
      }
    }
    
    let patterns = behavior.supportsGlobstar ? expandGlobstar(adjustedPattern) : [adjustedPattern]
    
    for pattern in patterns {
      var gt = glob_t()
      if executeGlob(pattern, gt: &gt) {
        populateFiles(gt, includeFiles: includeFiles)
      }
      
      globfree(&gt)
    }
    
    paths = Array(Set(paths)).sort { lhs, rhs in
      lhs.compare(rhs) != NSComparisonResult.OrderedDescending
    }
    
    clearCaches()
  }
  
  // MARK: Private
  
  private var globalFlags = GLOB_TILDE | GLOB_BRACE | GLOB_MARK
  
  private func executeGlob(pattern: UnsafePointer<CChar>, gt: UnsafeMutablePointer<glob_t>) -> Bool {
    return 0 == glob(pattern, globalFlags, nil, gt)
  }
  
  private func expandGlobstar(pattern: String) -> [String] {
    guard pattern.containsString("**") else {
      return [pattern]
    }
    
    var results = [String]()
    var parts = pattern.componentsSeparatedByString("**")
    let firstPart = parts.removeFirst()
    var lastPart = parts.joinWithSeparator("**")
    
    let fileManager = NSFileManager.defaultManager()
    
    var directories: [String]
    
    do {
      directories = try fileManager.subpathsOfDirectoryAtPath(firstPart).flatMap { subpath in
        let fullPath = NSString(string: firstPart).stringByAppendingPathComponent(subpath)
        var isDirectory = ObjCBool(false)
        if fileManager.fileExistsAtPath(fullPath, isDirectory: &isDirectory) && isDirectory {
          return fullPath
        } else {
          return nil

//  Released under the Apache v2 License.
//
//  Adapted from https://gist.github.com/blakemerryman/76312e1cbf8aec248167
//  Adapted from https://gist.github.com/efirestone/ce01ae109e08772647eb061b3bb387c3


import Foundation


public let GlobBehaviorBashV3 = Glob.Behavior(
  supportsGlobstar: false,
  includesFilesFromRootOfGlobstar: false,
  includesDirectoriesInResults: true,
  includesFilesInResultsIfTrailingSlash: false
)
public let GlobBehaviorBashV4 = Glob.Behavior(
  supportsGlobstar: true, // Matches Bash v4 with "shopt -s globstar" option
  includesFilesFromRootOfGlobstar: true,
  includesDirectoriesInResults: true,
  includesFilesInResultsIfTrailingSlash: false
)
public let GlobBehaviorGradle = Glob.Behavior(
  supportsGlobstar: true,
  includesFilesFromRootOfGlobstar: true,
  includesDirectoriesInResults: false,
