Skip to content

Commit 1fd693f

Browse files
committed
Fixed template parsing with several <<>> placeholders, and also when template has both <<>> and normal placeholders and null replacement is passed.
The function may be a bit more complex now, but simpler solutions were 4-5 times slower, which was a considerable amount. In the cases where the previous implementation worked fine, the new one is as fast, or in some benchmarks even faster.
1 parent 2d70cbf commit 1fd693f

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

External/Plugins/ASCompletion/Completion/TemplateUtils.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,34 @@ public static string CallParametersString(MemberModel member)
157157

158158
public static string ReplaceTemplateVariable(string template, string var, string replace)
159159
{
160-
Match m = Regex.Match(template, String.Format(template_variable, var));
161-
if (m.Success)
160+
MatchCollection mc = Regex.Matches(template, String.Format(template_variable, var));
161+
int mcCount = mc.Count;
162+
if (mcCount > 0)
162163
{
163-
if (replace == null)
164+
var sb = new System.Text.StringBuilder();
165+
int pos = 0;
166+
for (int i = 0; i < mcCount; i++)
164167
{
165-
template = template.Substring(0, m.Index) + template.Substring(m.Index + m.Length);
166-
return template;
167-
}
168-
else
169-
{
170-
string val = m.Value;
171-
val = val.Substring(2, val.Length - 4);
172-
template = template.Substring(0, m.Index) + val + template.Substring(m.Index + m.Length);
168+
Match m = mc[i];
169+
int endIndex = m.Index + m.Length;
170+
sb.Append(template.Substring(pos, m.Index - pos));
171+
if (replace != null)
172+
{
173+
string val = m.Value;
174+
val = val.Substring(2, val.Length - 4);
175+
sb.Append(val);
176+
}
177+
if (i == mcCount - 1)
178+
sb.Append(template.Substring(endIndex));
179+
else
180+
{
181+
int next = mc[i + 1].Index;
182+
sb.Append(template.Substring(endIndex, next - endIndex));
183+
pos += next;
184+
}
173185
}
186+
187+
template = sb.ToString();
174188
}
175189
template = template.Replace("$(" + var + ")", replace);
176190
return template;

0 commit comments

Comments
 (0)