/// <summary> /// Builds up a string regular expression pattern, substituting in the string patterns /// wherever <see cref="RegexString"/> objects are referenced. /// </summary> /// <param name="pattern">The string pattern into which to interpolate.</param> /// <param name="regexVariables">The set of other patterns to use to interpolate, represented /// as a dictionary where the names of the string patterns are the keys and the /// <see cref="RegexString"/> patterns are the values.</param> /// <param name="comparer">The comparer to use for the keys of dictionary.</param> /// <returns>The built-up final string.</returns> public static string Interpolate(string pattern, IDictionary <string, RegexString> regexVariables, IEqualityComparer <string> comparer) { if (comparer == default(IEqualityComparer <string>)) { throw new ArgumentNullException("comparer"); } ValueFetchingDelegate fetcher = delegate(string key, out string value) { RegexString rexVar; bool ret = regexVariables.TryGetValue(key, out rexVar); if (ret) { value = rexVar.GetWrappedValue(); } else { value = default(string); } return(ret); }; if (comparer == default(IEqualityComparer <string>)) { return(Interpolate(pattern, fetcher, EqualityComparer <string> .Default)); } else { return(Interpolate(pattern, fetcher, comparer)); } }
/// <summary> /// Builds up a string regular expression pattern, substituting in the string patterns /// wherever <see cref="RegexString"/> objects are referenced. /// </summary> /// <param name="pattern">The string pattern into which to interpolate.</param> /// <param name="regexVariables">The set of other patterns to use to interpolate, represented /// as a dictionary where the names of the string patterns are the keys and the string /// patterns are the values.</param> /// <param name="comparer">The comparer to use for the keys in <paramref name="regexVariables"/>.</param> /// <returns>The built-up final string.</returns> public static string Interpolate(string pattern, IDictionary <string, string> regexVariables, IEqualityComparer <string> comparer) { ValueFetchingDelegate fetcher = regexVariables.TryGetValue; if (comparer == default(IEqualityComparer <string>)) { comparer = EqualityComparer <string> .Default; } return(Interpolate(pattern, fetcher, comparer)); }
private static string Interpolate(string pattern, ValueFetchingDelegate valDelegate, IEqualityComparer <string> comparer) { var interpolated = new Dictionary <string, string>(comparer); Func <string, string> getInterpolatedResult = default(Func <string, string>); getInterpolatedResult = delegate(string patt) { int[] charPositions; string[] pattPieces = splitIntoPieces(patt, out charPositions); for (int i = 1; i < pattPieces.Length; i += 2) { string outString; string key = pattPieces[i]; if (key == "%%") { pattPieces[i] = "%"; } else if (interpolated.TryGetValue(key, out outString)) { if (outString != default(string)) { pattPieces[i] = outString; } else { throw new ParseException(patt, charPositions[i], String.Format("Invalid self-reference detected in evaluating \"{0}\".", pattPieces[i])); } } else if (valDelegate(key, out outString)) { interpolated.Add(key, default(string)); try { interpolated[key] = pattPieces[i] = getInterpolatedResult(outString); } catch (Exception e) { throw new VariableNameNotFound(patt, charPositions[i], key, e); } } else { throw new VariableNameNotFound(patt, charPositions[i], key); } } return(String.Concat(pattPieces)); }; return(getInterpolatedResult(pattern)); }