<commit_before>//===--- RegexOptions.swift ------------------------------------------------------===//
//Copyright (c) 2016 <NAME> (dileping)
//
//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
//
//http://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

public struct RegexOptions : OptionSet {
    public let rawValue: UInt
    
    public init(rawValue: UInt) { self.rawValue = rawValue }
    
    public static let caseInsensitive = RegexOptions(rawValue: 1) /* Match letters in the pattern independent of case. */
    public static let allowCommentsAndWhitespace = RegexOptions(rawValue: 2) /* Ignore whitespace and #-prefixed comments in the pattern. */
    public static let ignoreMetacharacters = RegexOptions(rawValue: 4) /* Treat the entire pattern as a literal string. */
    public static let dotMatchesLineSeparators = RegexOptions(rawValue: 8) /* Allow . to match any character, including line separators. */
    public static let anchorsMatchLines = RegexOptions(rawValue: 16) /* Allow ^ and $ to match the start and end of lines. */
    public static let useUnixLineSeparators = RegexOptions(rawValue: 32) /* Treat only \n as a line separator (otherwise, all standard line separators are used). */
    public static let useUnicodeWordBoundaries = RegexOptions(rawValue: 64) /* Use Unicode TR#29 to specify word boundaries (otherwise, traditional regular expression word boundaries are used). */
    public static let defaultOptions:RegexOptions = [caseInsensitive]
    public static let none:RegexOptions = []
}

#if !os(Linux)
    public typealias RegularExpression = NSRegularExpression
#else
    public extension RegularExpression {
        public typealias MatchingOptions = NSMatchingOptions
    }
#endif

extension RegularExpression.Options : Hashable {
    public var hashValue: Int {
        get {
            return Int(rawValue)
        }
    }
}

extension RegexOptions : Hashable {
    public var hashValue: Int {
        get {
            return Int(rawValue)
        }
    }
}

private let nsToRegexOptionsMap:Dictionary<RegularExpression.Options, RegexOptions> = [
    .caseInsensitive:.caseInsensitive,
    .allowCommentsAndWhitespace:.allowCommentsAndWhitespace,
    .ignoreMetacharacters:.ignoreMetacharacters,
    .dotMatchesLineSeparators:.dotMatchesLineSeparators,
    .anchorsMatchLines:.anchorsMatchLines,
    .useUnixLineSeparators:.useUnixLineSeparators,
    .useUnicodeWordBoundaries:.useUnicodeWordBoundaries]


private let regexToNSOptionsMap:Dictionary<RegexOptions, RegularExpression.Options> = nsToRegexOptionsMap.reduce([:]) { (dict, kv) in
    var dict = dict
    dict[kv.value] = kv.key
    return dict
}

public extension RegexOptions {
    public var ns:RegularExpression.Options {
        get {
            let nsSeq = regexToNSOptionsMap.filter { (regex, _) in
                self.contains(regex)
            }.map { (_, ns) in
                ns
            }
            
            return RegularExpression.Options(nsSeq)<commit_msg>Better namings<commit_after>//===--- RegexOptions.swift ------------------------------------------------------===//
//Copyright (c) 2016 <NAME> (dileping)
//
//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
//
//http://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

public struct RegexOptions : OptionSet {
    public let rawValue: UInt
    
    public init(rawValue: UInt) { self.rawValue = rawValue }
    
    public static let caseInsensitive = RegexOptions(rawValue: 1) /* Match letters in the pattern independent of case. */
    public static let allowCommentsAndWhitespace = RegexOptions(rawValue: 2) /* Ignore whitespace and #-prefixed comments in the pattern. */
    public static let ignoreMetacharacters = RegexOptions(rawValue: 4) /* Treat the entire pattern as a literal string. */
    public static let dotMatchesLineSeparators = RegexOptions(rawValue: 8) /* Allow . to match any character, including line separators. */
    public static let anchorsMatchLines = RegexOptions(rawValue: 16) /* Allow ^ and $ to match the start and end of lines. */
    public static let useUnixLineSeparators = RegexOptions(rawValue: 32) /* Treat only \n as a line separator (otherwise, all standard line separators are used). */
    public static let useUnicodeWordBoundaries = RegexOptions(rawValue: 64) /* Use Unicode TR#29 to specify word boundaries (otherwise, traditional regular expression word boundaries are used). */
    public static let defaultOptions:RegexOptions = [caseInsensitive]
    public static let none:RegexOptions = []
}

#if !os(Linux)
    public typealias RegularExpression = NSRegularExpression
#else
    public extension RegularExpression {
        public typealias MatchingOptions = NSMatchingOptions
    }
#endif

extension RegularExpression.Options : Hashable {
    public var hashValue: Int {
        get {
            return Int(rawValue)
        }
    }
}

extension RegexOptions : Hashable {
    public var hashValue: Int {
        get {
            return Int(rawValue)
        }
    }
}

private let nsToRegexOptionsMap:[RegularExpression.Options: RegexOptions] = [
    .caseInsensitive:.caseInsensitive,
    .allowCommentsAndWhitespace:.allowCommentsAndWhitespace,
    .ignoreMetacharacters:.ignoreMetacharacters,
    .dotMatchesLineSeparators:.dotMatchesLineSeparators,
    .anchorsMatchLines:.anchorsMatchLines,
    .useUnixLineSeparators:.useUnixLineSeparators,
    .useUnicodeWordBoundaries:.useUnicodeWordBoundaries]


private let regexToNSOptionsMap:[RegexOptions: RegularExpression.Options] = nsToRegexOptionsMap.reduce([:]) { (dict, kv) in
    var dict = dict
    dict[kv.value] = kv.key
    return dict
}

public extension RegexOptions {
    public var ns:RegularExpression.Options {
        get {
            let nsSeq = regexToNSOptionsMap.filter { (regex, _) in
                self.contains(regex)
            }.map { (_, ns) in
                ns
            }
            
            return RegularExpression.Options(nsSeq)
{'fraction_non_alphanumeric': 0.11833309628786802, 'fraction_numerical': 0.005689091167685962, 'mean_word_length': 2.915367483296214, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '3845221', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1797, 'n_words': 604}