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 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); }
public string AsString(ValueCollection <EnumFormat> formats) => _enumCache.FormatInternal(Value, this, formats);
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 string FormatFlags(TInt value, string delimiter, ValueCollection <EnumFormat> formats) => FormatFlagsInternal(value, null, delimiter, formats);
private bool TryParseInternal(string value, bool ignoreCase, out TInt result, out EnumMemberInternal <TInt, TIntProvider> member, ValueCollection <EnumFormat> formats, bool getValueOnly) { foreach (var format in formats) { if (format == EnumFormat.UnderlyingValue) { if (Provider.TryParseNative(value, out result)) { member = getValueOnly ? null : GetMember(result); return(true); } } else if (format == EnumFormat.DecimalValue || format == EnumFormat.HexadecimalValue) { if (Provider.TryParseNumber(value, format == EnumFormat.DecimalValue ? NumberStyles.AllowLeadingSign : NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out result)) { member = getValueOnly ? null : GetMember(result); return(true); } } else { var parser = GetEnumMemberParser(format); if (parser.TryParse(value, ignoreCase, out member)) { result = member.Value; return(true); } } } result = default; member = null; return(false); }
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); }
internal string FormatInternal(TInt value, EnumMemberInternal <TInt, TIntProvider> member, ValueCollection <EnumFormat> formats) { var isInitialized = member != null; foreach (var format in formats) { var formattedValue = FormatInternal(value, ref isInitialized, ref member, format); if (formattedValue != null) { return(formattedValue); } } return(null); }
internal string AsStringInternal(TInt value, EnumMemberInternal <TInt, TIntProvider> member, ValueCollection <EnumFormat> formats) => formats.Any() ? FormatInternal(value, member, formats) : AsStringInternal(value, member);
public string AsString(TInt value, ValueCollection <EnumFormat> formats) => AsStringInternal(value, null, formats);
public abstract string?AsString(ValueCollection <EnumFormat> formats);
public abstract bool TryFormat(Span <char> destination, out int charsWritten, ValueCollection <EnumFormat> formats);