Help with a search & replace
-
I have a text box & a property window. The text box is read-only and displays C code. As the users change properties the C-code changes respectively. I've implemented this using Regex's replace function. However it is really really slow. Here's what I have
private void updateCCodeTextBoxWorker(int address, string name) { cCodeTextBox.SuspendLayout(); string pattern = name + @", 0x.."; string replace = String.Format("{0}, 0x{1:X2}", name, regData[address]); Regex rgx = new Regex(pattern); cCodeTextBox.Text = rgx.Replace(cCodeTextBox.Text, replace); cCodeTextBox.ResumeLayout(); }
The string "name" is basically what I'm searching for and "regData[address]" is the value that's changing. Is there a better way to do this? How can I make it faster? Thanks, --RB
-
I have a text box & a property window. The text box is read-only and displays C code. As the users change properties the C-code changes respectively. I've implemented this using Regex's replace function. However it is really really slow. Here's what I have
private void updateCCodeTextBoxWorker(int address, string name) { cCodeTextBox.SuspendLayout(); string pattern = name + @", 0x.."; string replace = String.Format("{0}, 0x{1:X2}", name, regData[address]); Regex rgx = new Regex(pattern); cCodeTextBox.Text = rgx.Replace(cCodeTextBox.Text, replace); cCodeTextBox.ResumeLayout(); }
The string "name" is basically what I'm searching for and "regData[address]" is the value that's changing. Is there a better way to do this? How can I make it faster? Thanks, --RB
-
I have a text box & a property window. The text box is read-only and displays C code. As the users change properties the C-code changes respectively. I've implemented this using Regex's replace function. However it is really really slow. Here's what I have
private void updateCCodeTextBoxWorker(int address, string name) { cCodeTextBox.SuspendLayout(); string pattern = name + @", 0x.."; string replace = String.Format("{0}, 0x{1:X2}", name, regData[address]); Regex rgx = new Regex(pattern); cCodeTextBox.Text = rgx.Replace(cCodeTextBox.Text, replace); cCodeTextBox.ResumeLayout(); }
The string "name" is basically what I'm searching for and "regData[address]" is the value that's changing. Is there a better way to do this? How can I make it faster? Thanks, --RB
Hi, So for every property change, the code executes, creates a new Regex and makes it Replace something. Regex is an expensive class by itself, it has to either interpret your intentions, or, optionally generate compiled code for it (more cost effective only when required over and over, not your case). If the user types 5 characters for a property, it changes five times. I would look for a way to run the code less frequently. Some ideas: - let the user trigger the code by clicking a button or so (less comfortable); - let a timer trigger the code; maybe have a scheme where the worker runs say 3 seconds after the last change (i.e. every change starts/restarts a timer, and only when the 3 seconds elapse is the code executed). Hope this helps :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }