<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Enum.cs" company="Catel development team">
//   Copyright (c) 2008 - 2014 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Catel
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.Linq;
    using System.Reflection;

    using Catel.Reflection;

    /// <summary>
    /// Generic enumeration wrapper.
    /// </summary>
    /// <typeparam name="TEnum">Type of the enumeration to wrap.</typeparam>
    public static class Enum<TEnum>
        where TEnum : struct, IComparable, IFormattable
    {
        #region Static Methods
        /// <summary>
        /// Converts an enumaration to a list.
        /// </summary>
        /// <returns><see cref="List{TEnum}"/> containing all the values.</returns>
        public static List<TEnum> ToList()
        {
            return GetValues().ToList();
        }

        /// <summary>
        /// Converts a specific enum value from one specific enum type to another enum type by it's name.
        /// <para/>
        /// For example, to convert <c>Catel.Services.CameraType</c> to <c>Microsoft.Devices.CameraType</c>, use the
        /// following code:
        /// <para/>
        /// ConvertEnum&lt;Microsoft.Devices.CameraType&gt;(Catel.Services.CameraType.Primary);
        /// </summary>
        /// <param name="inputEnumValue">The input enum value.</param>
        /// <returns>The converted enum value.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="inputEnumValue"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="inputEnumValue"/> is not of type <see cref="Enum"/>.</exception>
        /// <exception cref="ArgumentException">The value of <paramref name="inputEnumValue"/> cannot be converted to a value of <typeparamref name="TEnum"/>.</exception>
        public static TEnum ConvertFromOtherEnumValue(object inputEnumValue)
        {
            Argument.IsNotNull("inputEnumValue", inputEnumValue);
            Argument.IsOfType("inputEnumValue", inputEnumValue, typeof(Enum));

            string value = inputEnumValue.ToString();

            return (TEnum)Enum.Parse(typeof(TEnum), value, true);
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The name of the value.</returns>
        public static string GetName(int value)
        {
            return Enum.GetName(typeof(TEnum), value);
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The name of the value.</returns>
        public static string GetName(long value)
        {
            return Enum.GetName(typeof(TEnum), value);
        }

        /// <summary>
        /// Gets the names.
        /// </summary>
        /// <returns>Array of names of an enum.</returns>
        public static string[] GetNames()
        {
            var enumType = typeof(TEnum);

            var fields = from field in GetFields(enumType)
                         select field.Name;

            return fields.ToArray();
        }

        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <returns><see cref="List{TEnum}"/> of values.</returns>
        public static List<TEnum> GetValues()
        {
            var enumType = typeof(TEnum);

            var fields = GetFields(enumType);

            return (from field in fields
                    select field.GetValue(enumType)).Cast<TEnum>().ToList();
        }

        /// <summary>
        /// Gets the fields from an enum.
        /// </summary>
        /// <param name="enumType">Type of the enum.</param>
        /// <returns>Array of <see cref="FieldInfo"/> values.</returns>
        private static FieldInfo[] GetFields(Type enumType)
        {
            var fields = enumType.GetFieldsEx(BindingFlagsHelper.GetFinalBindingFlags(true, true));

            fields = (from field in fields
                      where field.IsLiteral && field.IsPublic
                      select field).ToArray();

            return fields.ToArray();
        }

        /// <summary>
        /// Parses the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="ignoreCase">if set to <c>true</c>, the case should be ignored.</param>
        /// <returns>The enum value.</returns>
        public static TEnum Parse(string input, bool ignoreCase = false)
        {
            return (TEnum)Enum.Parse(typeof(TEnum), input, ignoreCase);
        }

        /// <summary>
        /// Tries to parse an enum value name.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if successful; otherwise <c>false</c>.</returns>
        public static bool TryParse(string input, out TEnum? result)
        {
            result = null;
            if (!Enum.IsDefined(typeof(TEnum), input))
            {
                return false;
            }

            try
            {
                result = (TEnum)Enum.Parse(typeof(TEnum), input, true);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Tries to parse an enum value name.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if successful; otherwise <c>false</c>.</returns>
        public static bool TryParse(string input, out TEnum result)
        {
            TEnum? temp;
            if (!TryParse(input, out temp))
            {
                // input not found in the Enum, fill the out parameter with the first item from the enum
                var values = GetValues().ToArray();

                result = (TEnum)values.GetValue(values.GetLowerBound(0));<commit_msg>Enum parsing can now be case-sensitive<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Enum.cs" company="Catel development team">
//   Copyright (c) 2008 - 2014 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Catel
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.Linq;
    using System.Reflection;

    using Catel.Reflection;

    /// <summary>
    /// Generic enumeration wrapper.
    /// </summary>
    /// <typeparam name="TEnum">Type of the enumeration to wrap.</typeparam>
    public static class Enum<TEnum>
        where TEnum : struct, IComparable, IFormattable
    {
        #region Static Methods
        /// <summary>
        /// Converts an enumaration to a list.
        /// </summary>
        /// <returns><see cref="List{TEnum}"/> containing all the values.</returns>
        public static List<TEnum> ToList()
        {
            return GetValues().ToList();
        }

        /// <summary>
        /// Converts a specific enum value from one specific enum type to another enum type by it's name.
        /// <para/>
        /// For example, to convert <c>Catel.Services.CameraType</c> to <c>Microsoft.Devices.CameraType</c>, use the
        /// following code:
        /// <para/>
        /// ConvertEnum&lt;Microsoft.Devices.CameraType&gt;(Catel.Services.CameraType.Primary);
        /// </summary>
        /// <param name="inputEnumValue">The input enum value.</param>
        /// <returns>The converted enum value.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="inputEnumValue"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="inputEnumValue"/> is not of type <see cref="Enum"/>.</exception>
        /// <exception cref="ArgumentException">The value of <paramref name="inputEnumValue"/> cannot be converted to a value of <typeparamref name="TEnum"/>.</exception>
        public static TEnum ConvertFromOtherEnumValue(object inputEnumValue)
        {
            Argument.IsNotNull("inputEnumValue", inputEnumValue);
            Argument.IsOfType("inputEnumValue", inputEnumValue, typeof(Enum));

            string value = inputEnumValue.ToString();

            return (TEnum)Enum.Parse(typeof(TEnum), value, true);
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The name of the value.</returns>
        public static string GetName(int value)
        {
            return Enum.GetName(typeof(TEnum), value);
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The name of the value.</returns>
        public static string GetName(long value)
        {
            return Enum.GetName(typeof(TEnum), value);
        }

        /// <summary>
        /// Gets the names.
        /// </summary>
        /// <returns>Array of names of an enum.</returns>
        public static string[] GetNames()
        {
            var enumType = typeof(TEnum);

            var fields = from field in GetFields(enumType)
                         select field.Name;

            return fields.ToArray();
        }

        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <returns><see cref="List{TEnum}"/> of values.</returns>
        public static List<TEnum> GetValues()
        {
            var enumType = typeof(TEnum);

            var fields = GetFields(enumType);

            return (from field in fields
                    select field.GetValue(enumType)).Cast<TEnum>().ToList();
        }

        /// <summary>
        /// Gets the fields from an enum.
        /// </summary>
        /// <param name="enumType">Type of the enum.</param>
        /// <returns>Array of <see cref="FieldInfo"/> values.</returns>
        private static FieldInfo[] GetFields(Type enumType)
        {
            var fields = enumType.GetFieldsEx(BindingFlagsHelper.GetFinalBindingFlags(true, true));

            fields = (from field in fields
                      where field.IsLiteral && field.IsPublic
                      select field).ToArray();

            return fields.ToArray();
        }

        /// <summary>
        /// Parses the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="ignoreCase">if set to <c>true</c>, the case should be ignored.</param>
        /// <returns>The enum value.</returns>
        public static TEnum Parse(string input, bool ignoreCase = false)
        {
            return (TEnum)Enum.Parse(typeof(TEnum), input, ignoreCase);
        }

        /// <summary>
        /// Tries to parse an enum value name.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if successful; otherwise <c>false</c>.</returns>
        public static bool TryParse(string input, out TEnum? result)
        {
            return TryParse(input, true, out result);
        }

        /// <summary>
        /// Tries to parse an enum value name.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if successful; otherwise <c>false</c>.</returns>
        public static bool TryParse(string input, out TEnum result)
        {
            return TryParse(input, true, out result);
        }

        /// <summary>
        /// Tries to parse an enum value name.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="ignoreCase">if set to <c>true</c>, the case will be ignored.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if successful; otherwise <c>false</c>.</returns>
        public static bool TryParse(string input, bool ignoreCase, out TEnum? result)
        {
            result = null;
            if (!Enum.IsDefined(typeof(TEnum), input))
            {
                return false;
            }

            try
            {
                result = (TEnum)Enum.Parse(typeof(TEnum), input, ignoreCase);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Tries to parse an enum value name.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="ignoreCase">if set to <c>true</c>, the case will be ignored.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if successful; otherwise <c>false</c>.</returns>
        public static bool TryParse(string input, bool ignoreCase, out TEnum result)
        {
            TEnum? temp;
            if (!TryParse(input, ignoreCase, out temp))
            {
                // input not found in the Enum, fill the out parameter with the first item from the enum
                var values = GetValues().ToArray();

                result = (TEnum)values.GetValue(values.GetLowerBound(0));
{'fraction_non_alphanumeric': 0.16817789291882557, 'fraction_numerical': 0.0012953367875647669, 'mean_word_length': 1.6290200529701098, 'pattern_counts': {'":': 0, '<': 239, '<?xml version=': 0, '>': 239, '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': '3299878', 'n_tokens_mistral': 3553, 'n_tokens_neox': 3458, 'n_words': 973}