Пример #1
0
 private void UpdateMainValue()
 {
     MainValue = new SmartNumber
     {
         Value         = projectContext.FlagsNumber.Value,
         NumericalBase = projectContext.NumericalBaseService,
         BitCount      = projectContext.FlagsNumber.BitCount
     };
 }
Пример #2
0
 private void UpdateFlagValue()
 {
     FlagValue = new SmartNumber
     {
         Value         = flagItem.Value,
         NumericalBase = numericalBaseService,
         BitCount      = flagItem.Parent?.BitCount ?? 0
     };
 }
        private static object RenderHexadecimal(SmartNumber smartNumber)
        {
            string xamlContent = smartNumber.ToString();

            List <string> result  = new List <string>();
            bool          isBegin = true;

            foreach (char c in xamlContent)
            {
                bool isDigit = char.IsDigit(c) || (c >= 'A' && c <= 'F');

                if (isBegin)
                {
                    if (isDigit && c != '0')
                    {
                        isBegin = false;
                        result.Add($@"<Run Foreground=""Black"">{c}</Run>");
                    }
                    else
                    {
                        result.Add(c.ToString());
                    }
                }
                else
                {
                    if (isDigit)
                    {
                        result.Add($@"<Run Foreground=""Black"">{c}</Run>");
                    }
                    else
                    {
                        result.Add(c.ToString());
                    }
                }
            }

            xamlContent = string.Join("", result);

            string xamlTextBlock = @"
            <TextBlock
                xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                Padding = ""5""
                VerticalAlignment = ""Center""
                HorizontalAlignment = ""Stretch""
                Foreground = ""Gray"">

                {0}

            </TextBlock>";

            xamlTextBlock = string.Format(xamlTextBlock, xamlContent);

            using (StringReader stringReader = new StringReader(xamlTextBlock))
                using (XmlReader xmlReader = XmlReader.Create(stringReader))
                    return(XamlReader.Load(xmlReader));
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            SmartNumber smartNumber = value as SmartNumber;

            if (smartNumber != null)
            {
                return(ConvertFromSmartNumber(smartNumber));
            }

            return(null);
        }
        public void does_not_add_zeroes_to_the_left_if_BitCount_is_more_then_real_digit_count()
        {
            SmartNumber smartValue = new SmartNumber
            {
                Value         = 13,
                NumericalBase = NumericalBase.Decimal,
                BitCount      = 16
            };

            string actual = smartValue.ToString();

            Assert.That(actual, Is.EqualTo("13"));
        }
Пример #6
0
        public void does_not_add_zeroes_to_the_left_if_BitCount_0()
        {
            SmartNumber smartValue = new SmartNumber
            {
                Value         = 13,
                NumericalBase = NumericalBase.Hexadecimal,
                BitCount      = 0
            };

            string actual = smartValue.ToString();

            Assert.That(actual, Is.EqualTo("D"));
        }
Пример #7
0
        public void returns_string_empty_if_numerical_base_is_None()
        {
            SmartNumber smartValue = new SmartNumber
            {
                Value         = 13,
                NumericalBase = NumericalBase.None,
                BitCount      = 16
            };

            string actual = smartValue.ToString();

            Assert.That(actual, Is.Empty);
        }
Пример #8
0
        public void groups_digits_by_two_with_padding()
        {
            SmartNumber smartValue = new SmartNumber
            {
                Value         = 1000,
                NumericalBase = NumericalBase.Hexadecimal,
                BitCount      = 16
            };

            string actual = smartValue.ToString();

            Assert.That(actual, Is.EqualTo("03 E8"));
        }
Пример #9
0
        public void adds_zeroes_to_the_left_if_BitCount_is_more_then_real_digit_count()
        {
            SmartNumber smartValue = new SmartNumber
            {
                Value         = 13,
                NumericalBase = NumericalBase.Binary,
                BitCount      = 8
            };

            string actual = smartValue.ToString();

            Assert.That(actual, Is.EqualTo("0000 1101"));
        }
Пример #10
0
        public void returns_only_zeros_if_value_is_zero_and_BitCount_is_8()
        {
            SmartNumber smartValue = new SmartNumber
            {
                Value         = 0,
                NumericalBase = NumericalBase.Binary,
                BitCount      = 8
            };

            string actual = smartValue.ToString();

            Assert.That(actual, Is.EqualTo("0000 0000"));
        }
Пример #11
0
        public void groups_digits_by_four_with_padding()
        {
            SmartNumber smartValue = new SmartNumber
            {
                Value         = 93,
                NumericalBase = NumericalBase.Binary,
                BitCount      = 8
            };

            string actual = smartValue.ToString();

            Assert.That(actual, Is.EqualTo("0101 1101"));
        }
        public void does_not_add_zeroes_to_the_left_if_PadLeft_is_false()
        {
            SmartNumber smartValue = new SmartNumber
            {
                Value         = 13,
                NumericalBase = NumericalBase.Decimal,
                BitCount      = 16,
                PadLeft       = false
            };

            string actual = smartValue.ToString();

            Assert.That(actual, Is.EqualTo("13"));
        }
Пример #13
0
        public void throws_if_NumericalBase_has_invalid_value()
        {
            SmartNumber smartValue = new SmartNumber
            {
                Value         = 13,
                NumericalBase = (NumericalBase)1000,
                BitCount      = 16,
                PadLeft       = false
            };

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                smartValue.ToString();
            });
        }
Пример #14
0
        protected override void DoExecute(object parameter)
        {
            NumericalBase numericalBase = projectContext.NumericalBaseService;

            SmartNumber numberSerializer = new SmartNumber
            {
                Value         = projectContext.FlagsNumber.Value,
                NumericalBase = numericalBase,
                BitCount      = projectContext.FlagsNumber.BitCount,
                GroupLength   = 0,
                PadLeft       = numericalBase == NumericalBase.Binary
            };

            string text = numberSerializer.ToString();

            Clipboard.SetText(text);
        }
        private static object ConvertFromSmartNumber(SmartNumber smartNumber)
        {
            switch (smartNumber.NumericalBase)
            {
            case NumericalBase.None:
            case NumericalBase.Decimal:
                return(smartNumber.ToString());

            case NumericalBase.Hexadecimal:
                return(RenderHexadecimal(smartNumber));

            case NumericalBase.Binary:
                return(RenderBinary(smartNumber));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private static object RenderBinary(SmartNumber smartNumber)
        {
            string xamlContent = smartNumber.ToString();

            xamlContent = xamlContent.Replace("1", @"<Run Foreground = ""Black"">1</Run>");

            string xamlTextBlock = @"
            <TextBlock
                xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                Padding = ""5""
                VerticalAlignment = ""Center""
                HorizontalAlignment = ""Stretch""
                Foreground = ""Gray"">

                {0}

            </TextBlock>";

            xamlTextBlock = string.Format(xamlTextBlock, xamlContent);

            using (StringReader stringReader = new StringReader(xamlTextBlock))
                using (XmlReader xmlReader = XmlReader.Create(stringReader))
                    return(XamlReader.Load(xmlReader));
        }