<commit_before>//
//  DGXMLString.swift
//
//  The MIT License
//  Copyright (c) 2015 - 2017 <NAME>. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//

import Foundation

extension DGXMLDocument {
    
    public func xmlString(prettyPrinted: Bool = false) -> String {
        var result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
        self._xml(prettyPrinted: prettyPrinted, &result)
        return result
    }
    
    public func xmlData(prettyPrinted: Bool = false) -> Data {
        let xml = self.xmlString(prettyPrinted: prettyPrinted)
        return xml.data(using: .utf8) ?? Data()
    }
    
    private func _xml(prettyPrinted: Bool, _ output: inout String) {
        for element in self {
            element._xml(prettyPrinted ? "\n" : "", prefixMap: [:], &output)
        }
    }
}

extension DGXMLDocument : CustomStringConvertible {
    
    public var description: String {
        return xmlString()
    }
}

extension DGXMLElement {
    
    fileprivate func _xml(_ terminator: String, prefixMap: [String: String], _ output: inout String) {
        
        switch kind {
        case let .node(_name, namespace):
            
            var prefixMap = prefixMap
            
            for (key, value) in attributes.filter({ $0.key.hasPrefix("xmlns:") }) {
                let substr = String(key.dropFirst(6)).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
                if !substr.isEmpty && !substr.contains(":") {
                    prefixMap[value] = substr
                }
            }
            
            let name = namespace.flatMap { prefixMap[$0].map { "\($0):\(_name)" } } ?? _name
            
            if attributes.count == 0 {
                if self.count == 0 {
                    "\(terminator)<\(name) />".write(to: &output)
                } else {
                    "\(terminator)<\(name)>".write(to: &output)
                    for element in self {
                        element._xml(terminator == "" ? terminator : "\(terminator)  ", prefixMap: prefixMap, &output)
                    }
                    "\(terminator)</\(name)>".write(to: &output)
                }
            } else {
                let attributes = self.attributes.map { "\($0)=\"\($1)\"" }.joined(separator: " ")
                if self.count == 0 {
                    "\(terminator)<\(name) \(attributes) />".write(to: &output)
                } else {
                    "\(terminator)<\(name) \(attributes)>".write(to: &output)
                    for element in self {
                        element._xml(terminator == "" ? terminator : "\(terminator)  ", prefixMap: prefixMap, &output)
                    }
                    "\(terminator)</\(name)>".write(to: &output)
                }
            }
            
        case let .characters(value): value.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).write(to: &output)
        case let .comment(value): "\(terminator)<!--\(value)-->".write(to: &output)
        case let .CDATA(value): "\(terminator)<![CDATA[\(value)]]>".write(to: &output)
        }
        
    }
    
}<commit_msg>clean<commit_after>//
//  DGXMLString.swift
//
//  The MIT License
//  Copyright (c) 2015 - 2017 <NAME>. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//

import Foundation

extension DGXMLDocument {
    
    public func xmlString(prettyPrinted: Bool = false) -> String {
        var result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
        self._xml(prettyPrinted: prettyPrinted, &result)
        return result
    }
    
    public func xmlData(prettyPrinted: Bool = false) -> Data {
        let xml = self.xmlString(prettyPrinted: prettyPrinted)
        return xml.data(using: .utf8) ?? Data()
    }
    
    private func _xml(prettyPrinted: Bool, _ output: inout String) {
        for element in self {
            element._xml(prettyPrinted ? "\n" : "", prefixMap: [:], &output)
        }
    }
}

extension DGXMLDocument : CustomStringConvertible {
    
    public var description: String {
        return xmlString()
    }
}

extension DGXMLElement {
    
    fileprivate func _xml(_ terminator: String, prefixMap: [String: String], _ output: inout String) {
        
        switch kind {
        case let .node(_name, namespace):
            
            var prefixMap = prefixMap
            
            for (key, value) in attributes.filter({ $0.key.hasPrefix("xmlns:") }) {
                let substr = String(key.dropFirst(6)).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
                if !substr.isEmpty && !substr.contains(":") {
                    prefixMap[value] = substr
                }
            }
            
            let name = namespace.flatMap { prefixMap[$0].map { "\($0):\(_name)" } } ?? _name
            let _attributes = self.attributes.map { " \($0)=\"\($1)\"" }.joined()
            
            if self.count == 0 {
                "\(terminator)<\(name)\(_attributes) />".write(to: &output)
            } else {
                "\(terminator)<\(name)\(_attributes)>".write(to: &output)
                var flag = false
                for element in self {
                    flag = element.isNode || flag
                    element._xml(terminator == "" ? terminator : "\(terminator)  ", prefixMap: prefixMap, &output)
                }
                if flag {
                    "\(terminator)</\(name)>".write(to: &output)
                } else {
                    "</\(name)>".write(to: &output)
                }
            }
            
        case let .characters(value): value.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).write(to: &output)
        case let .comment(value): "\(terminator)<!--\(value)-->".write(to: &output)
        case let .CDATA(value): "\(terminator)<![CDATA[\(value)]]>".write(to: &output)
        }
        
    }
    
}
{'fraction_non_alphanumeric': 0.1225, 'fraction_numerical': 0.005, 'mean_word_length': 2.049161585365854, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 2, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 27}, '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': '4874387', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2129, 'n_words': 674}