1. Mike Podonyi
  2. TemplateStrings

Commits

mpodonyi  committed 2909d93

Buffer Regex; add "CompiledRegex" Argument

  • Participants
  • Parent commits 4faa4cf
  • Branches default

Comments (0)

Files changed (1)

File TemplateStrings/TemplateString.cs

View file
  • Ignore whitespace
 
     public abstract class TemplateString
     {
-        protected TemplateString(string template)
+        protected TemplateString(string template):this(template,false)
+        { }
+		
+		protected TemplateString(string template, bool compileRegex)
         {
             Template = template;
-        }
+			CompileRegex=compileRegex;
+		}
+	
+		
+		private Regex GeneratedRegex=null;
+		
+		private bool CompileRegex=false;
 
         protected abstract IList<TemplateValue> TemplateValues { get; }
 
 
                 if (string.IsNullOrEmpty(val.IdPattern))
                     throw new ArgumentException("You must set an IdPattern");
-
-                //escaped end nur wenn escaped start
             }
         }
 
-        public string Substitute(IDictionary<string, string> mapping)
+        private Regex GetRegex()
         {
-            return Substitute(mapping, false);
-        }
-
-        private string GetRegex()
-        {
+			if(GeneratedRegex != null)
+				return GeneratedRegex;
+				
+				
             var builder = new StringBuilder(200);
 
             for (int i = 0; i < TemplateValues.Count; i++)
             }
 
             builder.Length--;
+			
+			RegexOptions regexOptions = RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant;
+			
+			if(CompileRegex)
+				regexOptions | =RegexOptions.Compiled ;
 
-            return builder.ToString();
+            return GeneratedRegex = new Regex(builder.ToString(),regexOptions);
         }
 
 
         {
             return mapping[value];
         }
+		
+		public string Substitute(IDictionary<string, string> mapping)
+        {
+            return Substitute(mapping, false);
+        }
 
         public string Substitute(IDictionary<string, string> mapping, bool throwNoException)
         {
             Validate();
-            var regex = new Regex(GetRegex(),
-                                  RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant);
 
-            return regex.Replace(Template, delegate(Match match)
+            return GetRegex().Replace(Template, delegate(Match match)
                                                {
                                                    TemplateValue templateValue = null;
 
                                                            break;
                                                        }
                                                    }
+												   
+												   //maybe rewrite to get rid of links to templateValue so that only depend on regex
 
                                                    if (match.Groups["escapedstart"].Success)
                                                    {
                                                            return templateValue.StartDelimiter +
                                                                   GetValueFromDictionary(mapping,
                                                                                          match.Groups["named"].Value);
-                                                       //maybe test for second match here
                                                    }
 
                                                    if (match.Groups["escapedend"].Success)
                                                            return GetValueFromDictionary(mapping,
                                                                                          match.Groups["named"].Value) +
                                                                   templateValue.EndDelimiter;
-                                                       //maybe test for second match here
                                                    }
 
                                                    if (match.Groups["start"].Success)