"
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:#Text
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category:'Collections-Text'
!
Text comment:'
COPYRIGHT (c) 1989 by Claus Gittinger
All Rights Reserved
text is an array of lines which are strings.
this is just temporary - may change in the future.
Also, Text is a bad name - there is something totally different also named Text
in ST-80 ...
$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.6 1994-06-02 11:36:22 claus Exp $
'!
!Text 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"
|newText
size "{ Class: SmallInteger }" |
size := anArray size.
newText := self new:size.
1 to:size do:[:line |
newText at:line put:(anArray at:line)
].
^ newText
! !
!Text methodsFor:'growing'!
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:''
]
!
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
! !
!Text methodsFor:'converting'!
asString
"return myself as a string with embedded cr's"
|totalLength "{ Class:SmallInteger }"
pos "{ Class:SmallInteger }"
newString |
totalLength := 0.
self do:[:lineString |
lineString isNil ifTrue:[
totalLength := totalLength + 1
] ifFalse: [
totalLength := totalLength + lineString size + 1
].
0
].
newString := String new:totalLength.
pos := 1.
self do:[:lineString |
lineString isNil ifFalse:[
newString replaceFrom:pos with:lineString.
pos := pos + (lineString size)
].
newString at:pos put:(Character cr).
pos := pos + 1
].
^ newString
!
asStringFrom:firstLine to:lastLine
"return part of myself as a string with embedded cr's"
|totalLength "{ Class:SmallInteger }"
pos "{ Class:SmallInteger }"
newString lineString|
totalLength := 0.
firstLine to:lastLine do:[:lineIndex |
lineString := self at:lineIndex.
lineString isNil ifTrue:[
totalLength := totalLength + 1
] ifFalse: [
totalLength := totalLength + lineString size + 1
].
0
].
newString := String new:totalLength.
pos := 1.
firstLine to:lastLine do:[:lineIndex |
lineString := self at:lineIndex.
lineString isNil ifFalse:[
newString replaceFrom:pos with:lineString.
pos := pos + (lineString size)
].
newString at:pos put:(Character cr).
pos := pos + 1
].
^ newString
!
from:aString
"setup my contents from the argument, aString"
|numberOfLines "{ Class:SmallInteger }"
start "{ Class:SmallInteger }"
stop "{ Class:SmallInteger }" |
numberOfLines := aString occurrencesOf:(Character cr).
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
]
!
asText
^ self
! !
!Text methodsFor:'printing'!
printString
^ self asString
! !
!Text 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
! !