示例#1
0
 public override void Fire(RbnfContext context)
 {
     foreach (var sub in Substitutions)
     {
         Apply(sub, context);
     }
 }
示例#2
0
 public override void Fire(RbnfContext context)
 {
     foreach (var sub in Substitutions)
     {
         context.Text.Append(sub.Text);
     }
 }
示例#3
0
        public override void Fire(RbnfContext context)
        {
            foreach (var sub in Substitutions)
            {
                context.Text.Append(sub.Text);
                switch (sub.Token)
                {
                case "→→":
                    context.Number = Math.Abs(context.Number);
                    if (context.DoubleNumber.HasValue)
                    {
                        if (double.IsNegativeInfinity(context.DoubleNumber.Value))
                        {
                            context.DoubleNumber = double.PositiveInfinity;
                        }
                        else
                        {
                            context.DoubleNumber = Math.Abs(context.DoubleNumber.Value);
                        }
                    }
                    context.Ruleset.ApplyRules(context);
                    break;

                case "":
                    break;

                default:
                    throw new NotSupportedException($"Substitution token '{sub.Token}' is not allowed.");
                }
            }
        }
示例#4
0
        /// <summary>
        ///   Formats a <see cref="decimal"/> using the specified rule set name.
        /// </summary>
        /// <param name="value">
        ///   The number to format.
        /// </param>
        /// <param name="type">
        ///   The rule set name, such as "spellout-numbering" or "spellout-cardinal".
        /// </param>
        /// <returns>
        ///   The string representation of the <paramref name="value"/>.
        /// </returns>
        public string Format(decimal value, string type)
        {
            var ctx = new RbnfContext
            {
                Number       = value,
                RulesetGroup = this
            };

            Rulesets[type].ApplyRules(ctx);

            return(ctx.Text.ToString());
        }
示例#5
0
        /// <summary>
        ///   Formats a <see cref="decimal"/> using the specified rule set name.
        /// </summary>
        /// <param name="value">
        ///   The number to format.
        /// </param>
        /// <param name="type">
        ///   The rule set name, such as "spellout-numbering" or "spellout-cardinal".
        /// </param>
        /// <param name="locale">
        ///   The fall-back <see cref="Locale"/>.
        /// </param>
        /// <returns>
        ///   The string representation of the <paramref name="value"/>.
        /// </returns>
        public string Format(decimal value, string type, Locale locale = null)
        {
            var ctx = new RbnfContext
            {
                Number       = value,
                RulesetGroup = this,
                Locale       = locale ?? Locale.Create("root")
            };

            Rulesets[type].ApplyRules(ctx);

            return(ctx.Text.ToString());
        }
示例#6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        public void ApplyRules(RbnfContext context)
        {
            var previous = context.Ruleset;

            context.Ruleset = this;
            var rule = Rules.FirstOrDefault(r => r.Matches(context));

            if (rule != null)
            {
                rule.Fire(context);
            }
            context.Ruleset = previous;
        }
示例#7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        public void ApplyRules(RbnfContext context)
        {
            var previous = context.Ruleset;

            context.Ruleset = this;

            // If only one rule, then always fire it.
            var rule = (Rules.Count == 1)
                ? Rules.First()
                : Rules.FirstOrDefault(r => r.Matches(context));

            if (rule != null)
            {
                rule.Fire(context);
            }
            context.Ruleset = previous;
        }
示例#8
0
        void Apply(Substitution sub, RbnfContext context)
        {
            var number = context.Number;

            context.Text.Append(sub.Text);
            switch (sub.Token)
            {
            case "→→":
                context.Number = Decimal.Remainder(number, Divisor());
                context.Ruleset.ApplyRules(context);
                break;

            case "←←":
                context.Number = Math.Floor(number / Divisor());
                context.Ruleset.ApplyRules(context);
                break;

            case "=":
                // Fallback number formating?
                if (sub.Descriptor.StartsWith("#") || sub.Descriptor.StartsWith("0"))
                {
                    context.Text.Append(context.Number.ToString(sub.Descriptor, CultureInfo.InvariantCulture));
                }

                // Else goto the ruleset.
                else
                {
                    var ruleset = context.RulesetGroup.Rulesets[sub.Descriptor];
                    ruleset.ApplyRules(context);
                }
                break;

            case "":
                break;

            default:
                throw new NotSupportedException($"Substitution token '{sub.Token}' is not allowed.");
            }

            if (sub.Optional != null && (number % Divisor()) != 0)
            {
                Apply(sub.Optional, context);
            }

            context.Number = number;
        }
示例#9
0
        /// <summary>
        ///   Formats a <see cref="double"/> using the specified rule set name.
        /// </summary>
        /// <param name="value">
        ///   The number to format.
        /// </param>
        /// <param name="type">
        ///   The rule set name, such as "spellout-numbering" or "spellout-cardinal".
        /// </param>
        /// <returns>
        ///   The string representation of the <paramref name="value"/>.
        /// </returns>
        public string Format(double value, string type)
        {
            var ctx = new RbnfContext
            {
                DoubleNumber = value,
                RulesetGroup = this
            };

            if (!double.IsInfinity(value) && !double.IsNaN(value))
            {
                ctx.Number = (decimal)value;
            }

            Rulesets[type].ApplyRules(ctx);

            return(ctx.Text.ToString());
        }
