<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Utilities.Reflection;
using System.ComponentModel;
using System.Reflection;
using System.Collections.Concurrent;

namespace Signum.Utilities
{
    public static class EnumExtensions
    {
        public static bool HasFlag(this Enum value, Enum flag)
        {
            int val = Convert.ToInt32(flag);
            return (Convert.ToInt32(value) & val) == val;
        }

        public static T ToEnum<T>(this string str) where T : struct
        {
            return (T)Enum.Parse(typeof(T), str);
        }

        public static T ToEnum<T>(this string str, bool ignoreCase) where T : struct
        {
            return (T)Enum.Parse(typeof(T), str, ignoreCase);
        }

        public static T[] GetValues<T>()
        {
            return (T[])Enum.GetValues(typeof(T));
        }

        public static bool IsDefined<T>(T value) where T : struct
        {
            return Enum.IsDefined(typeof(T), value);
        }

        public static int MinFlag(int value)
        {
            int result = 1;
            while ((result & value) == 0 && result != 0)
                result <<= 1;
            return result;
        }

        public static int MaxFlag(int value)
        {
            int result = (int.MaxValue >> 1) + 1; // because C2
            while ((result & value) == 0 && result != 0) 
                result >>= 1;
            return result;
        }

        public static long MinFlag(long value)
        {
            int result = 1;
            while ((result & value) == 0 && result != 0)
                result <<= 1;
            return result;
        }

        public static long MaxFlag(long value)
        {
            int result = (int.MaxValue >> 1) + 1; // because C2
            while ((result & value) == 0 && result != 0)
                result >>= 1;
            return result;
        }

        public static bool TryParse(string value, Type enumType, bool ignoreCase, out Enum result)
        {
            if (!Enum.IsDefined(enumType, value))
            {
                result = null;
                return false;
            }
            result = (Enum)Enum.Parse(enumType, value);
            return true;
        }

        public static bool TryParse<T>(string value, bool ignoreCase, out T result)
            where T:struct
        {
            if (!Enum.IsDefined(typeof(T), value))
            {
                result = default(T);
                return false;
            }
            result = (T)(object)Enum.Parse(typeof(T), value);
            return true;
        }

        public static IEnumerable<Enum> UntypedGetValues(Type type)
        {
            if (!type.IsEnum)
                return Enumerable.Empty<Enum>();

            return Enum.GetValues(type).Cast<Enum>();
        }<commit_msg>fix EnumExtensions.TryParse to allow numbers<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Utilities.Reflection;
using System.ComponentModel;
using System.Reflection;
using System.Collections.Concurrent;

namespace Signum.Utilities
{
    public static class EnumExtensions
    {
        public static bool HasFlag(this Enum value, Enum flag)
        {
            int val = Convert.ToInt32(flag);
            return (Convert.ToInt32(value) & val) == val;
        }

        public static T ToEnum<T>(this string str) where T : struct
        {
            return (T)Enum.Parse(typeof(T), str);
        }

        public static T ToEnum<T>(this string str, bool ignoreCase) where T : struct
        {
            return (T)Enum.Parse(typeof(T), str, ignoreCase);
        }

        public static T[] GetValues<T>()
        {
            return (T[])Enum.GetValues(typeof(T));
        }

        public static bool IsDefined<T>(T value) where T : struct
        {
            return Enum.IsDefined(typeof(T), value);
        }

        public static int MinFlag(int value)
        {
            int result = 1;
            while ((result & value) == 0 && result != 0)
                result <<= 1;
            return result;
        }

        public static int MaxFlag(int value)
        {
            int result = (int.MaxValue >> 1) + 1; // because C2
            while ((result & value) == 0 && result != 0) 
                result >>= 1;
            return result;
        }

        public static long MinFlag(long value)
        {
            int result = 1;
            while ((result & value) == 0 && result != 0)
                result <<= 1;
            return result;
        }

        public static long MaxFlag(long value)
        {
            int result = (int.MaxValue >> 1) + 1; // because C2
            while ((result & value) == 0 && result != 0)
                result >>= 1;
            return result;
        }

        public static bool TryParse(string value, Type enumType, bool ignoreCase, out Enum result)
        {
            int rubish;
            if (!Enum.IsDefined(enumType, value) && !int.TryParse(value, out rubish))
            {
                result = null;
                return false;
            }
            result = (Enum)Enum.Parse(enumType, value);
            return true;
        }

        public static bool TryParse<T>(string value, bool ignoreCase, out T result)
            where T:struct
        {
            int rubish;
            if (!Enum.IsDefined(typeof(T), value) && !int.TryParse(value, out rubish))
            {
                result = default(T);
                return false;
            }
            result = (T)(object)Enum.Parse(typeof(T), value);
            return true;
        }

        public static IEnumerable<Enum> UntypedGetValues(Type type)
        {
            if (!type.IsEnum)
                return Enumerable.Empty<Enum>();

            return Enum.GetValues(type).Cast<Enum>();
        }
{'fraction_non_alphanumeric': 0.10546677880571909, 'fraction_numerical': 0.008074011774600504, 'mean_word_length': 1.5453767123287672, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 35, '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': '3848304', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1557, 'n_words': 454}