public FormatDetails(SmartFormatter formatter, object[] originalArgs, FormatCache formatCache, IFormatProvider provider) { Formatter = formatter; OriginalArgs = originalArgs; FormatCache = formatCache; Provider = provider; }
private void Initialize() { this.initialized = true; BindingStringFormat.TryGet(this.binding, out this.bindingQuantityFormat); if (!string.IsNullOrEmpty(this.bindingQuantityFormat?.SymbolFormat)) { if (this.ValueFormat != null) { this.errorText.AppendLine($"ValueFormat cannot be set when Binding.StringFormat is a unit format."); } if (this.StringFormat != null) { this.errorText.AppendLine($"ValueFormat cannot be set when Binding.StringFormat is a unit format."); } } if (this.quantityFormat != null) { if (this.ValueFormat != null) { this.errorText.AppendLine($"Both ValueFormat and StringFormat cannot be set."); } } else { if (this.unit != null && SymbolFormat != null) { this.quantityFormat = FormatCache <MagneticFieldStrengthUnit> .GetOrCreate(ValueFormat, this.unit.Value, SymbolFormat.Value); } } if (this.unit == null) { var hasFmtUnit = !string.IsNullOrEmpty(this.quantityFormat?.SymbolFormat); var hasBindingUnit = !string.IsNullOrEmpty(this.bindingQuantityFormat?.SymbolFormat); if (!hasFmtUnit && !hasBindingUnit) { this.errorText.AppendLine($"Unit cannot be null."); this.errorText.AppendLine($"Must be specified Explicitly or in StringFormat or in Binding.StringFormat."); } else if (hasFmtUnit && !hasBindingUnit) { this.unit = this.quantityFormat.Unit; } else if (!hasFmtUnit && hasBindingUnit) { this.unit = this.bindingQuantityFormat.Unit; } else { if (this.quantityFormat.Unit != this.bindingQuantityFormat.Unit) { this.errorText.AppendLine($"Ambiguous units StringFormat: {quantityFormat.CompositeFormat} Binding.StringFormat: {this.bindingQuantityFormat.CompositeFormat}"); } this.unit = this.quantityFormat.Unit; } } else { if (!string.IsNullOrEmpty(this.quantityFormat?.SymbolFormat) && this.unit != this.quantityFormat.Unit) { this.errorText.AppendLine($"Unit is set to '{this.unit}' but StringFormat is '{StringFormat.Replace("{0:", string.Empty).Replace("}", string.Empty)}'"); } var hasBindingUnit = !string.IsNullOrEmpty(this.bindingQuantityFormat?.SymbolFormat); } if (this.UnitInput == Wpf.UnitInput.SymbolRequired) { if (string.IsNullOrEmpty(this.quantityFormat?.SymbolFormat)) { if (string.IsNullOrEmpty(this.bindingQuantityFormat?.SymbolFormat)) { if (this.unit == null) { this.errorText.AppendLine("UnitInput == SymbolRequired but no unit format is specified"); } else if (this.SymbolFormat != null) { this.quantityFormat = FormatCache <MagneticFieldStrengthUnit> .GetOrCreate(this.ValueFormat, this.unit.Value, this.SymbolFormat.Value); if (this.UnitInput == null) { this.UnitInput = Wpf.UnitInput.SymbolRequired; } else if (this.UnitInput == Wpf.UnitInput.ScalarOnly) { this.errorText.AppendLine("Cannot have ScalarOnly and SymbolFormat specified"); } } else { this.quantityFormat = FormatCache <MagneticFieldStrengthUnit> .GetOrCreate(this.ValueFormat, this.unit.Value); if (this.UnitInput == null) { this.UnitInput = Wpf.UnitInput.ScalarOnly; } else if (this.UnitInput == Wpf.UnitInput.ScalarOnly) { this.errorText.AppendLine("Cannot have ScalarOnly and SymbolFormat specified"); } } } else { this.quantityFormat = this.bindingQuantityFormat; } } } if (UnitInput == null) { if (!string.IsNullOrEmpty(this.quantityFormat?.SymbolFormat) || !string.IsNullOrEmpty(this.bindingQuantityFormat?.SymbolFormat)) { UnitInput = Wpf.UnitInput.SymbolRequired; } } }
/// <summary> Formats the specified arguments using this string as a template. /// Caches the parsing results for increased performance. /// </summary> /// <param name="format">The template that defines how the arguments are formatted</param> /// <param name="args">A list of arguments to be used in formatting</param> /// <param name="cache">Outputs an object that increases performance if the same format string is used repeatedly.</param> public static string FormatSmart(this string format, ref FormatCache cache, params object[] args) { // With cache: return(Smart.Default.FormatWithCache(ref cache, format, args)); }
public void PerformanceTest_ComparedTo_StringFormat() { // Create the most basic formatter: //var Smart = new SmartFormatter(); //Smart.AddExtensions( // new DefaultFormatter(), // new DefaultSource() //); // Setup the test criteria: var tests = new[] { new { title = "Test - No Placeholders", format = "No Placeholders", expected = "No Placeholders", }, new { title = "Test - Very simple", format = "Zero: '{0}'", expected = "Zero: 'Zero'", }, new { title = "Test - Format a number", format = "One: {1:N2}", expected = "One: 1.00", }, new { title = "Test - Format a date", format = "Two: {2:d}", expected = "Two: 10/10/2010", }, new { title = "Test - All Three", format = "Zero: '{0}' One: {1:N2} Two: {2:d}", expected = "Zero: 'Zero' One: 1.00 Two: 10/10/2010", }, new { title = "Test - Lengthy format string", format = "Zero: '{0}' One: {1:N2} / {1:000} / {1:###} Two: {2:d} / {2:D} / {2:ddd, MMM d, ''yy} / {2:t} Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ", expected = "Zero: 'Zero' One: 1.00 / 001 / 001 Two: 10/10/2010 / Sunday, October 10, 2010 / Sun, Oct 10, '10 / 12:00:00 am Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ", }, }; var args = new object[] { "Zero", 1, new DateTime(2010, 10, 10) }; FormatCache cache = null; FormatCache cache2 = null; var NoExtensions = new SmartFormatter(); NoExtensions.AddExtensions(new DefaultSource(NoExtensions)); NoExtensions.AddExtensions(new DefaultFormatter()); var formatters = new[] { new { Title = "String.Format", Function = new Func <string, object[], string>(string.Format), }, new { Title = "Smart.Format", Function = new Func <string, object[], string>(Smart.Format), }, new { Title = "NoExtensions.Format", Function = new Func <string, object[], string>(NoExtensions.Format), }, new { Title = "Smart.FormatWithCache", Function = new Func <string, object[], string>((format, args2) => Smart.Default.FormatWithCache(ref cache, format, args2)), }, new { Title = "NoExtensions.FormatWithCache", Function = new Func <string, object[], string>((format, args2) => NoExtensions.FormatWithCache(ref cache2, format, args2)), }, }; const int iterations = 100000; foreach (var test in tests) { cache = null; cache2 = null; var results = TestHelpers.PerformanceTest(formatters.Select(f => f.Function).ToArray(), test.format, args, iterations); // Compare the results: Console.WriteLine("{0} Results: \"{1}\" => \"{2}\"", test.title, test.format, test.expected); var baseSeconds = results[0].TotalSeconds; Console.WriteLine("Test Function Ratio to String.Format Actual time taken"); for (int i = 0; i < formatters.Length; i++) { var f = formatters[i]; var r = results[i]; Console.WriteLine("{0,-25} 1 : {3:N2} {2:N1}µs per iteration {1:N2}s total)", f.Title, r.TotalSeconds, r.TotalSeconds * (double)1000000 / iterations, r.TotalSeconds / baseSeconds); } Console.WriteLine(); } }
public AgentImplementationController(StreamWrapper wrapper, ContextManager contextManager, FormatCache aFormatCache) : base(wrapper, contextManager) { formatCache = aFormatCache; knownFormatCollector = new KnownFormatCollector(); }