public override void Fire(RbnfContext context) { foreach (var sub in Substitutions) { Apply(sub, context); } }
public override void Fire(RbnfContext context) { foreach (var sub in Substitutions) { context.Text.Append(sub.Text); } }
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."); } } }
/// <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()); }
/// <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()); }
/// <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; }
/// <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; }
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; }
/// <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()); }
/// <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()); }
public override bool Matches(RbnfContext context) { return(LowerLimit <= context.Number && context.Number <= UpperLimit); }
public override bool Matches(RbnfContext context) { return(Math.Truncate(context.Number) != context.Number); }
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; }
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; }
public override bool Matches(RbnfContext context) { throw new NotImplementedException(); }
public override bool Matches(RbnfContext context) { return(context.DoubleNumber.HasValue && double.IsNaN(context.DoubleNumber.Value)); }
/// <inheritdoc /> public abstract void Fire(RbnfContext context);
public override bool Matches(RbnfContext context) { return(context.Number < 0 || (context.DoubleNumber.HasValue && double.IsNegativeInfinity(context.DoubleNumber.Value))); }
/// <inheritdoc /> public abstract bool Matches(RbnfContext context);
public override void Fire(RbnfContext context) { }