public void TestGuess(string input, string expectedDecimalSeparator, string expectedGroupSeparator) { var nf = NumberFormat.GuessNumberFormat(input); Check.That(nf.NumberDecimalSeparator).IsEqualTo(expectedDecimalSeparator); Check.That(nf.NumberGroupSeparator).IsEqualTo(expectedGroupSeparator); }
/// <summary> /// Creates the comparer from the user input. /// </summary> /// <returns>The scan comparer.</returns> private IScanComparer CreateComparer(ScanSettings settings) { Contract.Requires(settings != null); Contract.Ensures(Contract.Result <IScanComparer>() != null); var compareType = compareTypeComboBox.SelectedValue; var checkBothInputFields = compareType == ScanCompareType.Between || compareType == ScanCompareType.BetweenOrEqual; if (settings.ValueType == ScanValueType.Byte || settings.ValueType == ScanValueType.Short || settings.ValueType == ScanValueType.Integer || settings.ValueType == ScanValueType.Long) { var numberStyle = isHexCheckBox.Checked ? NumberStyles.HexNumber : NumberStyles.Integer; if (!long.TryParse(dualValueBox.Value1, numberStyle, null, out var value1)) { throw new InvalidInputException(dualValueBox.Value1); } if (!long.TryParse(dualValueBox.Value2, numberStyle, null, out var value2) && checkBothInputFields) { throw new InvalidInputException(dualValueBox.Value2); } switch (settings.ValueType) { case ScanValueType.Byte: return(new ByteMemoryComparer(compareType, (byte)value1, (byte)value2)); case ScanValueType.Short: return(new ShortMemoryComparer(compareType, (short)value1, (short)value2)); case ScanValueType.Integer: return(new IntegerMemoryComparer(compareType, (int)value1, (int)value2)); case ScanValueType.Long: return(new LongMemoryComparer(compareType, value1, value2)); } } else if (settings.ValueType == ScanValueType.Float || settings.ValueType == ScanValueType.Double) { int CalculateSignificantDigits(string input, NumberFormatInfo numberFormat) { Contract.Requires(input != null); Contract.Requires(numberFormat != null); var digits = 0; var decimalIndex = input.IndexOf(numberFormat.NumberDecimalSeparator, StringComparison.Ordinal); if (decimalIndex != -1) { digits = input.Length - 1 - decimalIndex; } return(digits); } var nf1 = NumberFormat.GuessNumberFormat(dualValueBox.Value1); if (!double.TryParse(dualValueBox.Value1, NumberStyles.Float, nf1, out var value1)) { throw new InvalidInputException(dualValueBox.Value1); } var nf2 = NumberFormat.GuessNumberFormat(dualValueBox.Value2); if (!double.TryParse(dualValueBox.Value2, NumberStyles.Float, nf2, out var value2) && checkBothInputFields) { throw new InvalidInputException(dualValueBox.Value2); } var significantDigits = Math.Max( CalculateSignificantDigits(dualValueBox.Value1, nf1), CalculateSignificantDigits(dualValueBox.Value2, nf2) ); var roundMode = roundStrictRadioButton.Checked ? ScanRoundMode.Strict : roundLooseRadioButton.Checked ? ScanRoundMode.Normal : ScanRoundMode.Truncate; switch (settings.ValueType) { case ScanValueType.Float: return(new FloatMemoryComparer(compareType, roundMode, significantDigits, (float)value1, (float)value2)); case ScanValueType.Double: return(new DoubleMemoryComparer(compareType, roundMode, significantDigits, value1, value2)); } } else if (settings.ValueType == ScanValueType.ArrayOfBytes) { var pattern = BytePattern.Parse(dualValueBox.Value1); return(new ArrayOfBytesMemoryComparer(pattern)); } else if (settings.ValueType == ScanValueType.String) { if (string.IsNullOrEmpty(dualValueBox.Value1)) { throw new InvalidInputException(dualValueBox.Value1); } var encoding = encodingUtf8RadioButton.Checked ? Encoding.UTF8 : encodingUtf16RadioButton.Checked ? Encoding.Unicode : Encoding.UTF32; return(new StringMemoryComparer(dualValueBox.Value1, encoding, caseSensitiveCheckBox.Checked)); } throw new InvalidOperationException(); }