How to find the offset of the last instance of a repeating character in a string? (Geoff Canyon)
Brian Milby
brian at milby7.com
Sat Nov 3 01:49:57 EDT 2018
Here is something... probably needs some optimization
function allOffsets2 D,S,pCase
local dLength, C, R
-- returns a comma-delimited list of the offsets of D in S
set the caseSensitive to pCase is true
set the itemDel to D
put length(D) into dLength
put 1 - dLength into C
if dLength > 1 then
local n, i, j, D2, L2
put 0 into n
repeat with i = 2 to dLength
if char i to -1 of D is char 1 to -i of D then
add 1 to n
put char (1-i) to -1 of D into D2[n]
put i-1 into L2[n]
end if
end repeat
end if
repeat for each item i in S
if C > 0 and n > 0 then
repeat with j = 1 to n
if i&D begins with D2[j] then
put C+L2[j],"" after R
end if
end repeat
end if
add length(i) + dLength to C
put C,"" after R
end repeat
set the itemDel to comma
delete char -1 of R
if item -1 of R > len(S) then
if the number of items of R is 1 then
return 0
else
delete item -1 of R
end if
end if
if char -dLength to -1 of S is D then
return R
end if
repeat with j = n down to 1
if char -len(D2[j]) to -1 of S is D2[j] then
delete item -1 of R
end if
end repeat
return R
end allOffsets2
I think a couple of private functions would be good. One for 0 overlap,
one for a single overlap, then a final general one for any number of
overlaps (the core of the above). After the loop that generates D2/L2 I
would branch based on n to avoid the additional comparisons inside the loop.
On Fri, Nov 2, 2018 at 9:45 PM Alex Tweedly via use-livecode <
use-livecode at lists.runrev.com> wrote:
> Oh dear - answering my own posts .... rarely a good sign :-)
>
>
> On 03/11/2018 02:10, Alex Tweedly via use-livecode wrote:
> >
> > On 03/11/2018 00:43, Geoff Canyon via use-livecode wrote:
> >> One thing I don't see how to do without significantly impacting
> >> performance
> >> is to return all offsets if there are overlapping strings. For example:
> >>
> >> allOffsets("aba","abababa")
> >>
> >> would return 1,5, when it might be reasonable to expect it to return
> >> 1,3,5.
> >> Using the offset function with numToSkip would make that easy; adapting
> >> allOffsets to do so would be harder to do cleanly I think.
> >>
> > Can I suggest changing it to "someOffsets()" :-) :-)
> >
> > But seriously, can you not iteratively run "allofsets" ?
> >
> Answer : NO. That doesn't work.
> However, there is a more efficient way that does work - but it needs to
> be tested before I post it.
>
> -- Alex.
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
More information about the use-livecode
mailing list