Note that there are some explanatory texts on larger screens.

plurals
  1. POString manipulation performance and speed
    text
    copied!<p>Below are three methods I have written for my <code>RichTextBox</code> derived class, which is a syntax highlighter. There is a shared copy, <code>lcpy_strLine</code>, of the current line of the richtextbox in all upper case. What these methods do is; </p> <ul> <li><p><strong>ColorInsideTwoChars</strong> colors the characters between the two specified characters the specified color. Ex. <code>ColorInsideTwoChar("(", ")", Color.Green)</code> would color all the characters between two parenthesis green for all sets of parenthesis in the current line</p></li> <li><p><strong>ColorTilNoNumFromChar</strong> colors, from the specified character, all of the characters that are numbers, Ex. <code>ColorTilNoNumFromChar("G", Color.Red)</code> would color all of the numbers after a G is encountered, red (this includes the G)</p></li> <li><p><strong>ColorCharIfNotFollowedByLetter</strong> colors the specified character if it is not followed by a letter. Ex. <code>ColorCharIfNotFollowedByLetter("x", Color.Orange)</code> would color all the X's orange that are not followed by a letter</p></li> </ul> <p>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.</p> <pre><code> private void ColorInsideTwoChars(String car1, String car2, Color clr) { int indx1 = 0; int indx2 = 0; while ((indx1 = lcpy_strLine.IndexOf(car1, indx1)) != -1 &amp;&amp; (indx2 = lcpy_strLine.IndexOf(car2, indx2)) != -1 &amp;&amp; indx1 &lt; 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 &lt; 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 &amp;&amp; indx1 + 1 &lt; 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload