/// <summary>
        /// Create a new <see cref="NumberConvertOptions"/> based on the provided flags
        /// </summary>
        public NumberConvertOptions(ParseNumericStringFlags parseFlags)
        {
            this.ParseFlags = parseFlags;

            this.ParseHex            = ParseFlags.HasFlag(ParseNumericStringFlags.HexString);
            this.ParseOctal          = ParseFlags.HasFlag(ParseNumericStringFlags.OctalString);
            this.ParseBinary         = ParseFlags.HasFlag(ParseNumericStringFlags.BinaryString);
            this.AllowDigitSeparator = ParseFlags.HasFlag(ParseNumericStringFlags.AllowDigitSeparator);
        }
Beispiel #2
0
        /// <summary>
        /// Test if the input value is a non-null numeric type
        /// or a string that can be parsed as a number, with detection
        /// of hex strings controlled by <paramref name="parseFlags"/>
        /// </summary>
        /// <param name="value"></param>
        /// <param name="parseFlags"></param>
        public static bool IsNumeric(object value, ParseNumericStringFlags parseFlags)
        {
            if (null == value)
            {
                return(false);
            }
            if (value is string sValue)
            {
                if (double.TryParse(sValue, out _))
                {
                    return(true);
                }

                bool allowDigitSep = (parseFlags & ParseNumericStringFlags.AllowDigitSeparator) != 0;
                if (allowDigitSep)
                {
                    bool changed = false;
                    while (sValue.IndexOf('_') >= 0)
                    {
                        sValue  = Regex.Replace(sValue, "([0-9a-fA-F])_+([0-9a-fA-F])", "$1$2");
                        changed = true;
                    }

                    if (changed && double.TryParse(sValue, out _))
                    {
                        return(true);
                    }
                }

                if ((parseFlags & ParseNumericStringFlags.HexString) != 0)
                {
                    if (Regex.IsMatch(sValue, @"^\s*0[xX][0-9a-fA-F]+$"))
                    {
                        return(true);
                    }
                }

                if ((parseFlags & ParseNumericStringFlags.OctalString) != 0)
                {
                    if (Regex.IsMatch(sValue, @"^\s*0[oO][0-7]+$"))
                    {
                        return(true);
                    }
                }

                if ((parseFlags & ParseNumericStringFlags.BinaryString) != 0)
                {
                    if (Regex.IsMatch(sValue, @"^\s*0[bB][01]+$"))
                    {
                        return(true);
                    }
                }

                return(false);
            }
            return(TypeInspection.IsNumberType(value.GetType()));
        }
 /// <summary>
 /// Set or update the <see cref="NumberConvertOptions"/> with the provided parameters
 /// </summary>
 /// <param name="parseFlags">Flags controlling how to parse numeric strings</param>
 /// <returns>A new <see cref="ConvertOptionsBuilder"/> with updated settings</returns>
 /// <see cref="NumberConvertOptions"/>
 /// <see cref="ParseNumericStringFlags"/>
 public ConvertOptionsBuilder WithNumberOptions(ParseNumericStringFlags parseFlags)
 => new ConvertOptionsBuilder(this, new NumberConvertOptions(parseFlags));
Beispiel #4
0
 public ConvertOptionsBuilder WithNumberOptions(ParseNumericStringFlags parseFlags) => throw null;
Beispiel #5
0
 public NumberConvertOptions(ParseNumericStringFlags parseFlags) => throw null;
Beispiel #6
0
 public static bool IsNumeric(object value, ParseNumericStringFlags parseFlags) => throw null;