Exemple #1
0
 /// <summary>
 /// Instance constructor
 /// </summary>
 /// <param name="literal"></param>
 /// <param name="digit"></param>
 /// <param name="flags"></param>
 public LiteralNumeral(string literal, uint digit, NumeralFlags flags = 0)
 {
     Literal = literal;
     Digit   = digit;
     Flags   = flags;
     //Flags = AdjustFlags(literal, flags);
 }
Exemple #2
0
 /// <summary>
 /// Converts the given <see cref="int"/> to Roman numerals
 /// </summary>
 /// <param name="value"></param>
 /// <param name="flags"></param>
 /// <returns></returns>
 public static string ToRomanNumerals(this int value, NumeralFlags flags = 0)
 {
     if (value < 0)
     {
         throw new ArgumentException("Only positive integers are supported");
     }
     return(NumeralBuilder.Build((uint)value, flags));
 }
Exemple #3
0
 /// <summary>
 /// Tries to get a numeral matching the digit and flags
 /// </summary>
 /// <param name="digit"></param>
 /// <param name="flags"></param>
 /// <returns></returns>
 public static LiteralNumeral TryGet(uint digit, NumeralFlags flags)
 {
     if (!LiteralNumerals.TryGetValue(digit, out var numerals))
     {
         return(null);
     }
     return(numerals.FirstOrDefault(v => v.Matches(flags)));
 }
Exemple #4
0
        private static NumeralFlags AdjustFlags(string literal, NumeralFlags flags)
        {
            foreach (var c in literal)
            {
                if (c >= 0x80)
                {
                    flags |= NumeralFlags.Unicode;
                    break;
                }
            }

            return(flags);
        }
        /// <summary>
        /// Builds roman numeral from integer
        /// </summary>
        /// <param name="v"></param>
        /// <param name="flags"></param>
        /// <returns></returns>
        public static string Build(uint v, NumeralFlags flags = 0)
        {
            // in case nothing was specified
            if ((flags & (NumeralFlags.Ascii | NumeralFlags.Unicode)) == 0)
            {
                flags |= NumeralFlags.Ascii;
            }
            var builder   = new StringBuilder();
            var remaining = v;

            foreach (var digit in EnumerateDigits(v))
            {
                if (digit == 0)
                {
                    continue;
                }

                // hint: if the remaining value fits in one digit, use it
                var literalNumeral = RomanNumeralsDefinition.TryGet(remaining, flags);
                if (literalNumeral != null)
                {
                    builder.Append(literalNumeral.Literal);
                    break;
                }

                // look for single digit
                literalNumeral = RomanNumeralsDefinition.TryGet(digit, flags);
                if (literalNumeral is null)
                {
                    throw new ArgumentOutOfRangeException(nameof(v), "Can't convert number");
                }
                builder.Append(literalNumeral.Literal);
                remaining -= v;
            }

            return(builder.ToString());
        }
Exemple #6
0
 /// <summary>
 /// Converts the given <see cref="uint"/> to Roman numerals
 /// </summary>
 /// <param name="value"></param>
 /// <param name="flags"></param>
 /// <returns></returns>
 public static string ToRomanNumerals(this uint value, NumeralFlags flags = 0)
 {
     return(NumeralBuilder.Build(value, flags));
 }
Exemple #7
0
 /// <summary>
 /// Indicates if this instance can be used with requested flags
 /// </summary>
 /// <param name="flags"></param>
 /// <returns></returns>
 public bool Matches(NumeralFlags flags)
 {
     return((Flags & ~flags) == 0);
 }