[Newbies] pretty printing

Claus Kick claus_kick at web.de
Mon Sep 29 16:52:09 UTC 2008


Bert Freudenberg wrote:
> 
> Am 28.09.2008 um 09:52 schrieb Randal L. Schwartz:
> 
>>>>>>> "Claus" == Claus Kick <claus_kick at web.de> writes:
>>
>>
>> Claus> YourClass >> escape: aString
>>
>> Claus> "escapes a String"
>>
>> Claus> |result|
>>
>> Claus> result := ''.
>> Claus> aString do:[:char | result := result, char xmlEscaped].
>> Claus> ^result
>>
>> This is an expensive way to build a string.  #collect: would be  better:
>>
>> aString collect: [:char | char xmlEscaped].

I had actually in mind to do it this way - but I thought the do:[] is 
easier to read. Runtime performance is always step 2 for me.

>> Internally, that uses a Stream, which extends itself nicely as new  data
>> appears.  In your version, the early string data is getting  
>> repeatedly copied
>> to make each new string.  Ouch.

That may be, my Squeak is rusty (VW these days).

> Err, #collect: constructs a string character by character, not from  
> other strings. Double-ouch ;)

So, the gist is -  whether you do do:[] or collect:[] doesnt matter much?


More information about the Beginners mailing list