<commit_before>//
//  URL.swift
//  DarkSwift
//
//  Created by <NAME> on 2017/4/8.
//  Copyright © 2017年 <NAME>. All rights reserved.
//

import Foundation

public extension URL {
    static func directory(for directory: FileManager.SearchPathDirectory, in domainMask: FileManager.SearchPathDomainMask = .userDomainMask) -> URL {
        return FileManager.default.urls(for: directory, in: domainMask)[0]
    }
    
    static var documentDirectory: URL {
        return directory(for: .documentDirectory)
    }
    
    static var cachesDirectory: URL {
        return directory(for: .cachesDirectory)
    }
    
    static var temporaryDirectory: URL {
        return URL(fileURLWithPath: NSTemporaryDirectory())
    }

    static func temporaryFile(pathExtension: String? = nil) -> URL {
        let filename = UUID().uuidString
        let extname: String
        if let ext = pathExtension, !ext.isEmpty {
            extname = "." + ext
        } else {
            extname = ""
        }
        return URL.temporaryDirectory.appendingPathComponent(filename + extname)
    }
    
    var fileSize: UInt64 {
        do {
            let attributes = try FileManager.default.attributesOfItem(atPath: path)
            return attributes[FileAttributeKey.size] as! UInt64
        } catch {
            print(error)
            return 0
        }
    }
    
    func createDirectory(withIntermediateDirectories: Bool = true, attributes: [String: Any]? = nil) {
        let manager = FileManager.default
        guard !manager.fileExists(atPath: path) else {
            return
        }
        do {
            try manager.createDirectory(at: self, withIntermediateDirectories: withIntermediateDirectories, attributes: attributes)<commit_msg>Add two methods to URL ext<commit_after>//
//  URL.swift
//  DarkSwift
//
//  Created by <NAME> on 2017/4/8.
//  Copyright © 2017年 <NAME>. All rights reserved.
//

import Foundation
import MobileCoreServices

public extension URL {
    static func directory(for directory: FileManager.SearchPathDirectory, in domainMask: FileManager.SearchPathDomainMask = .userDomainMask) -> URL {
        return FileManager.default.urls(for: directory, in: domainMask)[0]
    }
    
    static var documentDirectory: URL {
        return directory(for: .documentDirectory)
    }
    
    static var cachesDirectory: URL {
        return directory(for: .cachesDirectory)
    }
    
    static var temporaryDirectory: URL {
        return URL(fileURLWithPath: NSTemporaryDirectory())
    }

    static func temporaryFile(pathExtension: String? = nil) -> URL {
        let filename = UUID().uuidString
        let extname: String
        if let ext = pathExtension, !ext.isEmpty {
            extname = "." + ext
        } else {
            extname = ""
        }
        return URL.temporaryDirectory.appendingPathComponent(filename + extname)
    }
    
    var fileSize: UInt64 {
        do {
            let attributes = try FileManager.default.attributesOfItem(atPath: path)
            return attributes[FileAttributeKey.size] as! UInt64
        } catch {
            print(error)
            return 0
        }
    }
    
    //uniform type identifier (UTI)
    func conformToUTI(_ uti: String) -> Bool {
        if let values = try? resourceValues(forKeys: [.typeIdentifierKey]), let type = values.typeIdentifier {
            return UTTypeConformsTo(type as CFString, uti as CFString)
        }
        return false
    }
    
    //assume url as base URL, return path = self.path - base
    func pathComponentRelativeTo(_ url: URL) -> String? {
        let stdPath = standardizedFileURL.path
        let relativeStdPath = url.standardizedFileURL.path
        if stdPath.hasPrefix(relativeStdPath) {
            let index = relativeStdPath.index(relativeStdPath.startIndex, offsetBy: relativeStdPath.characters.count)
            return stdPath.substring(from: index)
        }
        return nil
    }
    
    func createDirectory(withIntermediateDirectories: Bool = true, attributes: [String: Any]? = nil) {
        let manager = FileManager.default
        guard !manager.fileExists(atPath: path) else {
            return
        }
        do {
            try manager.createDirectory(at: self, withIntermediateDirectories: withIntermediateDirectories, attributes: attributes)
{'fraction_non_alphanumeric': 0.08752025931928688, 'fraction_numerical': 0.007409122482056031, 'mean_word_length': 2.367108339828527, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5800286', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1151, 'n_words': 307}