internal string FormatFlagsInternal(TInt value, EnumMemberInternal <TInt, TIntProvider> member, string delimiter, ValueCollection <EnumFormat> formats)
        {
            if (!formats.Any())
            {
                formats = Enums.DefaultFormats;
            }

            if (member == null)
            {
                member = GetMember(value);
            }

            if (member != null || value.Equals(Provider.Zero) || !IsValidFlagCombination(value))
            {
                return(FormatInternal(value, member, formats));
            }

            if (string.IsNullOrEmpty(delimiter))
            {
                delimiter = FlagEnums.DefaultDelimiter;
            }

            return(string.Join(delimiter,
                               GetFlags(value).Select(flag => FormatInternal(flag, null, formats))
#if NET20 || NET35
                               .ToArray()
#endif
                               ));
        }
        public TInt Parse(string value, bool ignoreCase, ValueCollection <EnumFormat> formats)
        {
            if (IsFlagEnum)
            {
                return(ParseFlags(value, ignoreCase, null, formats));
            }

            Preconditions.NotNull(value, nameof(value));

            value = value.Trim();

            if (!formats.Any())
            {
                formats = Enums.DefaultFormats;
            }

            if (TryParseInternal(value, ignoreCase, out var result, out _, formats, true))
            {
                return(result);
            }
            if (IsNumeric(value))
            {
                throw new OverflowException("value is outside the underlying type's value range");
            }
            throw new ArgumentException($"string was not recognized as being a member of {_enumTypeName}", nameof(value));
        }
        public EnumMemberInternal <TInt, TIntProvider> GetMember(string value, bool ignoreCase, ValueCollection <EnumFormat> formats)
        {
            Preconditions.NotNull(value, nameof(value));

            value = value.Trim();

            if (!formats.Any())
            {
                formats = Enums.NameFormat;
            }

            TryParseInternal(value, ignoreCase, out _, out var member, formats, false);
            return(member);
        }
        public TInt ParseFlags(string value, bool ignoreCase, string delimiter, ValueCollection <EnumFormat> formats)
        {
            Preconditions.NotNull(value, nameof(value));

            if (string.IsNullOrEmpty(delimiter))
            {
                delimiter = FlagEnums.DefaultDelimiter;
            }

            var effectiveDelimiter = delimiter.Trim();

            if (effectiveDelimiter.Length == 0)
            {
                effectiveDelimiter = delimiter;
            }

            if (!formats.Any())
            {
                formats = Enums.DefaultFormats;
            }

            var result      = Provider.Zero;
            var startIndex  = 0;
            var valueLength = value.Length;

            while (startIndex < valueLength)
            {
                while (startIndex < valueLength && char.IsWhiteSpace(value[startIndex]))
                {
                    ++startIndex;
                }
                var delimiterIndex = value.IndexOf(effectiveDelimiter, startIndex, StringComparison.Ordinal);
                if (delimiterIndex < 0)
                {
                    delimiterIndex = valueLength;
                }
                var newStartIndex = delimiterIndex + effectiveDelimiter.Length;
                while (delimiterIndex > startIndex && char.IsWhiteSpace(value[delimiterIndex - 1]))
                {
                    --delimiterIndex;
                }
                var indValue = value.Substring(startIndex, delimiterIndex - startIndex);
                if (TryParseInternal(indValue, ignoreCase, out var valueAsTInt, out _, formats, true))
                {
                    result = Provider.Or(result, valueAsTInt);
                }
        public bool TryParse(string value, bool ignoreCase, out TInt result, ValueCollection <EnumFormat> formats)
        {
            if (IsFlagEnum)
            {
                return(TryParseFlags(value, ignoreCase, null, out result, formats));
            }

            if (value != null)
            {
                value = value.Trim();

                if (!formats.Any())
                {
                    formats = Enums.DefaultFormats;
                }

                return(TryParseInternal(value, ignoreCase, out result, out _, formats, true));
            }
            result = Provider.Zero;
            return(false);
        }
 internal string AsStringInternal(TInt value, EnumMemberInternal <TInt, TIntProvider> member, ValueCollection <EnumFormat> formats) => formats.Any() ? FormatInternal(value, member, formats) : AsStringInternal(value, member);