[Newbies] pretty printing

Randal L. Schwartz merlyn at stonehenge.com
Sun Sep 28 16:52:46 UTC 2008


>>>>> "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].

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.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


More information about the Beginners mailing list