示例#10
0
        /// <summary>
        ///   Formats a <see cref="double"/> using the specified rule set name.
        /// </summary>
        /// <param name="value">
        ///   The number to format.
        /// </param>
        /// <param name="type">
        ///   The rule set name, such as "spellout-numbering" or "spellout-cardinal".
        /// </param>
        /// <param name="locale">
        ///   The fall-back <see cref="Locale"/>.
        /// </param>
        /// <returns>
        ///   The string representation of the <paramref name="value"/>.
        /// </returns>
        public string Format(double value, string type, Locale locale = null)
        {
            var ctx = new RbnfContext
            {
                DoubleNumber = value,
                RulesetGroup = this,
                Locale       = locale ?? Locale.Create("root")
            };

            if (!double.IsInfinity(value) && !double.IsNaN(value))
            {
                ctx.Number = (decimal)value;
            }

            Rulesets[type].ApplyRules(ctx);

            return(ctx.Text.ToString());
        }
示例#11
0
 public override bool Matches(RbnfContext context)
 {
     return(LowerLimit <= context.Number && context.Number <= UpperLimit);
 }
示例#12
0
 public override bool Matches(RbnfContext context)
 {
     return(Math.Truncate(context.Number) != context.Number);
 }
示例#13
0
        public override void Fire(RbnfContext context)
        {
            var number = context.Number;

            foreach (var sub in Substitutions)
            {
                context.Text.Append(sub.Text);
                switch (sub.Token)
                {
                case "←←":
                    context.Number = Math.Truncate(number);
                    context.Ruleset.ApplyRules(context);
                    break;

                case "→→":
                {
                    var sep = context.Text[context.Text.Length - 1];
                    var x   = number - Math.Truncate(number);
                    while (x != 0)
                    {
                        if (sep != context.Text[context.Text.Length - 1])
                        {
                            context.Text.Append(sep);
                        }
                        x = x * 10;
                        context.Number = Math.Truncate(x);
                        context.Ruleset.ApplyRules(context);
                        x = x - Math.Truncate(x);
                    }
                }
                break;

                case "→→→":
                {
                    var x = number - Math.Truncate(number);
                    while (x != 0)
                    {
                        x = x * 10;
                        context.Number = Math.Truncate(x);
                        context.Ruleset.ApplyRules(context);
                        x = x - Math.Truncate(x);
                    }
                }
                break;

                case "":
                    break;

                case "=":
                    // Fallback number formating?
                    if (sub.Descriptor.StartsWith("#") || sub.Descriptor.StartsWith("0"))
                    {
                        context.Text.Append(context.Number.ToString(sub.Descriptor, CultureInfo.InvariantCulture));
                    }

                    // Else goto the ruleset.
                    else
                    {
                        var ruleset = context.RulesetGroup.Rulesets[sub.Descriptor];
                        ruleset.ApplyRules(context);
                    }
                    break;

                default:
                    throw new NotSupportedException($"Substitution token '{sub.Token}' is not allowed.");
                }
            }
            context.Number = number;
        }
示例#14
0
        void Apply(Substitution sub, RbnfContext context)
        {
            var number = context.Number;

            context.Text.Append(sub.Text);
            switch (sub.Token)
            {
            // Divide the number by the rule's divisor and format the remainder.
            case "→→":
                context.Number = Decimal.Remainder(number, Divisor());
                context.Ruleset.ApplyRules(context);
                break;

            // Divide the number by the rule's divisor and format the quotient.
            case "←←":
                context.Number = Math.Floor(number / Divisor());
                context.Ruleset.ApplyRules(context);
                break;

            case "=":
                // Fallback number formating?
                if (sub.Descriptor.StartsWith("#") || sub.Descriptor.StartsWith("0"))
                {
                    var formatter = NumberFormatter.Create(context.Locale);
                    context.Text.Append(formatter.Format(context.Number));
                }

                // Else goto the ruleset.
                else
                {
                    var ruleset = context.RulesetGroup.Rulesets[sub.Descriptor];
                    ruleset.ApplyRules(context);
                }
                break;

            // Divide the number by the rule's divisor and format the remainder
            // with the specified ruleset.
            case "→":
                context.Number = Decimal.Remainder(number, Divisor());
                context.RulesetGroup
                .Rulesets[sub.Descriptor]
                .ApplyRules(context);
                break;

            // Divide the number by the rule's divisor and format the quotient
            // with the specified ruleset.
            case "←":
                context.Number = Math.Floor(number / Divisor());
                context.RulesetGroup
                .Rulesets[sub.Descriptor]
                .ApplyRules(context);
                break;

            case "":
                break;

            default:
                throw new NotSupportedException($"Substitution token '{sub.Token}' is not allowed.");
            }

            if (sub.Optionals != null && (number % Divisor()) != 0)
            {
                foreach (var optional in sub.Optionals)
                {
                    Apply(optional, context);
                }
            }

            context.Number = number;
        }
示例#15
0
 public override bool Matches(RbnfContext context)
 {
     throw new NotImplementedException();
 }
示例#16
0
 public override bool Matches(RbnfContext context)
 {
     return(context.DoubleNumber.HasValue && double.IsNaN(context.DoubleNumber.Value));
 }
示例#17
0
 /// <inheritdoc />
 public abstract void Fire(RbnfContext context);
示例#18
0
 public override bool Matches(RbnfContext context)
 {
     return(context.Number < 0 || (context.DoubleNumber.HasValue && double.IsNegativeInfinity(context.DoubleNumber.Value)));
 }
示例#19
0
 /// <inheritdoc />
 public abstract bool Matches(RbnfContext context);
示例#20
0
 public override void Fire(RbnfContext context)
 {
 }