Exemple #1
0
        public string Parse(string inputText)
        {
            // replacement dictionary -->
            // key: substring to replace
            // value: new string
            var stringReplDic = new Dictionary <string, string>();

            foreach (string funcName in this.Keys)
            {
                // get full matches, e.g. {MyFunc('arg1')}
                string[] funcStrings = GetFunctionStrings(inputText, funcName);
                foreach (string funcString in funcStrings)
                {
                    if (string.IsNullOrEmpty(funcString))
                    {
                        continue;
                    }

                    // warning: the delegate function is called once per unique function string
                    if (stringReplDic.ContainsKey(funcString))
                    {
                        continue;
                    }

                    var delArgs = new FunctionParseDelegateArgs();
                    delArgs.FunctionName = funcName;
                    delArgs.FunctionArgs = GetFunctionArgs(funcString);

                    string funcValue = Convert.ToString(this[funcName](delArgs));
                    stringReplDic.Add(funcString, funcValue);
                }
            }
            return(StringHelpers.ReplaceText(inputText, stringReplDic));
        }
Exemple #2
0
        // returns string with '?' instead of the function value
        public ParsedCriteria ParseToCriteria(string inputText)
        {
            var funcValues            = new List <object>();
            var distinctFuncStringDic = new Dictionary <string, object>();

            foreach (string funcName in this.Keys)
            {
                // get full matches of function strings, e.g. {MyFunc('arg1')}
                string[] funcStrings = GetFunctionStrings(inputText, funcName);

                foreach (string funcString in funcStrings)
                {
                    if (string.IsNullOrEmpty(funcString))
                    {
                        continue;
                    }

                    var delArgs = new FunctionParseDelegateArgs();
                    delArgs.FunctionName = funcName;
                    delArgs.FunctionArgs = GetFunctionArgs(funcString);

                    // get function value either from delegate or from cached dictionary
                    object funcValue = null;
                    if (!distinctFuncStringDic.TryGetValue(funcString, out funcValue))
                    {
                        distinctFuncStringDic.Add(funcString, this[funcName](delArgs));
                    }
                    funcValue = distinctFuncStringDic[funcString];

                    inputText = inputText.Replace(funcString, "?");
                    funcValues.Add(funcValue);
                }
            }
            var result = new ParsedCriteria();

            result.Criteria   = inputText;
            result.Parameters = funcValues.ToArray();
            return(result);
        }