How to find the offset of the last instance of a repeating character in a string?

Geoff Canyon gcanyon at gmail.com
Thu Nov 1 03:15:37 EDT 2018


I was curious if using the itemDelimiter might work for this, so I wrote
the below code out of curiosity; but in my quick testing with single-byte
characters it was only about 30% faster than the above methods, so I didn't
bother to post it.

But Ben Rubinstein just posted about a terrible slow-down doing pretty much
this same thing for text with unicode characters. So I ran a simple test
with 8000 character long strings that start with a single unicode
character, this is about 15x faster than offset() with skip. For
100,000-character lines it's about 300x faster, so it seems to be immune to
the line-painter issues skip is subject to. So for what it's worth:

function offsetList D,S
   -- returns a comma-delimited list of the offsets of D in S
   set the itemDel to D
   repeat for each item i in S
      add length(i) + 1 to C
      put C,"" after R
   end repeat
   set the itemDel to comma
   if char -1 of S is D then return char 1 to -2 of R
   put length(C) + 1 into lenC
   put length(R) into lenR
   if lenC = lenR then return 0
   return char 1 to lenR - lenC - 1 of R
end offsetList

>



More information about the use-livecode mailing list