<commit_before>//
//  Regex.swift
//  SwiftExpression
//
//  Created by <NAME> on 7/31/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

public struct Regex {
    public typealias Match = (String, Range<String.Index>)
    
    private var regexPattern: NSRegularExpression
    
    public init?(pattern: String) {
        do {
            self.regexPattern = try NSRegularExpression(pattern: pattern, options: [])
        } catch {
            return nil
        }
    }
    
    func toString() -> String {
        return regexPattern.pattern
    }
    
    internal func matchWithPattern(str: String) -> [Match] {
        let results = regexPattern.matchesInString(str, options: .ReportCompletion, range: NSRange(location: 0, length: str.startIndex.distanceTo(str.endIndex)))
        var components = [(String, Range<String.Index>)]()
        results.lazy.forEach {
            let stringRange = str.startIndex.advancedBy($0.range.location)..<str.startIndex.advancedBy($0.range.location + $0.range.length)
            components.append(str.substringWithRange(stringRange), stringRange)
        }
        return components
    }
    
    internal func replaceMatchesInString(str: String, replacement: String) -> String {
        let replaceString = NSMutableString(string: str)
        regexPattern.replaceMatchesInString(replaceString, options: .ReportCompletion, range: NSRange(location: 0, length: str.startIndex.distanceTo(str.endIndex)), withTemplate: replacement)
        return replaceString as String
    }
    
    internal func search(str: String) -> Int? {
        if let result = regexPattern.firstMatchInString(str, options: .ReportCompletion, range: NSRange(location: 0, length: str.startIndex.distanceTo(str.endIndex))) {
            result.range.location
        }
        return nil
    }
}

prefix operator <> { }

prefix func <> (pattern: String) -> Regex? {
    return Regex(pattern: pattern)
}

infix operator =~ {}

func =~ (input: String, regex: Regex) -> Bool {
    if let match = regex.search(input) where match > 0 {
        return true
    }
    return false
}

extension String {
    public func match(regex: Regex) -> [Regex.Match] {
        return regex.matchWithPattern(self)
    }
    
    public func replace(regex: Regex, withString: String) -> String {
        return regex.replaceMatchesInString(self, replacement: withString)
    }
    
    public func search(regex: Regex) -> Int? {
        return regex.search(self)
    }
}
<commit_msg>Added string to string infix operator overload<commit_after>//
//  Regex.swift
//  SwiftExpression
//
//  Created by <NAME> on 7/31/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

public struct Regex {
    public typealias Match = (String, Range<String.Index>)
    
    private var regexPattern: NSRegularExpression
    
    public init?(pattern: String) {
        do {
            self.regexPattern = try NSRegularExpression(pattern: pattern, options: [])
        } catch {
            return nil
        }
    }
    
    func toString() -> String {
        return regexPattern.pattern
    }
    
    internal func matchWithPattern(str: String) -> [Match] {
        let results = regexPattern.matchesInString(str, options: .ReportCompletion, range: NSRange(location: 0, length: str.startIndex.distanceTo(str.endIndex)))
        var components = [(String, Range<String.Index>)]()
        results.lazy.forEach {
            let stringRange = str.startIndex.advancedBy($0.range.location)..<str.startIndex.advancedBy($0.range.location + $0.range.length)
            components.append(str.substringWithRange(stringRange), stringRange)
        }
        return components
    }
    
    internal func replaceMatchesInString(str: String, replacement: String) -> String {
        let replaceString = NSMutableString(string: str)
        regexPattern.replaceMatchesInString(replaceString, options: .ReportCompletion, range: NSRange(location: 0, length: str.startIndex.distanceTo(str.endIndex)), withTemplate: replacement)
        return replaceString as String
    }
    
    internal func search(str: String) -> Int? {
        if let result = regexPattern.firstMatchInString(str, options: .ReportCompletion, range: NSRange(location: 0, length: str.startIndex.distanceTo(str.endIndex))) {
            result.range.location
        }
        return nil
    }
}

prefix operator <> { }

prefix func <> (pattern: String) -> Regex? {
    return Regex(pattern: pattern)
}

infix operator =~ {}

func =~ (input: String, regex: Regex) -> Bool {
    if let match = regex.search(input) where match > 0 {
        return true
    }
    return false
}

func =~ (input: String, regexPatternStr: String) -> Bool {
    if let regex = Regex(pattern: regexPatternStr) {
        if let match = regex.search(input) where match > 0 {
            return true
        }
    }
    return false
}

extension String {
    public func match(regex: Regex) -> [Regex.Match] {
        return regex.matchWithPattern(self)
    }
    
    public func replace(regex: Regex, withString: String) -> String {
        return regex.replaceMatchesInString(self, replacement: withString)
    }
    
    public func search(regex: Regex) -> Int? {
        return regex.search(self)
    }
}

{'fraction_non_alphanumeric': 0.11039453717754173, 'fraction_numerical': 0.00625948406676783, 'mean_word_length': 2.7423704755145493, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, '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': '3821818', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1477, 'n_words': 376}