Esempio n. 1
0
        /// <summary>
        /// Gets the text for an optional sign.
        /// </summary>
        /// <param name="sign">The sign.</param>
        protected string GetSignText(OptionalSign sign)
        {
            string Result   = string.Empty;
            bool   IsParsed = false;

            switch (sign)
            {
            case OptionalSign.None:
                IsParsed = true;
                break;

            case OptionalSign.Positive:
                Result   = "+";
                IsParsed = true;
                break;

            case OptionalSign.Negative:
                Result   = "-";
                IsParsed = true;
                break;
            }

            Debug.Assert(IsParsed);

            return(Result);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FormattedInteger"/> class.
        /// </summary>
        /// <param name="integerBase">The base.</param>
        /// <param name="sign">The optional sign.</param>
        /// <param name="leadingZeroCount">The number of leading zeroes.</param>
        /// <param name="integerText">The integer text..</param>
        /// <param name="invalidText">The trailing invalid text, if any.</param>
        /// <param name="canonical">The canonical form of the number.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="leadingZeroCount"/> is lesser than zero.</exception>
        internal FormattedInteger(IIntegerBase integerBase, OptionalSign sign, int leadingZeroCount, string integerText, string invalidText, CanonicalNumber canonical)
            : base(invalidText, canonical)
        {
            IntegerBase      = integerBase;
            Sign             = sign;
            LeadingZeroCount = leadingZeroCount;
            IntegerText      = integerText;

            if (leadingZeroCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(leadingZeroCount));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CanonicalNumber"/> class.
        /// </summary>
        /// <param name="significandSign">The sign of the significand.</param>
        /// <param name="significandText">The significand.</param>
        /// <param name="exponentSign">The sign of the exponent.</param>
        /// <param name="exponentText">The exponent.</param>
        public CanonicalNumber(OptionalSign significandSign, string significandText, OptionalSign exponentSign, string exponentText)
        {
            Debug.Assert(IntegerBase.Decimal.IsValidNumber(exponentText, supportLeadingZeroes: false));
            Debug.Assert(significandText != IntegerBase.Zero || (significandSign != OptionalSign.Negative && exponentSign != OptionalSign.Negative && exponentText == IntegerBase.Zero));

            SignificandSign = significandSign;
            SignificandText = significandText;
            ExponentSign    = exponentSign;
            ExponentText    = exponentText;

            FormatCanonicString();

            NumberFloat = CreateFloat();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CanonicalNumber"/> class.
        /// </summary>
        /// <param name="significandSign">The sign of the significand.</param>
        /// <param name="integerText">The integer significand.</param>
        public CanonicalNumber(OptionalSign significandSign, string integerText)
        {
            // For the value 0, use the CanonicalNumber.Zero constant.
            Debug.Assert(integerText != IntegerBase.Zero);

            SignificandSign = significandSign;
            SignificandText = integerText + Parser.NeutralDecimalSeparator + IntegerBase.Zero;
            ExponentSign    = OptionalSign.None;
            ExponentText    = IntegerBase.Zero;

            FormatCanonicString();

            NumberFloat = CreateFloat();
        }
Esempio n. 5
0
        private CanonicalNumber GetCanonical(OptionalSign significandSign, string significandText, OptionalSign exponentSign, string exponentText)
        {
            CanonicalNumber Canonical;

            if (significandText == IntegerBase.Zero)
            {
                Canonical = CanonicalNumber.Zero;
            }
            else
            {
                Canonical = new CanonicalNumber(significandSign, significandText, exponentSign, exponentText);
            }

            return(Canonical);
        }
Esempio n. 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FormattedReal"/> class.
        /// </summary>
        /// <param name="sign">The optional sign.</param>
        /// <param name="leadingZeroCount">The number of leading zeroes.</param>
        /// <param name="integerText">The text before the decimal separator character. Can be empty.</param>
        /// <param name="separatorCharacter">The decimal separator character. Can be <see cref="Parser.NoSeparator"/>.</param>
        /// <param name="fractionalText">The text after the decimal separator character and before the exponent. Can be empty.</param>
        /// <param name="exponentCharacter">The exponent character (e or E). Can be <see cref="Parser.NoSeparator"/>.</param>
        /// <param name="exponentSign">The optional exponent sign.</param>
        /// <param name="exponentText">The text after the exponent character. Can be empty.</param>
        /// <param name="invalidText">The trailing invalid text, if any.</param>
        /// <param name="canonical">The canonical form of the number.</param>
        /// <exception cref="NullReferenceException"><paramref name="invalidText"/> or <paramref name="canonical"/> is null.</exception>
        internal FormattedReal(OptionalSign sign, int leadingZeroCount, string integerText, char separatorCharacter, string fractionalText, char exponentCharacter, OptionalSign exponentSign, string exponentText, string invalidText, CanonicalNumber canonical)
            : base(invalidText, canonical)
        {
            Sign               = sign;
            LeadingZeroCount   = leadingZeroCount;
            IntegerText        = integerText ?? throw new ArgumentNullException(nameof(integerText), "Value cannot be null.");
            SeparatorCharacter = separatorCharacter;
            FractionalText     = fractionalText ?? throw new ArgumentNullException(nameof(fractionalText), "Value cannot be null.");
            ExponentCharacter  = exponentCharacter;
            ExponentSign       = exponentSign;
            ExponentText       = exponentText ?? throw new ArgumentNullException(nameof(exponentText), "Value cannot be null.");

            if (leadingZeroCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(leadingZeroCount));
            }

            if (SeparatorCharacter != Parser.NoSeparator)
            {
                if (integerText.Length + fractionalText.Length == 0)
                {
                    throw new ArgumentException($"Either {nameof(integerText)} or {nameof(fractionalText)} must not be empty.");
                }
            }
            else
            {
                if (integerText.Length == 0)
                {
                    throw new ArgumentException($"{nameof(integerText)} must not be empty.");
                }
            }

            if (exponentCharacter != Parser.NoSeparator)
            {
                if (exponentText.Length == 0)
                {
                    throw new ArgumentException($"{nameof(exponentText)} must not be empty.");
                }
            }
        }