Kinman
2004-11-30, 22:07:31
Hi, ich schreibe gerade einen PHP Editor. Natürlich möchte ich Syntaxhighlighting unterstützen. Ich hab auch schon passenden Code entwickelt. Jedoch wird er bei größeren Files ziehmlich langsam. Was könnte man hier optimieren?
searchWord und wordColor sind Arrays in dem die zu färbenden Worte & die Farben drinstehen.
Public Sub colorizeTags(ByRef rtfField As RichTextBox)
On Error Resume Next
Dim oldPos, tmpPos, i As Integer
Dim tmpStr As String
oldPos = rtfField.SelStart
tmpPos = 0
tmpStr = ""
rtfField.Visible = False
rtfField.SelStart = 0
rtfField.SelLength = Len(rtfField.Text)
rtfField.SelColor = stdColor
For i = 0 To UBound(searchWord) Step 1
searchWord(i) = Trim$(searchWord(i))
tmpPos = 0
If searchWord(i) <> "" Then
While rtfField.Find(searchWord(i), tmpPos) <> -1
tmpStr = Left$(rtfField.Text, rtfField.SelStart)
If CountSigns(tmpStr, Chr(34)) Mod 2 = 0 Then '34 --> "
If CountSigns(rtfField.Text, Chr(39)) Mod 2 = 0 Then '39 --> '
rtfField.SelColor = wordColor(i)
End If
End If
tmpPos = rtfField.SelStart
rtfField.SelStart = rtfField.SelStart + rtfField.SelLength
rtfField.SelColor = stdColor
tmpPos = tmpPos + 1
Wend
End If
Next i
rtfField.Visible = True
rtfField.SetFocus
rtfField.SelStart = rtfField.SelStart + rtfField.SelLength
rtfField.SelColor = stdColor
rtfField.SelStart = oldPos
rtfField.SelLength = 0
End Sub
Und hier die Funktion CountSigns:
Public Function CountSigns(ByVal sText As String, ByVal sFindThis As String, Optional ByVal bCaseSensitiv As Boolean = False) As Long
Dim nSigns As Long
Dim nSearchLen As Long
If Len(sFindThis) = 0 Then CountSigns = -1: Exit Function
nSigns = Len(sText)
nSearchLen = Len(sFindThis)
If bCaseSensitiv Then
sText = Replace(sText, sFindThis, "")
Else
sText = Replace(LCase$(sText), LCase$(sFindThis), "")
End If
CountSigns = (nSigns - Len(sText)) / nSearchLen
End Function
Ich bin für jegliche Hilfe sehr dankbar.
mfg Kinman
searchWord und wordColor sind Arrays in dem die zu färbenden Worte & die Farben drinstehen.
Public Sub colorizeTags(ByRef rtfField As RichTextBox)
On Error Resume Next
Dim oldPos, tmpPos, i As Integer
Dim tmpStr As String
oldPos = rtfField.SelStart
tmpPos = 0
tmpStr = ""
rtfField.Visible = False
rtfField.SelStart = 0
rtfField.SelLength = Len(rtfField.Text)
rtfField.SelColor = stdColor
For i = 0 To UBound(searchWord) Step 1
searchWord(i) = Trim$(searchWord(i))
tmpPos = 0
If searchWord(i) <> "" Then
While rtfField.Find(searchWord(i), tmpPos) <> -1
tmpStr = Left$(rtfField.Text, rtfField.SelStart)
If CountSigns(tmpStr, Chr(34)) Mod 2 = 0 Then '34 --> "
If CountSigns(rtfField.Text, Chr(39)) Mod 2 = 0 Then '39 --> '
rtfField.SelColor = wordColor(i)
End If
End If
tmpPos = rtfField.SelStart
rtfField.SelStart = rtfField.SelStart + rtfField.SelLength
rtfField.SelColor = stdColor
tmpPos = tmpPos + 1
Wend
End If
Next i
rtfField.Visible = True
rtfField.SetFocus
rtfField.SelStart = rtfField.SelStart + rtfField.SelLength
rtfField.SelColor = stdColor
rtfField.SelStart = oldPos
rtfField.SelLength = 0
End Sub
Und hier die Funktion CountSigns:
Public Function CountSigns(ByVal sText As String, ByVal sFindThis As String, Optional ByVal bCaseSensitiv As Boolean = False) As Long
Dim nSigns As Long
Dim nSearchLen As Long
If Len(sFindThis) = 0 Then CountSigns = -1: Exit Function
nSigns = Len(sText)
nSearchLen = Len(sFindThis)
If bCaseSensitiv Then
sText = Replace(sText, sFindThis, "")
Else
sText = Replace(LCase$(sText), LCase$(sFindThis), "")
End If
CountSigns = (nSigns - Len(sText)) / nSearchLen
End Function
Ich bin für jegliche Hilfe sehr dankbar.
mfg Kinman