StringCollection.st
author Claus Gittinger <cg@exept.de>
Thu, 23 Nov 1995 18:29:43 +0100
changeset 633 46e996b3c18f
parent 608 cd5ac440fa95
child 647 d78d43505b9f
permissions -rw-r--r--
version at the end

"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

VariableArray subclass:#StringCollection
	 instanceVariableNames:''
	 classVariableNames:''
	 poolDictionaries:''
	 category:'Collections-Text'
!

!StringCollection class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    StringCollection is an variable sized array of lines which are strings.
    WARNING:
	This class is temporary (a historic leftover) - it may change or
	even vanish in the future. Use OrderedCollections or other standard
	classes to represent collections of strings.

    StringCollection used to be called Text, but this is a very bad name
     - there is something totally different also named Text in ST-80 ...
"
! !

!StringCollection class methodsFor:'instance creation'!

from:aString
    "return a new text object with lines taken from the argument, aString"

    ^ (self new:1) from:aString
!

fromArray:anArray
    "return a new text object with lines taken from the argument, an array
     of strings"

    |newStringCollection
     size "{ Class: SmallInteger }" |

    size := anArray size.
    newStringCollection := self new:size.
    1 to:size do:[:line |
	newStringCollection at:line put:(anArray at:line)
    ].
    ^ newStringCollection
! !

!StringCollection methodsFor:'converting'!

asString
    "return myself as a string with embedded cr's"

    ^ self asStringWith:Character cr
		   from:1 to:(self size) 
	   compressTabs:false 
		  final:Character cr
!

asStringCollection
    "return the receiver as a stringCollection - thats easy"

    ^ self
!

from:aString
    "setup my contents from the argument, aString"

    |numberOfLines "{ Class:SmallInteger }"
     start         "{ Class:SmallInteger }"
     stop          "{ Class:SmallInteger }" |

    numberOfLines := aString occurrencesOf:(Character cr).
    (aString endsWith:(Character cr)) ifFalse:[
	numberOfLines := numberOfLines + 1.
    ].
    self grow:numberOfLines.
    start := 1.
    1 to:numberOfLines do:[:lineNr |
	stop := aString indexOf:(Character cr) startingAt:start.
	stop == 0 ifTrue:[
	    stop := aString size
	] ifFalse: [
	    stop := stop - 1.
	].

	(stop < start) ifTrue: [
	    self at:lineNr put:(String new:0)
	] ifFalse: [
	    self at:lineNr put:(aString copyFrom:start to:stop)
	].
	start := stop + 2
    ]
! !

!StringCollection methodsFor:'growing'!

add:aString
    "append the argument, aString to myself -
     we increase physical size by 50 to avoid lots of copying around"

    |oldSize "{ Class:SmallInteger }"|

    oldSize := tally.
    super grow:(oldSize + 50).
    tally := oldSize + 1.
    contentsArray at:tally put:aString
!

grow:newSize
    "grow to newsize - new elements are initialized with empty strings -
     not nil"

    |oldSize "{ Class:SmallInteger }"|

    oldSize := tally.
    super grow:newSize.
    (oldSize < newSize) ifTrue:[
	contentsArray from:(oldSize + 1) to:newSize put:''
    ]
! !

!StringCollection methodsFor:'printing'!

printString
    "return the receivers printString"

    ^ self asString
! !

!StringCollection methodsFor:'searching'!

indexOfLineStartingWith:aString
    "return the index of the first line starting with the argument, aString"

    |index "{ Class:SmallInteger }" |

    index := 1.
    [index <= self size] whileTrue:[
	((self at:index) startsWith:aString) ifTrue:[
	    ^ index
	].
	index := index + 1
    ].
    ^ 0
! !

!StringCollection class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.19 1995-11-23 17:28:32 cg Exp $'
! !