<commit_before>﻿namespace Argu

open System
open System.IO
open System.Collections.Generic
open System.Text
open System.Reflection

open System.Xml
open System.Xml.Linq

open Microsoft.FSharp.Reflection
open Microsoft.FSharp.Quotations.Patterns

[<AutoOpen>]
module internal Utils =

    let allBindings = BindingFlags.NonPublic ||| BindingFlags.Public ||| BindingFlags.Static ||| BindingFlags.Instance

    /// gets the top-Level methodInfo call in a quotation
    let rec getMethod =
        function
        | Lambda(_,e) -> getMethod e
        | Call(_,f,_) -> f
        | _ -> invalidArg "expr" "quotation is not of method."


    [<RequireQualifiedAccess>]
    module internal Enum =

        let inline hasFlag (flag : ^Enum) (value : ^Enum) = flag &&& value = value

    /// reflected version of Unchecked.defaultof
    type Unchecked =
        static member DefaultOf<'T> () = Unchecked.defaultof<'T>
        static member UntypedDefaultOf(t : Type) =
            typeof<Unchecked>
                .GetMethod("DefaultOf", BindingFlags.NonPublic ||| BindingFlags.Static)
                .MakeGenericMethod([| t |])
                .Invoke(null, [||])

    type UnionCaseInfo with
        member uci.GetAttrs<'T when 'T :> Attribute> (?includeDeclaringTypeAttrs) =
            let includeDeclaringTypeAttrs = defaultArg includeDeclaringTypeAttrs false

            let attrs = uci.GetCustomAttributes(typeof<'T>) |> Seq.map (fun o -> o :?> 'T)<commit_msg>minor refactoring<commit_after>﻿namespace Argu

open System
open System.IO
open System.Collections.Generic
open System.Text
open System.Reflection

open System.Xml
open System.Xml.Linq

open Microsoft.FSharp.Reflection
open Microsoft.FSharp.Quotations.Patterns

[<AutoOpen>]
module internal Utils =

    let allBindings = BindingFlags.NonPublic ||| BindingFlags.Public ||| BindingFlags.Static ||| BindingFlags.Instance

    /// gets the top-Level methodInfo call in a quotation
    let rec getMethod =
        function
        | Lambda(_,e) -> getMethod e
        | Call(_,f,_) -> f
        | _ -> invalidArg "expr" "quotation is not of method."


    [<RequireQualifiedAccess>]
    module internal Enum =

        let inline hasFlag (flag : ^Enum) (value : ^Enum) = flag &&& value = value

    [<AbstractClass>]
    type Existential internal () =
        static let genTy = typedefof<Existential<_>>
        abstract Type : Type
        abstract Accept : IFunc<'R> -> 'R
        static member FromType(t : Type) =
            let et = genTy.MakeGenericType [|t|]
            Activator.CreateInstance et :?> Existential

    and Existential<'T> () =
        inherit Existential()
        override __.Type = typeof<'T>
        override __.Accept func = func.Invoke<'T>()

    and IFunc<'R> =
        abstract Invoke<'T> : unit -> 'R

    /// reflected version of Unchecked.defaultof
    type Unchecked =
        static member UntypedDefaultOf(t : Type) =
            Existential.FromType(t).Accept { 
                new IFunc<obj> with 
                    member __.Invoke<'T> () = Unchecked.defaultof<'T> :> obj
                }

    type UnionCaseInfo with
        member uci.GetAttrs<'T when 'T :> Attribute> (?includeDeclaringTypeAttrs) =
            let includeDeclaringTypeAttrs = defaultArg includeDeclaringTypeAttrs false

            let attrs = uci.GetCustomAttributes(typeof<'T>) |> Seq.map (fun o -> o :?> 'T)
{'fraction_non_alphanumeric': 0.10723781388478582, 'fraction_numerical': 0.0, 'mean_word_length': 2.6684723726977246, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 44, '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': '5128956', 'n_tokens_mistral': 1005, 'n_tokens_neox': 971, 'n_words': 262}