I am using C#. NET 2.0 and WinForms.
I have a piece of code which is formatted in Assembly like this:
VARIABLE = 40
ADDRESS = $60
LDA VARIABLE;
STA ADDRESS;
Output should be:
!VARIABLE = 40
!ADDRESS = $60
LDA !VARIABLE;
STA !ADDRESS;
Of course, there is much more than that. Like 2000 lines, but the point is there are declarations at the top of the file and I can load/save or do whatever with them. But my problem is, I have to “prepend” ALL of these declarations (even in raw code) with a !.
My current method is this:
var defs = tab.tb.GetRanges(@"^\s*([A-Za-z0-9_]+){4,255}\s*=", RegexOptions.Multiline); // all declarations are formatted like this, so use regex to get all of them
foreach (var def in defs)
{
string actual = def.Text.Substring(0, def.Text.IndexOf(' ')); // remove the = sign since its not used in actual code
txt = txt.Replace(actual, "!" + actual);
}
However, this method is very slow. It takes approximately 3 seconds to “fix up” all of the declarations in my file. Is there any better way? And for the record, the syntax is slightly different from a normal text box because I am using http://www.codeproject.com/KB/edit/FastColoredTextBox_.aspx as my text control.
You nest quantifiers in your regex!
Take a simple string as ‘aaaaaaaa’: what is the regex engine supposed to capture? ‘a’ 8 times, ‘aa’ 4 times, ‘aa’, then ‘a’, then ‘a’, then… ?
This is very probably the cause of your performance problems. Just don’t do that! All the more that you try to match in the whole file. Even though the regex engine will eventually choose the leftmost, longest match, it tries all possibilities, always.
Remove the
+!