Below are three methods I have written for my RichTextBox derived class, which is a syntax highlighter. There is a shared copy, lcpy_strLine, of the current line of the richtextbox in all upper case. What these methods do is;
-
ColorInsideTwoChars colors the characters between the two specified characters the specified color. Ex.
ColorInsideTwoChar("(", ")", Color.Green)would color all the characters between two parenthesis green for all sets of parenthesis in the current line -
ColorTilNoNumFromChar colors, from the specified character, all of the characters that are numbers, Ex.
ColorTilNoNumFromChar("G", Color.Red)would color all of the numbers after a G is encountered, red (this includes the G) -
ColorCharIfNotFollowedByLetter colors the specified character if it is not followed by a letter. Ex.
ColorCharIfNotFollowedByLetter("x", Color.Orange)would color all the X’s orange that are not followed by a letter
My question is, is there a faster way to do these methods. They look ugly and I think that there are definitely simpler and more aesthetic ways of doing these methods. Any suggestions? I ask because these methods are ran on each line of few thousand line files and are pretty slow. I need to speed them up. I could try and rewrite each of them a different way a few times, or I could have a few smarties try and head me in the right direction.
private void ColorInsideTwoChars(String car1, String car2, Color clr)
{
int indx1 = 0;
int indx2 = 0;
while ((indx1 = lcpy_strLine.IndexOf(car1, indx1)) != -1
&& (indx2 = lcpy_strLine.IndexOf(car2, indx2)) != -1
&& indx1 < indx2)
{
SelectionStart = m_nLineStart + indx1;
SelectionLength = (indx2 - indx1) + 1;
SelectionColor = clr;
indx1 = ++indx2;
}
}
private void ColorTilNoNumFromChar(String car, Color clr)
{
int indx1 = 0;
while ((indx1 = lcpy_strLine.IndexOf(car, indx1)) != -1)
{
int j = 0;
for (j = indx1 + 1; j < m_nLineLength; j++)
{
if (!Char.IsDigit(lcpy_strLine[j]))
break;
}
SelectionStart = m_nLineStart + indx1;
SelectionLength = j - indx1;
SelectionColor = clr;
indx1 = j;
}
}
private void ColorCharIfNotFollowedByLetter(String car, Color clr)
{
int indx1 = 0;
while ((indx1 = lcpy_strLine.IndexOf(car, indx1)) != -1 &&
indx1 + 1 < m_nLineLength)
{
SelectionStart = m_nLineStart + indx1;
SelectionLength = 1;
if (!Char.IsLetter(lcpy_strLine[lcpy_strLine.IndexOf(car) + 1]))
SelectionColor = clr;
else
SelectionColor = Color.Black;
++indx1;
}
}
As for
ColorInsideTwoChar(), note that you’ll color what’s in between parentheses even if they are nested, resulting in doing it more that once. What if parentheses (or these chars in general) aren’t matched properly? Maybe find opening char, then mathing it closing char and color everything in between? Then start searching from position at which you had closing char, and look for the next opening one.. Also, I would call this only on line change, don’t know what’s your idea 😉 And maybe only on visible part of RichBox first, and then color the rest in the background?i would swap for
although it’s probably more or less the same…
But I think you should wait for some more professional answer, there’s probably some more elegant way to do this.
Also, maybe finding all occurences of wanted chars at the beginning of methods, and putting them into an array, would be faster than looking them up again and again?
It might be also faster to do that for larger chunks of file than single lines…