public static List <string> Preprocess(EngineState s, IEnumerable <string> strs, bool escapePercent = true) { return(Unescape(ExpandVariables(s, strs), escapePercent)); }
/// <summary> /// Expand #1, #2, #3, etc... /// </summary> /// <param name="str"></param> /// <returns></returns> public static string ExpandSectionParams(EngineState s, string str) { // Expand #1 into its value Regex regex = new Regex(@"(?<!#)(#[0-9]+)", RegexOptions.Compiled); MatchCollection matches = regex.Matches(str); while (0 < matches.Count) { StringBuilder b = new StringBuilder(); for (int x = 0; x < matches.Count; x++) { string pIdxStr = matches[x].Groups[1].ToString().Substring(1); if (!int.TryParse(pIdxStr, NumberStyles.Integer, CultureInfo.InvariantCulture, out int pIdx)) { throw new InternalException("ExpandVariables failure"); } if (x == 0) { b.Append(str.Substring(0, matches[0].Index)); } else { int startOffset = matches[x - 1].Index + matches[x - 1].Value.Length; int endOffset = matches[x].Index - startOffset; b.Append(str.Substring(startOffset, endOffset)); } string param; if (s.CurSectionParams.ContainsKey(pIdx)) { param = s.CurSectionParams[pIdx]; } else { if (s.CurDepth == 1) // Dirty Hack for WB082 compatibility { param = $"##{pIdx}"; // [Process] -> Should return #{pIdx} even it was not found } else { param = string.Empty; // Not in entry section -> return string.Empty; } } b.Append(param); if (x + 1 == matches.Count) // Last iteration { b.Append(str.Substring(matches[x].Index + matches[x].Value.Length)); } } str = b.ToString(); matches = regex.Matches(str); } // Escape #a (Current Argument Count) if (str.IndexOf("#a", StringComparison.Ordinal) != -1) { str = StringHelper.ReplaceEx(str, "#a", s.CurSectionParamsCount.ToString(), StringComparison.Ordinal); } // Escape #r (Return Value) if (str.IndexOf("#r", StringComparison.Ordinal) != -1) { str = StringHelper.ReplaceEx(str, "#r", s.SectionReturnValue, StringComparison.Ordinal); } // Escape #c (Loop Counter) if (s.LoopRunning) { str = StringHelper.ReplaceEx(str, "#c", s.LoopCounter.ToString(), StringComparison.Ordinal); } return(str); }
public static string Preprocess(EngineState s, string str, bool escapePercent = true) { return(Unescape(ExpandVariables(s, str), escapePercent)); }
/// <summary> /// Expand #n and %Var% variables. /// </summary> /// <param name="s"></param> /// <param name="str"></param> /// <returns></returns> public static string ExpandVariables(EngineState s, string str) { return(s.Variables.Expand(ExpandSectionParams(s, str))); }