1. Mike Podonyi
  2. TemplateStrings

Commits

mpodonyi  committed cd7be57

implement callback overload

  • Participants
  • Parent commits c9894b6
  • Branches default

Comments (0)

Files changed (1)

File TemplateStrings/TemplateString.cs

View file
 
 //((?<escapedend>}})  |  (?<end>}))
 
-        private string GetValueFromDictionary(IDictionary<string, string> mapping, string value)
+        private string GetValueFromDictionary(IDictionary<string, string> mapping, Func<string,string> callback, string value)
         {
-            return mapping[value];
+			return mapping!=null 
+							? mapping[value]
+							:  callback(value);
+        }
+		
+		public string Substitute(IDictionary<string, string> mapping, bool throwNoException)
+        {
+            return Substitute(mapping,null, throwNoException);
         }
 		
 		public string Substitute(IDictionary<string, string> mapping)
             return Substitute(mapping, false);
         }
 
-        public string Substitute(IDictionary<string, string> mapping, bool throwNoException)
+		public string Substitute(Func<string,string> callback)
+        {
+            return Substitute(null,callback,false);
+        }
+
+        private string Substitute(IDictionary<string, string> mapping, Func<string,string> callback, bool throwNoException)
         {
             Validate();
 
 																	  templateValue.EndDelimiter;
 														   else if (match.Groups["end"].Success)
 															   return templateValue.StartDelimiter +
-																	  GetValueFromDictionary(mapping,
+																	  GetValueFromDictionary(mapping,callback,
 																							 match.Groups["named"].Value);
 													   }
 
 															   return templateValue.StartDelimiter + match.Groups["named"] +
 																	  templateValue.EndDelimiter;
 														   else if (match.Groups["start"].Success)
-															   return GetValueFromDictionary(mapping,
+															   return GetValueFromDictionary(mapping,callback,
 																							 match.Groups["named"].Value) +
 																	  templateValue.EndDelimiter;
 													   }
 													   {
 														   if (string.IsNullOrEmpty(templateValue.EndDelimiter) ||
 															   match.Groups["end"].Success)
-															   return GetValueFromDictionary(mapping,
+															   return GetValueFromDictionary(mapping,callback,
 																							 match.Groups["named"].Value);
 														   else if (match.Groups["escapedend"].Success)
 															   return
-																   GetValueFromDictionary(mapping,
+																   GetValueFromDictionary(mapping,callback,
 																						  match.Groups["named"].Value) +
 																   templateValue.EndDelimiter;
 													   }
 													   {
 														   if (string.IsNullOrEmpty(templateValue.StartDelimiter) ||
 															   match.Groups["start"].Success)
-															   return GetValueFromDictionary(mapping,
+															   return GetValueFromDictionary(mapping,callback,
 																							 match.Groups["named"].Value);
 														   else if (match.Groups["escapedstart"].Success)
 															   return templateValue.StartDelimiter +
-																	  GetValueFromDictionary(mapping,
+																	  GetValueFromDictionary(mapping,callback,
 																							 match.Groups["named"].Value);
 													   }