[squeak-dev] readFrom: vs readFromString:

Stéphane Rollandin lecteur at zogotounga.net
Tue Nov 1 16:45:17 UTC 2011


> That's it, I would change for something like
>
> readFromString: aString
>      | aStream newInstance |
>      aStream := aString readString.
>      newInstance := self readFrom: aStream.
>      aStream atEnd ifFalse: [FormatError raise].
>     ^newInstance

I would propose to break this in two methods:

readFromString: aString onLeftOver: aBlock
       | aStream newInstance |
       aStream := aString readString.
       newInstance := self readFrom: aStream.
       aStream atEnd ifFalse: [aBlock value].
      ^newInstance

and

readFromString: aString

	^ readFromString: aString onLeftOver: [FormatError raise]


This way we can handle cases where we do not care about trailing stuff, 
without having to modify the string (which could be expensive)

Stef




More information about the Squeak-dev mailing list