So glad you chimed in, Mark. This is pretty impressive. I'll need to use the "for each element" structure because my tags are not unique, but it still is much faster. When clicking a tag at the top of the document that links to the last anchor at the bottom of the text, I get a timing of about 25ms. If I omit the timing for loading the htmltext and the selection of the text at the end of the handler it brings the timing to almost 0. The test text is long, but not nearly as long as Bernd's sample.

I need to select the entire range of text covered by the metadata span, not just a single word. I've got that working, but since we're on a roll here, I wonder if there's a more optimal way to do it.

I'm using chars instead of codepoints because when I tried it, they both gave the same number. Should I change that?

  put the styledText of fld 1 into tDataA
  put 0 into tTotalChars
  put 0 into tStartChar
  repeat with i = 1 to the number of elements in tDataA
    put tDataA[i]["runs"] into tRunsA
    repeat with j = 1 to the number of elements in tRunsA
      put tRunsA[j] into tRunA
      add the num of chars in tRunA["text"] to tTotalChars
      if tRunA["metadata"] is pTag then
        if tStartChar = 0 then
          put tTotalChars - len(tRunA["text"]) + 3 into tStartChar
        end if
      else if tStartChar > 0 then
        put tTotalChars - len(tRunA["text"]) into tEndChar
        select char tStartChar to tEndChar of fld 1
        select empty
        set the backcolor of char tStartChar to tEndChar of fld 1 to "yellow"
        return tStartChar & comma & tEndChar
      end if
    end repeat
  end repeat

Also, I had to add 3 to tStartChar to get the right starting point but I can't figure out why. Otherwise it selects the last character before the metadata span as the starting point.


On 2/20/20 2:13 AM, Mark Waddingham via use-livecode wrote:
Of course *all* three of my suggested approaches are wrong - I messed up the 
inner loop in each...

On 2020-02-20 07:56, Mark Waddingham via use-livecode wrote:
NON-UNIQUE ANCHORS
repeat with i = 1 to the number of elements in tDataA
  local tRunsA
  put tDataA[i]["runs"] into tRunsA
  repeat with j = 1 to the number of elements in tRunsA
    if tRunsA[j]["metadata"] is tSearchText then
      repeat with m = 1 to j
        add the number of words of tRunsA[m]["text"] to tNumWords
        put true into tFlagExit
        exit repeat
      end repeat
    end if
  end repeat
  if tFlagExit then
    exit repeat
  end if
end repeat
select word tNumWords of line i of field "x"

Should be:

  repeat with i = 1 to the number of elements in tDataA
    local tRunsA
    put tDataA[i]["runs"] into tRunsA
    repeat with j = 1 to the number of elements in tRunsA
      if tRunsA[j]["metadata"] is tSearchText then
        repeat with m = 1 to j
          add the number of words of tRunsA[m]["text"] to tNumWords
        end repeat
        put true into tFlagExit
        exit repeat
      end if
    end repeat
    if tFlagExit then
      exit repeat
    end if
  end repeat
  select word tNumWords of line i of field "x"

UNIQUE ANCHORS

repeat for each key i in tDataA
  local tRunsA
  put tDataA[i]["runs"] into tRunsA
  repeat for each key j in tRunsA
    if tRunsA[j]["metadata"] is tSearchText then
      repeat with m = 1 to j
        add the number of words of tRunsA[m]["text"] to tNumWords
        put true into tFlagExit
        exit repeat
      end repeat
    end if
  end repeat
  if tFlagExit then
    exit repeat
  end if
end repeat
select word tNumWords of line i of field "x"

Should be:

  repeat for each key i in tDataA
    local tRunsA
    put tDataA[i]["runs"] into tRunsA
    repeat for each key j in tRunsA
      if tRunsA[j]["metadata"] is tSearchText then
        repeat with m = 1 to j
          add the number of words of tRunsA[m]["text"] to tNumWords
        end repeat
        put true into tFlagExit
        exit repeat
      end if
    end repeat
    if tFlagExit then
      exit repeat
    end if
  end repeat
  select word tNumWords of line i of field "x"

RUN WITH METADATA DEFINES SELECTION - NON-UNIQUE SEARCH

repeat with i = 1 to the number of elements in tDataA
  local tRunsA
  put tDataA[i]["runs"] into tRunsA
  repeat with j = 1 to the number of elements in tRunsA
    local tRunA
    put tRunsA[j] into tRunA
    if tRunA["metadata"] is tSearchText then
      repeat with m = 1 to j - 1
        add the number of codeunits of tRunsA[m]["text"] to tNumCodeunitsBefore
        put the number of codeunits in tRunA["text"] into tNumCodeunits
        put true into tFlagExit
        exit repeat
      end repeat
    end if
  end repeat
  if tFlagExit then
    exit repeat
  end if
end repeat
select codeunit tNumCodeunitsBefore to tNumCodeunitsBefore +
tNumCodeunits - 1 of line i of field "x"

Should be:

  repeat with i = 1 to the number of elements in tDataA
    local tRunsA
    put tDataA[i]["runs"] into tRunsA
    repeat with j = 1 to the number of elements in tRunsA
      local tRunA
      put tRunsA[j] into tRunA
      if tRunA["metadata"] is tSearchText then
        repeat with m = 1 to j - 1
          add the number of codeunits of tRunsA[m]["text"] to 
tNumCodeunitsBefore
        end repeat
        put the number of codeunits in tRunA["text"] into tNumCodeunits
        put true into tFlagExit
        exit repeat
      end if
    end repeat
    if tFlagExit then
      exit repeat
    end if
  end repeat
 select codeunit tNumCodeunitsBefore to tNumCodeunitsBefore + tNumCodeunits - 1 of line i of field "x"

Oops!

Mark.



--
Jacqueline Landman Gay         |     jac...@hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com


_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Reply via email to