<commit_before>/*
 
 FilePermissionsFormatter.swift
 
 CotEditor
 https://coteditor.com
 
 Created by 1024jp on 2016-06-02.
 
 ------------------------------------------------------------------------------
 
 © 2016 1024jp
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 https://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 
 */

import Foundation

final class FilePermissionsFormatter: Formatter {
    
    // MARK: Formatter Function
    
    /// format permission number to human readable permission expression
    override func string(for obj: Any?) -> String? {
        
        guard let permission = obj as? UInt else { return nil }
        
        return String(format: "%lo (%@)", permission, humanReadable(permission: permission))
    }
    
    
    /// disable backwards formatting
    override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
        
        return false
    }
    
}



// MARK:
// MARK: Private Function

/// create human-readable permission expression from integer
private func humanReadable(permission: UInt) -> String {
    
    let units = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"]
    
    return (0...2).reversed()
        .map { index -> Int in return (Int(permission) >> (index * 3)) & 0x7 }
        .reduce("-") { (string, digit) -> String in return string + units[digit] }  // Document is always file.
}<commit_msg>Specifiy type of variables in humanReadable()<commit_after>/*
 
 FilePermissionsFormatter.swift
 
 CotEditor
 https://coteditor.com
 
 Created by 1024jp on 2016-06-02.
 
 ------------------------------------------------------------------------------
 
 © 2016 1024jp
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 https://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 
 */

import Foundation

final class FilePermissionsFormatter: Formatter {
    
    // MARK: Formatter Function
    
    /// format permission number to human readable permission expression
    override func string(for obj: Any?) -> String? {
        
        guard let permission = obj as? UInt else { return nil }
        
        return String(format: "%lo (%@)", permission, humanReadable(permission: permission))
    }
    
    
    /// disable backwards formatting
    override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
        
        return false
    }
    
}



// MARK: - Private Function

/// create human-readable permission expression from integer
private func humanReadable(permission: UInt) -> String {
    
    let units = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"]
    
    return (0...2).reversed()
        .map { index -> Int in return (Int(permission) >> (index * 3)) & 0x7 }
        .reduce("-") { (string: String, digit: Int) -> String in return string + units[digit] }  // Document is always file.
}
{'fraction_non_alphanumeric': 0.14073694984646878, 'fraction_numerical': 0.014841351074718526, 'mean_word_length': 2.547186932849365, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '4777', 'n_tokens_mistral': 1073, 'n_tokens_neox': 966, 'n_words': 390}