[FIX] TextStreamFix ([er] questions)

Boris Gaertner Boris.Gaertner at gmx.net
Sat Jun 7 21:45:07 UTC 2003


<danielv at netvision.net.il> wrote_
> Brent, I think the growTo: addition is good.

Well, I took part in that work and now I have really really to
write you an answer. I apologize that I need two .

>
> In using replaceFrom: position+1 to: position + n with: aCollection
> startingAt: 1, I think you're fixing the wrong thing - you're working
> around the real culprit, which is that Text>>replaceFrom:to:with: does
> copying, which is quite contrary to other implementations of the same
> selector.

SequenceableCollection>>replaceFrom:to:with:
delegates to the modifying method
replaceFrom:to:with:startingAt:,
and Text does not do that. There are no other
implementors of replaceFrom:to:with: in the standard 3.4
image.
 
Text has also an in-place replacement method. It is
replaceFrom: start to: stop with: replacement startingAt: repStart

This method was added a year ago by tk, in change set #4879,
which in turn was an addition to change set #4872 that improved
WriteStream>>pastEndPut. (better growing )

> Also, I don't understand the reasoning in removing the test Tim left
> there -
> (aCollection isMemberOf: String) not
> ifTrue: [^ super nextPutAll: aCollection].
>
And I do not understand why it is present at all. aCollection is typically
an instance of Text, and this statement sends adding to one of the
superclasses - to either WriteStream or Stream. WriteStream>>nextPutAll:
does the work when there is enough room left in the stream 
collection; otherwise, the work is further delegated to Stream, which
does element-wise adding.

When a Collection is really a String, it would be converted into
a Text in  Text>>replaceFrom:to:with:


The original method

nextPutAll: aCollection 
 "Optimized access to get around Text at:Put: overhead"
 | n |
 n _ aCollection size.
 ((aCollection isMemberOf: String) not or: [position + n > writeLimit])
    ifTrue: [^ super nextPutAll: aCollection].
 collection string
    replaceFrom: position+1
    to: position + n
    with: aCollection
   startingAt: 1.
 position _ position + n


handles strings in a very strange way: When there is enough free
space in the string, it adds to the string of the text, but not also 
to the runs. This works because instances of Text are always
created with a run that contains a default text attribute.
----------------------------------------------------


While I write this, I remember that I found a third place, where
the method growTo: allows the use of string replacement:
It is in WriteStream:

next: anInteger putAll: aCollection startingAt: startIndex
 "Store the next anInteger elements from the given collection."

 | newEnd |
 collection class == aCollection class ifFalse:
  [^ super next: anInteger putAll: aCollection startingAt: startIndex].

 newEnd _ position + anInteger.
 newEnd > writeLimit ifTrue:
  [self growTo: newEnd + 10].

 collection replaceFrom: position+1 
                to: newEnd  
                with: aCollection 
               startingAt: startIndex.
 position _ newEnd.

 ^aCollection


Currently WriteStream does not implement this method,
it inherits a method with this name from PositionableStream.


> Daniel

Greetings, Boris



More information about the Squeak-dev mailing list