How to find the offset of the last instance of a repeating character in a string? (Geoff Canyon)

Brian Milby brian at
Sun Nov 4 19:01:55 EST 2018

Logic matches my solution.  I also validated my solution using just the
offset function.  Speed hit for with overlap is similar.  One possible

put kList is not empty into pWithOverlaps

If with overlaps was requested but the source delimiter did not contain any
overlaps, then the extra loops are skipped.

Adding a character to the end is clever.  I'll need to incorporate that and
see what it does to my method.

My take on the code updates is here:

Stack and index of scripts here:

On Sun, Nov 4, 2018 at 12:42 PM Geoff Canyon via use-livecode <
use-livecode at> wrote:

> Alex, good catch! The code below and at
> now puts a stop character after the
> string to prevent the error you found. I also added a "with overlaps"
> option. I think this is correct, and about as efficient as possible, but
> thanks to anyone who finds a bug or a faster way.
> gc
> function allOffsets D,S,pCase,pWithOverlaps
>    -- returns a comma-delimited list of the offsets of D in S
>    set the caseSensitive to pCase is true
>    put length(D) into dLength
>    put numtochar(chartonum(char -1 of D) mod 2 + 1) after S
>    if pWithOverlaps then
>       repeat with i = 1 to dLength - 1
>          if not (char i + 1 to -1 of D is char 1 to dLength - i of D) then
> next repeat
>          put char -i to -1 of D into OV[i]
>          put i & cr after kList
>       end repeat
>    end if
>    set the itemDel to D
>    put 1 - dLength into C
>    if pWithOverlaps then
>       repeat for each item i in S
>          repeat for each line K in kList
>             if char 1 to K of (i & D) is OV[K] then put (C + K),"" after R
>          end repeat
>          add length(i) + dLength to C
>          put C,"" after R
>       end repeat
>    else
>       repeat for each item i in S
>          add length(i) + dLength to C
>          put C,"" after R
>       end repeat
>    end if
>    set the itemDel to comma
>    repeat until item 1 of R > 0
>       delete item 1 of R
>    end repeat
>    delete item -1 of R
>    if R is empty then return 0 else return char 1 to -2 of R
> end allOffsets
> _______________________________________________
> use-livecode mailing list
> use-livecode at
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:

More information about the use-livecode mailing list