Improve replacing logic to not replace with result of previous replace

This commit is contained in:
Jaex 2018-02-02 03:38:26 +03:00
parent c7f9f3c4f7
commit e83726176f
2 changed files with 39 additions and 2 deletions

View file

@ -167,6 +167,39 @@ public static string ReplaceAll(this string text, string search, Func<string> re
return text;
}
public static string BatchReplace(this string text, Dictionary<string, string> replace)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
string current = text.Substring(i);
bool replaced = false;
foreach (KeyValuePair<string, string> entry in replace)
{
if (current.StartsWith(entry.Key))
{
if (!string.IsNullOrEmpty(entry.Value))
{
sb.Append(entry.Value);
}
i += entry.Key.Length - 1;
replaced = true;
continue;
}
}
if (!replaced)
{
sb.Append(text[i]);
}
}
return sb.ToString();
}
public static string RemoveWhiteSpaces(this string str)
{
return new string(str.Where(c => !char.IsWhiteSpace(c)).ToArray());

View file

@ -162,8 +162,12 @@ public Dictionary<string, string> GetArguments(string filename = "", string inpu
string value = arg.Value;
value = NameParser.Parse(NameParserType.Text, value);
value = value.Replace("$filename$", filename);
value = value.Replace("$input$", input);
value = value.BatchReplace(new Dictionary<string, string>()
{
{ "$filename$", filename },
{ "$input$", input }
});
arguments.Add(arg.Key, value);
}