Hi Kent,
I don't have a set location for it yet. The current location is examples/gui/stringutil.inc as it is being tested with the opengl controls. (PortViewer2.o2bas)
The classes do not have built-in iterators but traversing the text is quite simple
e=txt.lastline
for i=1 to e
s=txt.line i
...
next
Charles
string cr=chr(13)+chr(10)
string tab=chr(9)
'Elastic String array
'----------------
class StringArray
'================
protected
en as sys
mx as sys
list as bstring
public
method length(byval i as sys) as sys
'===================================
if i>mx then return 0
bstring * t : &t=?list
if i>0 then return len t[i]
end method
method line(byval i as sys, byval s as bstring)
'=============================================
sys a
if i>en then
a=(i+4096-en)*4
en=4096+i
list+=nuls a
end if
bstring * t : &t=?list
if i>0 then
t[i]=s
if mx<i then mx=i
end if
end method
method insert(byval i as sys, byval s as bstring)
'================================================
sys a,e
if i>mx+1 then exit method
mx+=1
en+=1
if i>0 then
a=i*4-4 'strings below i
list=left(list,a)+nuls(4)+mid(list,a+1) 'alter list of pointers only
bstring * t : &t=?list
'e=?list+a : *e=0 'nullify slot before assigning string
t[i]=s
end if
end method
method delete(byval i as sys)
'============================
if en=0 then exit method
if mx=0 then exit method
if i>mx then exit method
en-=1 : mx-=1
if i>0 then
sys a
bstring * t : &t=?list
'a=i*4-4+&t : frees *a
a=?t[i] : frees a
't[i]=""
a=i*4-4
list=left(list,a)+mid(list,a+5) 'alter list of pointers only
mx-=1
end if
end method
method line(byval i as sys) as bstring
'=====================================
bstring * t : &t=?list
if i>0 then
if i<=mx then return t[i]
end if
end method
'-----------------------
method LastLine() as sys
'=======================
return mx
end method
method locate(w as string, i as sys, j as sys) as sys
'====================================================
'case insensitive
'returns linepos i and charpos j
'
sys e
string s,k
k=lcase w
e=mx
if i<1 then i=1
if j<1 then j=1
do
if i>e then exit do
s=lcase line(i)
if len s then j=instr j,s,k else j=0
if j then return i else j=1
i+=1
end do
i=0 : j=0
end method
method free()
'============
sys i, q=?list
for i=1 to mx
frees *q : q+=4
next
frees ?list : ?list=0
en=0 : mx=0
end method
end class
'--------------
class TextArray
'==============
has StringArray
method load(n as string) as sys
'==============================
sys i=0, a=1, b=1, et=0
string s=getfile n
do
b=instr a,s,cr
if b=0 then
b=len(s)+1 : et=1
if b=1 then return 0
end if
i+=1 : line(i)=mid s,a,b-a
if et then
if a=b then i-=1 : mx-=1 'null line at end
return i 'number of lines
end if
a=b+2
end do
end method
method save(n as string) as sys
'==============================
sys i=1, la, ls, le, e
string s="" : e=mx
do
if i>e then exit do
le=length(i)+2
if la+le>ls then
s+=nuls le+0x10000 'add more space to buffer
ls+=le+0x10000
end if
mid(s,la)=line(i)+cr
la+=le
i+=1
end do
putfile n,left s,la-1
end method
end class
'------------------------------------
sub skiplspace(s as string, i as sys)
'====================================
sys e=len s
sys a
do
if i>e then exit do
a=asc s,i
if a>32 then exit do
end do
end sub
'-------------------------------------------------
function getword(s as string, b as sys) as string
'=================================================
'b=1
sys a,c,d,bb,bc
a=0
bb=b
do
c=asc s,b
if c=0 then exit do
if c>32 then exit do
b+=1
end do
bc=b
do
c=asc s,b
if c<=32 then exit do
if c=34 or c=96 then
do
b+=1
d=asc s,b
if d=0 or d=c then b+=1 : jmp fwd done
end do
end if
if c<48 then
if c=35 then jmp fwd more '#
if b=bc then b+=1
exit do
end if
if c<58 then jmp fwd more
if c<64 then
if b=bc then b+=1
exit do
end if
if c<92 then jmp fwd more
if c<97 then
if c=95 then jmp fwd more ' underscore
if b=bc then b+=1
exit do
end if
if c<123 then jmp fwd more
if c<128 then
if b=bc then b+=1
exit do
end if
'
more:
'
b+=1
end do
'
done:
'
if b=bb then function="" else function=mid s,bb,b-bb
end function