Am 29.09.2008 um 10:07 schrieb Claus Kick:
Bert Freudenberg wrote:
Guess I have to provide an alternative: #('&' '&' '<' '<' '>' '>' '''' ''' '"' '"') pairsDo: [:c :s| string := string copyReplaceAll: c with: s].
(this is the same approach as in String>>asHTML)
Ok, to nitpick (;)) one message send less for each pair:
#('&' '&' '<' '<' '>' '>' '''' ''' '"' '"') pairsDo: [:c :s| string := string copyReplaceAll: c with: s asTokens:false].
This is silly advice. Why would you willingly expose an implementation detail? There are many constructs that could be expanded but readability and portability is hurt by that.
How do pairs compare to dictionaries in Squeak?
Well, there are no "pairs" per se but just an iteration method on sequenceable collections.
When iterating a Dictionary, the order is unpredictable. In my example it is vital to replace '&' first.
Also, Squeak does not have a literal syntax for dictionaries but for Arrays so my example is more efficient because the Array is constructed at compile-time. OTOH, in real code one might put the dictionary into a class variable so this wouldn't matter.
- Bert -