I took Peter Brigham's 'offsets' routine, found in his 'TextLib' stack
Code: Select all
function offsets str,tContainer,includeOverlaps
-- returns a comma-delimited list of all the offsets of str in tContainer
-- returns 0 if not found
-- third param is optional:
-- offsets("xx","xxxxxx") returns "1,3,5" not "1,2,3,4,5"
-- ie, by default, overlapping offsets are not counted
-- if you want overlapping offsets then pass "true" in 3rd param
-- note: to get the last occurrence of a string in a container (often useful)
-- use "item -1 of offsets(...)"
if str is not in tContainer then return 0
if includeOverlaps = empty then put false into includeOverlaps
put empty into offsetList
put 0 into startPoint
repeat
put offset(str,tContainer,startPoint) into thisOffset
if thisOffset = 0 then exit repeat
add thisOffset to startPoint
put startPoint & comma after offsetList
if not includeOverlaps then
add length(str)-1 to startPoint
end if
end repeat
return item 1 to -1 of offsetList -- delete trailing comma
end offsets
Very often but not always I got an endless loop.
I modified Peter's function:
Code: Select all
function offsets str,tContainer,includeOverlaps
put "" into msg
put "" into XX
put length(tContainer) into charCount
put length(str)-1 into LS
--
if str is not in tContainer then return 0
if includeOverlaps = empty then put false into includeOverlaps
put empty into offsetList
put 0 into startPoint
repeat
put offset(str,tContainer,startPoint) into thisOffset
if thisOffset = 0 \
or startPoint > charCount \
then exit repeat
add thisOffset to startPoint
put startPoint & comma after offsetList
if not includeOverlaps then
put thisOffset && "•" && startPoint & cr after msg
add LS to startPoint
end if
end repeat
return item 1 to -1 of offsetList -- delete trailing comma
end offsets
1283 • 1283
151109 • 152400
86061 • 238469
4294967288 • 4295205765,
although my source list contains only 263067 (= charCount) characters.
Regards
Peter
-----
MacPro 2012 • OS X El Capitan 10.11.6 • LiveCode Community 8.1.3