From 8d10e8bbd1e19adc7c70e1101dbb69c213c910dd Mon Sep 17 00:00:00 2001 From: fcolin Date: Fri, 22 Aug 2008 16:44:01 +0000 Subject: optimisation for parsing same regular expression from multiple client using fxCop for code beauty fix bug on concurrent connect --- Ivy/IvyApplicationBinding.cs | 69 ++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 21 deletions(-) (limited to 'Ivy/IvyApplicationBinding.cs') diff --git a/Ivy/IvyApplicationBinding.cs b/Ivy/IvyApplicationBinding.cs index 299f459..b5fe303 100644 --- a/Ivy/IvyApplicationBinding.cs +++ b/Ivy/IvyApplicationBinding.cs @@ -1,11 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Collections.Specialized; -using System.Text; -using System.Threading; namespace IvyBus { + using System; + using System.Collections.Generic; + using System.Collections.Specialized; + using System.Text; + using System.Threading; + using System.Text.RegularExpressions; + using System.Collections.ObjectModel; + /* This is the Application side of binding storage */ /* association of a generated Key and a delegate and the expression */ /* this is SEND to other client */ @@ -14,15 +17,20 @@ namespace IvyBus { private BindingType binding; + // Argument formatting Expression to replace argument in in regular expresion binding + // format %number[:objectFormat]% + // where number in argument index and objectFormat the ToString format passed to argument + private Regex re = new Regex(@"%(\d+)(|:(.*))%"); + public BindingType Binding { get { return binding; } set { binding = value; } } - private ushort key; + private int key; - public ushort Key + public int Key { get { return key; } set { key = value; } @@ -30,11 +38,6 @@ namespace IvyBus private object[] args; - public object[] Args - { - get { return args; } - set { args = value; } - } private string expression; public string Expression @@ -42,22 +45,22 @@ namespace IvyBus get { return expression; } set { expression = value; } } - private string formated_expression; - public string FormatedExpression + private string formatted_expression; + public string FormattedExpression { get { FormatExpression(); - return formated_expression; + return formatted_expression; } } - private List arguments; + private Collection arguments; ///SentMessageClasses the first word token of sent messages /// optimise the parsing process when sending messages /// - public List Arguments + public Collection Arguments { get { @@ -66,12 +69,35 @@ namespace IvyBus } public event EventHandler Callback; - + public IvyApplicationBinding() + :this(BindingType.RegularExpression,string.Empty,null) { - arguments = new List(); + + } + public IvyApplicationBinding(BindingType type, string expression, params object[] args) + { + arguments = new Collection(); + this.Binding = type; + this.Expression = expression; + this.args = args; } + private string ReplaceArgs(Match m) + { + string s = string.Empty; + // Get the matched string. convert to argindex + int argindex = int.Parse(m.Groups[1].Value); + // use %... to specify formating + object arg = args[argindex]; + IFormattable fmt = arg as IFormattable; + if (fmt != null) + s = fmt.ToString(m.Groups[3].Value, null); + else if (arg != null) + s = arg.ToString(); + + return s; + } // translate part of expression to object property public void FormatExpression() { @@ -94,11 +120,12 @@ namespace IvyBus args[i] = arguments[i]; } } - formated_expression = string.Format(expression, args); + + formatted_expression = re.Replace(expression, ReplaceArgs); } else //TODO Abnormal condition Design Time #endif - formated_expression = expression; + formatted_expression = expression; } -- cgit v1.1