<commit_before>//
//  Route.swift
//  Routing
//
//  Created by <NAME> on 6/17/16.
//  Copyright © 2016 Routing. All rights reserved.
//

import Foundation

public typealias Parameters = [String: String]
public typealias RouteHandler = (String, Parameters, Completed) -> Void
public typealias Completed = () -> Void
public typealias ProxyHandler = (String, Parameters, Next) -> Void
public typealias Next = (String?, Parameters?) -> Void

internal struct Route {
    
    internal enum HandlerType {
        case Route(RouteHandler)
        case Proxy(ProxyHandler)
    }
    
    internal let pattern: String
    internal let handler: HandlerType
    internal let queue: dispatch_queue_t
    internal let isProxy: Bool
    private let dynamicSegments: [String]
    
    internal init(_ pattern: String, queue: dispatch_queue_t, handler: HandlerType) {
        var pattern = pattern
        var dynamicSegments = [String]()
        Route.prepare(&pattern, dynamicSegments: &dynamicSegments)
        
        self.pattern = pattern
        self.queue = queue
        self.handler = handler
        
        if case .Proxy(_) = handler {
            isProxy = true
        } else {
            isProxy = false
        }
        
        self.dynamicSegments = dynamicSegments
    }
    
    internal func matches(route: String) -> Bool {
        return _matches(route) != nil
    }
    
    internal func matches(route: String, inout parameters: Parameters) -> Bool {
        guard let matches = _matches(route) else {
            return false
        }
        
        if dynamicSegments.count > 0 && dynamicSegments.count == matches.numberOfRanges - 1 {
            [Int](1 ..< matches.numberOfRanges).forEach { (index) in
                parameters[dynamicSegments[index-1]] = (route as NSString).substringWithRange(matches.rangeAtIndex(index))
            }
        }
        
        return true
    }
    
    private func _matches(route: String) -> NSTextCheckingResult? {
        return (try? NSRegularExpression(pattern: pattern, options: .CaseInsensitive))
            .flatMap {
                $0.matchesInString(route, options: [], range: NSMakeRange(0, route.characters.count))
            }?.first
    }
    
    private static func prepare(inout pattern: String, inout dynamicSegments: [String]) {
        let options: NSStringCompareOptions = [.RegularExpressionSearch, .CaseInsensitiveSearch]
        while let range = pattern.rangeOfString(":[a-zA-Z0-9-_]+", options: options) {
            let segment = pattern
                .substringWithRange(range.startIndex.advancedBy(1)..<range.endIndex)
            dynamicSegments.append(segment)
            pattern.replaceRange(range, with: "([^/]+)")
        }
    }
    
}<commit_msg>Removing func and changing loop syntax<commit_after>//
//  Route.swift
//  Routing
//
//  Created by <NAME> on 6/17/16.
//  Copyright © 2016 Routing. All rights reserved.
//

import Foundation

public typealias Parameters = [String: String]
public typealias RouteHandler = (String, Parameters, Completed) -> Void
public typealias Completed = () -> Void
public typealias ProxyHandler = (String, Parameters, Next) -> Void
public typealias Next = (String?, Parameters?) -> Void

internal struct Route {
    
    internal enum HandlerType {
        case Route(RouteHandler)
        case Proxy(ProxyHandler)
    }
    
    internal let pattern: String
    internal let handler: HandlerType
    internal let queue: dispatch_queue_t
    internal let isProxy: Bool
    private let dynamicSegments: [String]
    
    internal init(_ pattern: String, queue: dispatch_queue_t, handler: HandlerType) {
        var pattern = pattern
        var dynamicSegments = [String]()
        let options: NSStringCompareOptions = [.RegularExpressionSearch, .CaseInsensitiveSearch]
        while let range = pattern.rangeOfString(":[a-zA-Z0-9-_]+", options: options) {
            let segment = pattern
                .substringWithRange(range.startIndex.advancedBy(1)..<range.endIndex)
            dynamicSegments.append(segment)
            pattern.replaceRange(range, with: "([^/]+)")
        }
        
        self.pattern = pattern
        self.queue = queue
        self.handler = handler
        if case .Proxy(_) = handler {
            isProxy = true
        } else {
            isProxy = false
        }
        self.dynamicSegments = dynamicSegments
    }
    
    internal func matches(route: String) -> Bool {
        return _matches(route) != nil
    }
    
    internal func matches(route: String, inout parameters: Parameters) -> Bool {
        guard let matches = _matches(route) else {
            return false
        }
        
        if dynamicSegments.count > 0 && dynamicSegments.count == matches.numberOfRanges - 1 {
            for i in (1 ..< matches.numberOfRanges) {
                parameters[dynamicSegments[i-1]] = (route as NSString)
                    .substringWithRange(matches.rangeAtIndex(i))
            }
        }
        
        return true
    }
    
    private func _matches(route: String) -> NSTextCheckingResult? {
        return (try? NSRegularExpression(pattern: pattern, options: .CaseInsensitive))
            .flatMap {
                $0.matchesInString(route, options: [], range: NSMakeRange(0, route.characters.count))
            }?.first
    }
    
}
{'fraction_non_alphanumeric': 0.09908222513579322, 'fraction_numerical': 0.006742835737029406, 'mean_word_length': 2.2660550458715596, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 21, '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': '2311389', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1411, 'n_words': 383}