Commits

mpodonyi  committed 8e964de

second trial

  • Participants
  • Parent commits 54f4bf1

Comments (0)

Files changed (1)

File TemplateStrings/Template.cs

 {
     public class TemplateValue
     {
-        public string StartDelimiter{ get; set; }
+        private string _StartDelimiter;
+        public string StartDelimiter
+        {
+            get
+            {
+                return _StartDelimiter;
+            }
+            set
+            {
+                _StartDelimiter = value;
+            }
+        }
 
-        public string StartDelimiterEscape { get; set; }
-        public string EndDelimiterEscape { get; set; }
+        private string _StartDelimiterEscape;
+        public string StartDelimiterEscape
+        {
+            get
+            {
+                return _StartDelimiterEscape;
+            }
+            set
+            {
+                _StartDelimiterEscape = value;
+            }
+        }
 
-        public string EndDelimiter { get; set; }
+        private string _EndDelimiterEscape;
+        public string EndDelimiterEscape
+        {
+            get
+            {
+                return _EndDelimiterEscape;
+            }
+            set
+            {
+                _EndDelimiterEscape = value;
+            }
+        }
 
-        public string IdPattern { get; set; }   
+        private string _EndDelimiter;
+        public string EndDelimiter
+        {
+            get
+            {
+                return _EndDelimiter;
+            }
+            set
+            {
+                _EndDelimiter = value;
+            }
+        }
+
+        private string _IdPattern;
+        public string IdPattern
+        {
+            get
+            {
+                return _IdPattern;
+            }
+            set
+            {
+                _IdPattern = value;
+            }
+        }
+
 
     }
 
-    public class TemplateValueCollection: Collection<TemplateValue>
-    {}
+    public class TemplateValueCollection : Collection<TemplateValue>
+    { }
 
-    public class Template
+    public abstract class Template
     {
 
-        protected virtual TemplateValueCollection TemplateValues
+        protected abstract TemplateValueCollection TemplateValues
+        { get;}
+
+        private string _template;
+        public string template
         {
             get
             {
-                TemplateValueCollection templateValues = new TemplateValueCollection();
-
-                templateValues.Add(new TemplateValue { StartDelimiter = "$", IdPattern = "[_a-z][_a-z0-9]*", StartDelimiterEscape="$$" });
-                templateValues.Add(new TemplateValue { StartDelimiter = "${", IdPattern = "[_a-z][_a-z0-9]*", EndDelimiter = "}",StartDelimiterEscape = "$${",EndDelimiterEscape = "}}"});
-
-                return templateValues;
+                return _template;
             }
-        }
-
-        protected virtual string delimiter 
-        {
-            get
+            set
             {
-                return "$";
+                _template = value;
             }
         }
 
-        protected virtual string idpattern 
-        {
-            get
-            {
-                return "[_a-z][_a-z0-9]*";
-            }
-        }
-
-
-        protected virtual string pattern
-        {
-            get
-            {
-                return @"{0}(?:(?<escaped>{0})|(?<named>{1})|{{(?<braced>{1})}}|(?<invalid>))";
-            }
-        }
-
-        public string template
-        {
-            get; 
-            set;
-        }
-
         public Template(string template)
         {
             this.template = template;
         }
 
+        private void Validate()
+        {
+            foreach (TemplateValue val in TemplateValues)
+            {
+                if (!string.IsNullOrEmpty(val.EndDelimiter) && string.IsNullOrEmpty(val.StartDelimiter))
+                    throw new ArgumentException("When an EndDelimiter is used you must set a StartDelimiter as well.");
+
+                if (!string.IsNullOrEmpty(val.StartDelimiterEscape) && string.IsNullOrEmpty(val.StartDelimiter))
+                    throw new ArgumentException("When an StartDelimiterEscape is used you must use set a StartDelimiter as well.");
+
+                if (!string.IsNullOrEmpty(val.EndDelimiterEscape) && string.IsNullOrEmpty(val.EndDelimiter))
+                    throw new ArgumentException("When an EndDelimiterEscape is used you must use set a EndDelimiter as well.");
+
+                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)
         {
             return Substitute(mapping, false);
         }
 
-        public string Substitute(IDictionary<string,string> mapping,bool throwNoException)
+        public string GetRegex()
         {
-            var pat = string.Format(pattern, Regex.Escape(delimiter),idpattern);
+            StringBuilder builder = new StringBuilder(200);
 
-            Regex regex = new Regex(pat);
 
+            for (int i = 0; i < TemplateValues.Count; i++)
+            {
+                TemplateValue value = TemplateValues[i];
 
-            regex.Replace(this.template, match =>
+                builder.AppendFormat("(?<{0}>", i + 1);
+
+                bool hasval = false;
+                if (!string.IsNullOrEmpty(value.EndDelimiterEscape))  //means has also start delimiter
+                {
+                    //builder.AppendFormat("(?<escapedend>{0})", Regex.Escape(value.EndDelimiterEscape));
+                    builder.AppendFormat("(?<escapedstart>{0})(?<named>{1})(?<escapedend>{2})", value.StartDelimiterEscape != null ? Regex.Escape(value.StartDelimiterEscape) : string.Empty, value.IdPattern, Regex.Escape(value.EndDelimiterEscape));
+
+                    hasval = true;
+                }
+
+                if (!string.IsNullOrEmpty(value.StartDelimiterEscape))
+                {
+                    if (hasval)
+                        builder.Append('|');
+
+                    //devide here if end delimier exist or not
+
+                    if (string.IsNullOrEmpty(value.EndDelimiter))
+                    {
+                        builder.AppendFormat("(?<escapedstart>{0}){1}{2}", Regex.Escape(value.StartDelimiterEscape), value.IdPattern, Regex.Escape(value.EndDelimiter));
+                    
+                    }
+                    else
+                    {
+                        builder.AppendFormat("(?<escapedstart>{0}){1}", Regex.Escape(value.StartDelimiterEscape), value.IdPattern, Regex.Escape(value.EndDelimiter));
+                                            
+                    }
+
+
+                    
+                    hasval = true;
+                }
+
+              
+                
+                
+                
+                if (!string.IsNullOrEmpty(value.IdPattern))
+                {
+                    if (hasval)
+                        builder.Append('|');
+
+                    builder.AppendFormat("{0}(?<named>{1}){2}", value.StartDelimiter != null ? Regex.Escape(value.StartDelimiter) : string.Empty, value.IdPattern, value.EndDelimiter != null ? Regex.Escape(value.EndDelimiter) : string.Empty);
+                    hasval = true;
+                }
+
+                builder.Append(")|");
+            }
+
+            builder.Length = builder.Length - 1;
+
+            return builder.ToString();
+        }
+
+        public static string[,] PythonTestInternal={{"Lorem $ipsum dolor sit amet.","Lorem test1 dolor sit amet."},
+                                  {"Lorem ${ipsum} dolor sit amet.","Lorem test1 dolor sit amet."},
+                                  {"Lorem $$ipsum dolor sit amet.","Lorem $ipsum dolor sit amet."},  //2
+                                  {"Lorem $${ipsum} dolor sit amet.","Lorem ${ipsum} dolor sit amet."}};  //2
+
+
+        public static string[,] MvcTestInternal={{"Lorem {ipsum} dolor sit amet.","Lorem test1 dolor sit amet."},
+                                  {"Lorem {{ipsum}} dolor sit amet.","Lorem {ipsum} dolor sit amet."},   //1
+                                  {"Lorem {ipsum}} dolor sit amet.","Lorem test1} dolor sit amet."},
+                                  {"Lorem {{ipsum} dolor sit amet.","Lorem {test1 dolor sit amet."},
+                                  {"Lorem {ipsum dolor sit amet.","Lorem {ipsum dolor sit amet."},
+                                  {"Lorem ipsum} dolor sit amet.","Lorem ipsum} dolor sit amet."},
+                                  {"Lorem {{ipsum dolor sit amet.","Lorem {{ipsum dolor sit amet."},
+                                  {"Lorem ipsum}} dolor sit amet.","Lorem ipsum}} dolor sit amet."}};
+
+       
+
+
+        public string Substitute(IDictionary<string, string> mapping, bool throwNoException)
+        {
+            Validate();
+            Regex regex = new Regex(GetRegex());
+
+            regex.Replace(this.template, delegate(Match match)
                                              {
                                                  string val = match.Groups["named"].Value;
-                                                 if(!string.IsNullOrEmpty(val))
+                                                 if (!string.IsNullOrEmpty(val))
                                                  {
                                                      try
                                                      {
                                                              throw;
                                                          }
                                                      }
-                                                     
+
 
 
 
                                                  }
 
-                                                 val = match.Groups["braced"].Value;
-                                                 if (!string.IsNullOrEmpty(val))
-                                                 {
-
-
-
-                                                 }
+                                               
 
                                                  val = match.Groups["escaped"].Value;
                                                  if (!string.IsNullOrEmpty(val))
                                                  {
-                                                     return this.delimiter;
+                                                     //   return this.delimiter;
                                                  }
 
-                                                 val = match.Groups["invalid"].Value;
-                                                 if (!string.IsNullOrEmpty(val))
-                                                 {
-                                                     //test this
-                                                 }
+                                                 //val = match.Groups["invalid"].Value;
+                                                 //if (!string.IsNullOrEmpty(val))
+                                                 //{
+                                                 //    //test this
+                                                 //}
 
 
                                                  throw new ApplicationException("Unrecognized named group in pattern");
                                              });
 
             return "";
-            
+
 
         }
     }
+
+    public class TemplateStringsPython : Template
+    {
+        public TemplateStringsPython(string template)
+            : base(template)
+        { }
+
+        protected override TemplateValueCollection TemplateValues
+        {
+            get
+            {
+                TemplateValueCollection templateValues = new TemplateValueCollection();
+
+                TemplateValue value1 = new TemplateValue();
+                value1.StartDelimiter = "$";
+                value1.IdPattern = "[_a-z][_a-z0-9]*";
+                value1.StartDelimiterEscape = "$$";
+
+                TemplateValue value2 = new TemplateValue();
+                value2.StartDelimiter = "${";
+                value2.IdPattern = "[_a-z][_a-z0-9]*";
+                value2.StartDelimiterEscape = "$${";
+                value2.EndDelimiter = "}";
+
+                templateValues.Add(value1);
+                templateValues.Add(value2);
+
+                return templateValues;
+            }
+        }
+    }
+
+    public class TemplateStringsMvc : Template
+    {
+        public TemplateStringsMvc(string template)
+            : base(template)
+        { }
+
+        protected override TemplateValueCollection TemplateValues
+        {
+            get
+            {
+                TemplateValueCollection templateValues = new TemplateValueCollection();
+
+
+                TemplateValue value2 = new TemplateValue();
+                value2.StartDelimiter = "{";
+                value2.StartDelimiterEscape = "{{";
+                value2.IdPattern = "[_a-z][_a-z0-9]*";
+                value2.EndDelimiter = "}";
+                value2.EndDelimiterEscape = "}}";
+
+                templateValues.Add(value2);
+
+                return templateValues;
+            }
+        }
+    }
 }