<commit_before>import Foundation

class SwiftParser {

    enum FormatError: Error {
        case stringError
    }

    fileprivate static let OperatorList: [Character: [String]] = [
        "+": ["+=<", "+=", "+++=", "+++", "+"],
        "-": ["->", "-=", "-<<"],
        "*": ["*=", "*"],
        "/": ["/=", "/"],
        "~": ["~=", "~~>", "~>"],
        "%": ["%=", "%"],
        "^": ["^="],
        "&": ["&&=", "&&&", "&&", "&=", "&+", "&-", "&*", "&/", "&%"],
        "<": ["<<<", "<<=", "<<", "<=", "<~~", "<~", "<--", "<-<", "<-", "<^>", "<|>", "<*>", "<||?", "<||", "<|?", "<|", "<"],
        ">": [">>>", ">>=", ">>-", ">>", ">=", ">->", ">"],
        "|": ["|||", "||=", "||", "|=", "|"],
        "!": ["!==", "!="],
        "=": ["===", "==", "="]
    ]
    fileprivate static let NegativeCheckSigns: [Character] = ["+", "-", "*", "/", "&", "|", "^", "<", ">", ":", "(", "[", "{", "=", ",", ".", "?"]
    fileprivate static let NegativeCheckKeys = ["case", "return", "if", "for", "while", "in"]
    fileprivate static let Numbers: [Character] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

    static var indentChar: String = ""
    let string: String
    var retString = ""
    var strIndex: String.Index
    var blockStack = [Block]()
    var blockType: BlockType = .curly
    var indent = 0
    var tempIndent = 0
    var inSwitch = false
    var switchCount = 0
    var newlineIndex = 0

    enum BlockType: Character {
        case parentheses = "(", square = "[", curly = "{"
    }

    struct Block {
        let indent: Int
        let tempIndent: Int
        let indentCount: Int
        let type: BlockType
    }

    init(string: String) {
        self.string = string
        strIndex = string.startIndex
    }

    func format() throws -> String {
        while strIndex < string.endIndex {
            let char = string[strIndex]
            strIndex = try checkChar(char)
        }
        return retString.trim()
    }

    func checkChar(_ char: Character) throws -> String.Index {
        switch char {
        case "+", "*", "%", ">", "|", "=":
            return spaceWithArray(SwiftParser.OperatorList[char]!)!
        case "-":
            if let index = spaceWithArray(SwiftParser.OperatorList[char]!) {
                return index
            } else {
                var noSpace = false
                if retString.count > 0 {
                    // check scientific notation
                    if strIndex != string.endIndex {
                        if string[string.index(before: strIndex)] == "e" && SwiftParser.Numbers.contains(string[string.index(after: strIndex)]) {
                            noSpace = true
                        }
                    }
                    // check negative
                    let last = retString.lastNonSpaceChar(retString.endIndex)
                    if last.isAZ() {
                        if SwiftParser.NegativeCheckKeys.contains(retString.lastWord()) {
                            noSpace = true
                        }
                    } else {
                        if SwiftParser.NegativeCheckSigns.contains(last) {
                            noSpace = true
                        }
                    }
                }
                if noSpace {
                    return addChar(char)
                }
                return spaceWith("-")
            }
        case "~", "^", "!", "&":
            if let index = spaceWithArray(SwiftParser.OperatorList[char]!) {
                return index
            }
            return addChar(char)
        case ".":
            if isNextChar(".") {
                if isNextString("...") {<commit_msg>* improve performance<commit_after>import Foundation

class SwiftParser {

    enum FormatError: Error {
        case stringError
    }

    fileprivate static let OperatorList: [Character: [String]] = [
        "+": ["+=<", "+=", "+++=", "+++", "+"],
        "-": ["->", "-=", "-<<"],
        "*": ["*=", "*"],
        "/": ["/=", "/"],
        "~": ["~=", "~~>", "~>"],
        "%": ["%=", "%"],
        "^": ["^="],
        "&": ["&&=", "&&&", "&&", "&=", "&+", "&-", "&*", "&/", "&%"],
        "<": ["<<<", "<<=", "<<", "<=", "<~~", "<~", "<--", "<-<", "<-", "<^>", "<|>", "<*>", "<||?", "<||", "<|?", "<|", "<"],
        ">": [">>>", ">>=", ">>-", ">>", ">=", ">->", ">"],
        "|": ["|||", "||=", "||", "|=", "|"],
        "!": ["!==", "!="],
        "=": ["===", "==", "="]
    ]
    fileprivate static let NegativeCheckSigns: [Character] = ["+", "-", "*", "/", "&", "|", "^", "<", ">", ":", "(", "[", "{", "=", ",", ".", "?"]
    fileprivate static let NegativeCheckKeys = ["case", "return", "if", "for", "while", "in"]
    fileprivate static let Numbers: [Character] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

    static var indentChar: String = ""
    let string: String
    var retString = ""
    var strIndex: String.Index
    var blockStack = [Block]()
    var blockType: BlockType = .curly
    var indent = 0
    var tempIndent = 0
    var inSwitch = false
    var switchCount = 0
    var newlineIndex = 0

    enum BlockType: Character {
        case parentheses = "(", square = "[", curly = "{"
    }

    struct Block {
        let indent: Int
        let tempIndent: Int
        let indentCount: Int
        let type: BlockType
    }

    init(string: String) {
        self.string = string
        strIndex = string.startIndex
    }

    func format() throws -> String {
        while strIndex < string.endIndex {
            let char = string[strIndex]
            strIndex = try checkChar(char)
        }
        return retString.trim()
    }

    func checkChar(_ char: Character) throws -> String.Index {
        switch char {
        case "+", "*", "%", ">", "|", "=":
            return spaceWithArray(SwiftParser.OperatorList[char]!)!
        case "-":
            if let index = spaceWithArray(SwiftParser.OperatorList[char]!) {
                return index
            } else {
                var noSpace = false
                if retString.count > 0 {
                    // check scientific notation
                    if strIndex != string.endIndex {
                        if retString.lastChar == "e" && SwiftParser.Numbers.contains(string[string.index(after: strIndex)]) {
                            noSpace = true
                        }
                    }
                    // check negative
                    let last = retString.lastNonSpaceChar(retString.endIndex)
                    if last.isAZ() {
                        if SwiftParser.NegativeCheckKeys.contains(retString.lastWord()) {
                            noSpace = true
                        }
                    } else {
                        if SwiftParser.NegativeCheckSigns.contains(last) {
                            noSpace = true
                        }
                    }
                }
                if noSpace {
                    return addChar(char)
                }
                return spaceWith("-")
            }
        case "~", "^", "!", "&":
            if let index = spaceWithArray(SwiftParser.OperatorList[char]!) {
                return index
            }
            return addChar(char)
        case ".":
            if isNextChar(".") {
                if isNextString("...") {
{'fraction_non_alphanumeric': 0.21670305676855894, 'fraction_numerical': 0.004093886462882096, 'mean_word_length': 1.5150995195607413, 'pattern_counts': {'":': 36, '<': 59, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5688508', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2094, 'n_words': 393}