Parsing Text to Highlight Strings...

I know the problem is simple. can someone point out where I’m doing this wrong?

What am I doing?

parsing Xojo Code. Boldening and Coloring strings… (“asdas”) <–

dim astringstart as string ="""" //find the quotation mark...
  wcount=s.CountFields(astringstart)-1 //count how many separations...
  dim kw_length as integer
  if wcount > 0 then //something exists
    for i as Integer = 1 to wcount //from one to the count of separations
      kw_start=s.InStr(kw_laststart,astringstart)-1 //find the first one
      kw_end=s.InStr(kw_start+2,astringstart) //find the second // end of string
      kw_length=kw_end-kw_start //grab the length from the first to the last...
      
      xt.Bold(kw_start,kw_length)=True //bold it
      xt.TextColor(kw_start,kw_length)=RGB(217,109,0) //color it
      
      kw_laststart=kw_end+1 //now get ready to run through the "for" again, starting the search after the second iteration.
      wmain.zcode.StyledText=xt //update the text while we do that...  
    next // next...
  end
end

First one works every time.
5th one works every time

everything between 2-4 gets bold and color.

so uhh… brain says divide by 4, but only in… nvm.

brains fried.

help. lol

Just a thought: wouldn’t it be easier to us RegEx for this? The following code using the respective MBS plugin works just fine for making everything between quotation marks in the textarea bold:

Var regExPattern As String
Var r as new RegExMBS
r.CompileOptionMultiline = true
r.CompileOptionUngreedy = false

regExPattern = "([" + chr(34) +"'])(?:(?=(\\\\?))\\2.)*?\\1"

if r.Compile(regExPattern) Then
  Var start As Integer = 0
  while r.Execute(TextArea1.Value, start)>0
    Var p as Integer = r.OffsetCharacters(0)
    Var l as Integer = r.OffsetCharacters(1)-r.OffsetCharacters(0)
    TextArea1.StyledText.Bold(p, l) = True
    start = r.Offset(1)
  Wend
End if

Dim aStringStart As String = Chr(34) // Find the quotation mark…

tried that - won’t compile.

that said… its finding the marks…

perfectly for the first and 5th string

“string” < perfect
“string” < start
“string” < everything between start and end boldens and changes color
“string” < end
“string” < perfect

here is a link to an old realbasic class “SyntaxHighlightEditField” that does what you want. from alex restrepo.
hope this helps.
https://www.cjoint.com/c/ILDkZqjuGPz

This item was deprecated in version 2019r2. Please use String.IndexOf as a replacement. Note that InStr is 1-based and IndexOf is 0-based.

LMAO - I got it. Divide by 2… I have no idea why it took me coming back a couple days later…to see something so simple!

  dim astringstart as string ="""" //we are looking for a single quotation mark
  wcount=s.CountFields(astringstart) //count how many fields there are 1=2 - 2=4 - 3=6
  wcount=wcount*0.5 //multiple by half... (div by half)
  dim kw_length as integer
  if wcount > 0 then
    for i as Integer = 1 to wcount //from 1 to 3
      kw_start=s.InStr(kw_laststart,astringstart)-1 //find the first
      kw_end=s.InStr(kw_start+2,astringstart) //find the last
      kw_length=kw_end-kw_start //whats there...
      xt.Bold(kw_start,kw_length)=True //bolden it
      xt.TextColor(kw_start,kw_length)=RGB(217,109,0) //color it
      kw_laststart=kw_end+1 //set the end place
      wmain.zcode.StyledText=xt // set the text
    next //roll right through...
  end
end

a good help is inserting temporary debug output for all your values with System.DebugLog
or you set a break point and step through.
Chr should be replaced with unicode notation &u

For highlighting purposes I usually take a two-step approach: First I tokenize the String (converted to a MemoryBlock for speed), then I highlight each token, for example:

[code]Var tmp As MemoryBlock = TextArea1.Value
Var lim As Integer = tmp.Size - 1

Var pairs() As Pair
Var i, tmpStart As Integer
Var isToken As Boolean = False
Var cnt As Integer = -1

While i <= lim
cnt = cnt + 1
If tmp.Byte(i) < 128 Then
If tmp.Byte(i) = 34 Then
If Not isToken Then
tmpStart = cnt
Else
pairs.AddRow(New Pair(tmpStart, cnt - tmpStart + 1))
End If
isToken = Not isToken
End If
Else
If tmp.Byte(i) >= 224 Then
i = i + 2
Elseif tmp.Byte(i) >= 192 Then
i = i + 1
End If
End If
i = i + 1
Wend

TextArea1.TextColor = &c000000
Var st As StyledText = TextArea1.StyledText

For Each token As Pair In pairs
st.TextColor(token.Left, token.Right) = &c3399CC
Next[/code]

The lexer is easily expanded (keywords, single quotes etc.) and rather fast; the downside: unfortunately Xojo’s StyledText is quite slow, so highlighting takes much more time than lexing