<commit_before>﻿namespace FsControl.Core.TypeMethods

open System
open System.Collections.Generic
open System.Text
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Core.Printf
open FsControl.Core.Prelude

module Converter =

    type FromBytes = FromBytes with
        static member instance (FromBytes, _:bool   ) = fun (x, i) -> BitConverter.ToBoolean(x, i)
        static member instance (FromBytes, _:char   ) = fun (x, i) -> BitConverter.ToChar   (x, i)
        static member instance (FromBytes, _:float  ) = fun (x, i) -> BitConverter.ToDouble (x, i)
        static member instance (FromBytes, _: int16 ) = fun (x, i) -> BitConverter.ToInt16  (x, i)
        static member instance (FromBytes, _: int   ) = fun (x, i) -> BitConverter.ToInt32  (x, i)
        static member instance (FromBytes, _:int64  ) = fun (x, i) -> BitConverter.ToInt64  (x, i)
        static member instance (FromBytes, _:float32) = fun (x, i) -> BitConverter.ToSingle (x, i)
        static member instance (FromBytes, _:string ) = fun (x, i) -> BitConverter.ToString (x, i)
        static member instance (FromBytes, _:uint16 ) = fun (x, i) -> BitConverter.ToUInt16 (x, i)
        static member instance (FromBytes, _:uint32 ) = fun (x, i) -> BitConverter.ToUInt32 (x, i)
        static member instance (FromBytes, _:uint64 ) = fun (x, i) -> BitConverter.ToUInt64 (x, i)
        
    let inline internal fromBytesWithOffset (startIndex:int) (value:byte[]) = Inline.instance FromBytes (value, startIndex)
    let inline internal fromBytes                            (value:byte[]) = Inline.instance FromBytes (value, 0         )


    type ToBytes = ToBytes with
        static member instance (ToBytes, x:bool   , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:char   , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:float  , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x: int16 , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x: int   , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:int64  , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:float32, _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:string , _) = fun () -> Array.map byte (x.ToCharArray())
        static member instance (ToBytes, x:uint16 , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:uint32 , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:uint64 , _) = fun () -> BitConverter.GetBytes(x)

    let inline internal toBytes value :byte[] = Inline.instance (ToBytes, value) ()

    let internal inv = System.Globalization.CultureInfo.InvariantCulture

    type Parse = Parse with
        static member instance (Parse, _:bool          ) = fun x -> Boolean       .Parse(x)
        static member instance (Parse, _:char          ) = fun x -> Char          .Parse(x)
        static member instance (Parse, _:byte          ) = fun x -> Byte          .Parse(x, inv)
        static member instance (Parse, _:sbyte         ) = fun x -> SByte         .Parse(x, inv)
        static member instance (Parse, _:float         ) = fun x -> Double        .Parse(x, inv)
        static member instance (Parse, _: int16        ) = fun x -> Int16         .Parse(x, inv)
        static member instance (Parse, _: int          ) = fun x -> Int32         .Parse(x, inv)
        static member instance (Parse, _:int64         ) = fun x -> Int64         .Parse(x, inv)
        static member instance (Parse, _:float32       ) = fun x -> Single        .Parse(x, inv)
        static member instance (Parse, _:uint16        ) = fun x -> UInt16        .Parse(x, inv)
        static member instance (Parse, _:uint32        ) = fun x -> UInt32        .Parse(x, inv)
        static member instance (Parse, _:uint64        ) = fun x -> UInt64        .Parse(x, inv)
        static member instance (Parse, _:decimal       ) = fun x -> Decimal       .Parse(x, inv)
        static member instance (Parse, _:DateTime      ) = fun x -> DateTime      .Parse(x, inv)
        static member instance (Parse, _:DateTimeOffset) = fun x -> DateTimeOffset.Parse(x, inv)
        static member instance (Parse, _:string ) = id :string->_

    let inline internal parse (value:string) = Inline.instance Parse value

    type ToString = ToString with
        static member instance (ToString, x:bool,          _) = fun () -> x.ToString inv
        static member instance (ToString, x:char,          _) = fun () -> x.ToString inv
        static member instance (ToString, x:byte,          _) = fun () -> x.ToString inv
        static member instance (ToString, x:sbyte,         _) = fun () -> x.ToString inv
        static member instance (ToString, x:float,         _) = fun () -> x.ToString inv
        static member instance (ToString, x:int16,         _) = fun () -> x.ToString inv
        static member instance (ToString, x:int,           _) = fun () -> x.ToString inv
        static member instance (ToString, x:int64,         _) = fun () -> x.ToString inv
        static member instance (ToString, x:float32,       _) = fun () -> x.ToString inv
        static member instance (ToString, x:string,        _) = fun () -> if x = null then "null" else x
        static member instance (ToString, x:uint16,        _) = fun () -> x.ToString inv
        static member instance (ToString, x:uint32,        _) = fun () -> x.ToString inv<commit_msg>+ Enum as instance of Parse.<commit_after>﻿namespace FsControl.Core.TypeMethods

open System
open System.Collections.Generic
open System.Text
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Core.Printf
open FsControl.Core.Prelude

