[Newbies] pretty printing

Bert Freudenberg bert at freudenbergs.de
Sun Sep 28 17:33:38 UTC 2008


Am 28.09.2008 um 10:12 schrieb Bert Freudenberg:

> Am 28.09.2008 um 09:52 schrieb Randal L. Schwartz:
>>
>> This is an expensive way to build a string.  #collect: would be  
>> better:
>>
>> aString collect: [:char | char xmlEscaped].
>>
>> 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.
>
>
> Err, #collect: constructs a string character by character, not from  
> other strings. Double-ouch ;)


Guess I have to provide an alternative:

#('&' '&amp;' '<' '&lt;' '>' '&gt;' '''' '&apos;' '"' '&quot;')
	pairsDo: [:c :s| string := string copyReplaceAll: c with: s].

which is invariant under pretty-printing so must be good ;)

(this is the same approach as in String>>asHTML)

- Bert -




More information about the Beginners mailing list