Author Topic: Elastic String Array  (Read 9726 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Elastic String Array
« on: October 29, 2010, 08:39:11 PM »
Two classes For the string library:

StringArray works like a dynamic array of dynamic strings. It does not need to be dimensioned or redimensioned, though it should be freed when no longer required.

TextArray is derived from StringArray and supports loading and saving files. When a file is loaded, each line is assigned to a location in the array (with crlf stripped).

Getting and Putting is acheived by using pseudovariables. They look just like normal array assignments (though always using round brackets, not square ones).

Example:

TextArray t

t.line(1)="Hello"

print t.line(1)

t.load "prog.o2bas"

t.save "temp.o2bas"


I use these classes in the Opengl Edit project.

Charles

Code: [Select]
'Newer version further on
« Last Edit: December 05, 2010, 03:18:03 AM by Charles Pegge »

kryton9

  • Guest
Re: Elastic String Array
« Reply #1 on: November 08, 2010, 06:24:26 PM »
You got a nice stl like container, actually even nicer there Charles. One additional thing would be an iterator, but not as STL does it.
Paradox the old Borland Language used to have a really nice command named scan.

So you could have:
TextArray t
t.load "prog.o2bas"
scan t
  ..it would go through each line of t here
  and do operations...
endscan

kryton9

  • Guest
Re: Elastic String Array
« Reply #2 on: December 05, 2010, 12:19:55 AM »
Charles, are these in the latest oxygen download? I can't seem to find them in there.

Charles Pegge

  • Guest
Re: Elastic String Array
« Reply #3 on: December 05, 2010, 03:16:02 AM »
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

Code: [Select]

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


efgee

  • Guest
Re: Elastic String Array
« Reply #4 on: November 02, 2011, 12:11:15 PM »
Charles,

testing the textarray class with this:

Code: [Select]
' string_array.o2bas
'
' Test of StringArray and TextArray
' efgee

$FileName "string_array.exe"

includepath "..\..\inc\"
include "StringUtil.inc"

TextArray t
t.load "string_array.o2bas"

e=t.lastline
for i=1 to e
  print t.line(i)
next

t.save "temp.o2bas"


and the first char (') is missing in the temp file.

Charles Pegge

  • Guest
Re: Elastic String Array
« Reply #5 on: November 02, 2011, 01:27:27 PM »
Hi Frank,

I checked the save method and found a missing offset, highlighted here in black.

  method save(n as string) as sys
  '==============================
  sys i=1, la=1, 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 


Thanks!

Charles

efgee

  • Guest
Re: Elastic String Array
« Reply #6 on: November 02, 2011, 03:02:34 PM »
Thanks

This did the trick.


Was wondering about the variable names:

"FileOffset" for "la" would have been my next guess  :D


BTW: If the variable names would be more expressive (instead of "la", "le" etc.) I would have taken a stab at it and tried to fixed it myself - I'm not a genius. As it is right now I have to waste more of your time than I want to  :P




Charles Pegge

  • Guest
Re: Elastic String Array
« Reply #7 on: November 04, 2011, 10:21:33 AM »

You may have noticed, I have a strong bias towards algebra and an aversion to long variable names :)

I find it is much easier to recognise coding patterns when the symbols are kept short. This is useful for recognising repetitive code, which can be compacted into utility functions.

Charles

efgee

  • Guest
Re: Elastic String Array
« Reply #8 on: November 04, 2011, 04:58:41 PM »
Feared that... I'm just the other way around; I like long variable names.

Maybe this is why you and Peter get along so well  ;D

Take care

Charles Pegge

  • Guest
Re: Elastic String Array
« Reply #9 on: November 04, 2011, 11:22:44 PM »

I appreciate long descriptive names are needed for Windows interfacing, but they obfuscate a calculation or formula, which is why short symbolic names are favoured in the sciences.

In my dream IDE, tool tips are used to show descriptions for any symbol.

Charles

Peter

  • Guest
Re: Elastic String Array
« Reply #10 on: November 05, 2011, 06:43:31 AM »
Hi efgee,

Quote
I'm just the other way around; I like long variable names.

is this your way?

Code: [Select]
sys together,we_are_together, some_friends
sys my_dog, my_cat, my_bird
sys we_have_these_animals

together =10
some_friends =-5

we_are_together = together + some_friends
print "we are together: " + we_are_together + " people in the dark forest"

my_dog  =1
my_cat  =1
my_bird =2

we_have_these_animals = my_dog + my_cat + my_bird
print "we have " + we_have_these_animals +" animals at home"

efgee

  • Guest
Re: Elastic String Array
« Reply #11 on: November 05, 2011, 06:53:20 AM »
Peter,
you nailed it.

You might not know, but this will be the new Oxygen coding standard  :D


Peter

  • Guest
Re: Elastic String Array
« Reply #12 on: November 05, 2011, 07:03:01 AM »
LOL

kryton9

  • Guest
Re: Elastic String Array
« Reply #13 on: November 05, 2011, 11:40:52 AM »
Code: OxygenBasic
  1. sys together, weAreTogether, someFriends
  2. sys myDog, myCat, myBird
  3. sys weHaveTheseAnimals
  4.  
  5. together =10
  6. someFriends =-5
  7.  
  8. weAreTogether = together + someFriends
  9. print "we are together: " + weAreTogether + " people in the dark forest"
  10.  
  11. myDog  =1
  12. myCat  =1
  13. myBird =2
  14.  
  15. weHaveTheseAnimals = myDog + myCat + myBird
  16. print "we have " + weHaveTheseAnimals +" animals at home"

This is very readable and understandable code. The underscores were the problem Peter :)

I like descriptive variable names, but for simple formulas, old 1 character variables are fine.
« Last Edit: November 05, 2011, 11:43:38 AM by kryton9 »

kryton9

  • Guest
StringUtil.inc problems
« Reply #14 on: June 05, 2012, 03:05:27 PM »
Charles, I tried to use the StringUtils.inc today and am getting an error that I don't understand. Hope you can help.

Code: OxygenBasic
  1. 'extractText.bas
  2. 'break parts from a bigger text file into sub components
  3. 'by Kent Sarikaya (kryton9) 2012 June
  4.  
  5. include "..\..\inc\StringUtil.inc"
  6.  
  7. TextArray t
  8. string s, s2
  9. s = ""
  10. s2 = ""
  11. t.Load "input.txt"
  12. eof = t.LastLine
  13. for i = 1 to eof
  14.     s = t.Line(i)
  15.     if left( s, 2 ) := "% " then
  16.         s2 += ( s + cr )
  17.     end if
  18. next
  19. putfile "output.txt", s2
  20.  
  21.