module Converter =

    type FromBytes = FromBytes with
        static member instance (FromBytes, _:bool   ) = fun (x, i) -> BitConverter.ToBoolean(x, i)
        static member instance (FromBytes, _:char   ) = fun (x, i) -> BitConverter.ToChar   (x, i)
        static member instance (FromBytes, _:float  ) = fun (x, i) -> BitConverter.ToDouble (x, i)
        static member instance (FromBytes, _: int16 ) = fun (x, i) -> BitConverter.ToInt16  (x, i)
        static member instance (FromBytes, _: int   ) = fun (x, i) -> BitConverter.ToInt32  (x, i)
        static member instance (FromBytes, _:int64  ) = fun (x, i) -> BitConverter.ToInt64  (x, i)
        static member instance (FromBytes, _:float32) = fun (x, i) -> BitConverter.ToSingle (x, i)
        static member instance (FromBytes, _:string ) = fun (x, i) -> BitConverter.ToString (x, i)
        static member instance (FromBytes, _:uint16 ) = fun (x, i) -> BitConverter.ToUInt16 (x, i)
        static member instance (FromBytes, _:uint32 ) = fun (x, i) -> BitConverter.ToUInt32 (x, i)
        static member instance (FromBytes, _:uint64 ) = fun (x, i) -> BitConverter.ToUInt64 (x, i)
        
    let inline internal fromBytesWithOffset (startIndex:int) (value:byte[]) = Inline.instance FromBytes (value, startIndex)
    let inline internal fromBytes                            (value:byte[]) = Inline.instance FromBytes (value, 0         )


    type ToBytes = ToBytes with
        static member instance (ToBytes, x:bool   , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:char   , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:float  , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x: int16 , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x: int   , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:int64  , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:float32, _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:string , _) = fun () -> Array.map byte (x.ToCharArray())
        static member instance (ToBytes, x:uint16 , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:uint32 , _) = fun () -> BitConverter.GetBytes(x)
        static member instance (ToBytes, x:uint64 , _) = fun () -> BitConverter.GetBytes(x)

    let inline internal toBytes value :byte[] = Inline.instance (ToBytes, value) ()

    let internal inv = System.Globalization.CultureInfo.InvariantCulture

    type Parse = Parse with
        static member instance (Parse, _:bool          ) = fun x -> Boolean       .Parse(x)
        static member instance (Parse, _:char          ) = fun x -> Char          .Parse(x)
        static member instance (Parse, _:byte          ) = fun x -> Byte          .Parse(x, inv)
        static member instance (Parse, _:sbyte         ) = fun x -> SByte         .Parse(x, inv)
        static member instance (Parse, _:float         ) = fun x -> Double        .Parse(x, inv)
        static member instance (Parse, _: int16        ) = fun x -> Int16         .Parse(x, inv)
        static member instance (Parse, _: int          ) = fun x -> Int32         .Parse(x, inv)
        static member instance (Parse, _:int64         ) = fun x -> Int64         .Parse(x, inv)
        static member instance (Parse, _:float32       ) = fun x -> Single        .Parse(x, inv)
        static member instance (Parse, _:uint16        ) = fun x -> UInt16        .Parse(x, inv)
        static member instance (Parse, _:uint32        ) = fun x -> UInt32        .Parse(x, inv)
        static member instance (Parse, _:uint64        ) = fun x -> UInt64        .Parse(x, inv)
        static member instance (Parse, _:decimal       ) = fun x -> Decimal       .Parse(x, inv)
        static member instance (Parse, _:DateTime      ) = fun x -> DateTime      .Parse(x, inv)
        static member instance (Parse, _:DateTimeOffset) = fun x -> DateTimeOffset.Parse(x, inv)
        static member instance (Parse, _:string ) = id :string->_

    let inline internal parse (value:string) = Inline.instance Parse value

    type Parse with
        static member instance (Parse, _:'enum         ) = fun x ->
            (match Enum.TryParse(x) with
             | (true, v) -> v
             | _         -> invalidArg "value" ("Requested value '" + x + "' was not found.")
            ):'enum

    type ToString = ToString with
        static member instance (ToString, x:bool,          _) = fun () -> x.ToString inv
        static member instance (ToString, x:char,          _) = fun () -> x.ToString inv
        static member instance (ToString, x:byte,          _) = fun () -> x.ToString inv
        static member instance (ToString, x:sbyte,         _) = fun () -> x.ToString inv
        static member instance (ToString, x:float,         _) = fun () -> x.ToString inv
        static member instance (ToString, x:int16,         _) = fun () -> x.ToString inv
        static member instance (ToString, x:int,           _) = fun () -> x.ToString inv
        static member instance (ToString, x:int64,         _) = fun () -> x.ToString inv
        static member instance (ToString, x:float32,       _) = fun () -> x.ToString inv
        static member instance (ToString, x:string,        _) = fun () -> if x = null then "null" else x
        static member instance (ToString, x:uint16,        _) = fun () -> x.ToString inv
        static member instance (ToString, x:uint32,        _) = fun () -> x.ToString inv
{'fraction_non_alphanumeric': 0.1202658969649261, 'fraction_numerical': 0.012420187177468732, 'mean_word_length': 2.3238372093023254, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 106, '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': '4793623', 'n_tokens_mistral': 3386, 'n_tokens_neox': 3270, 'n_words': 1085}