Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27235

Looping through string one character at a time slow.

$
0
0
Hi all,

I'm creating an HTML syntax highlighter, and it's working for the most part. The main problem is, it's extremely slow. I mean, if you open the source code of a site like Wikipedia, the window takes, literally, a few minutes to appear. It's obviously because I'm looping through the string one character at a time, but I don't know of any alternative methods.

Code:


    Private Sub writeSourceCode(ByVal source As String)
        For Each character As Char In source.ToCharArray()
            If openedQuote = False And character = " " Then
                hasReachedSpace = True
                RichTextBox1.SelectionColor = Color.Black
                RichTextBox1.AppendText(character)
            ElseIf openedQuote = True And character = """" Then
                openedQuote = False
                RichTextBox1.AppendText(character)
                RichTextBox1.SelectionColor = Color.Black
            ElseIf openedQuote = False And hasReachedSpace = True And inTagMode = True And character = """" Then
                openedQuote = True
                RichTextBox1.SelectionColor = Color.Blue
                RichTextBox1.AppendText(character)
            ElseIf inTagMode = True And hasReachedSpace = False And character <> "<" And character <> ">" Then
                RichTextBox1.SelectionColor = Color.Purple
                RichTextBox1.AppendText(character)
            ElseIf inTagMode = True And character = ">" Then
                inTagMode = False
                hasReachedSpace = False
                RichTextBox1.SelectionColor = Color.Black
                RichTextBox1.AppendText(character)
            ElseIf inTagMode = False And character = "<" Then
                inTagMode = True
                hasReachedSpace = False
                RichTextBox1.SelectionColor = Color.Black
                RichTextBox1.AppendText(character)
            Else
                If inCharMode = False And character = "&" Then
                    inCharMode = True
                    lastSavedColour = RichTextBox1.SelectionColor
                    RichTextBox1.SelectionColor = Color.Red
                    RichTextBox1.AppendText(character)
                ElseIf inCharMode = True And character = ";" Or (character = " " And openedQuote = False) Then
                    inCharMode = False
                    RichTextBox1.AppendText(character)
                    RichTextBox1.SelectionColor = lastSavedColour
                Else
                    RichTextBox1.AppendText(character)
                End If
            End If
        Next
    End Sub

It works, it's just painfully slow:




Viewing all articles
Browse latest Browse all 27235

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>