class: CharacterArray
authorClaus Gittinger <cg@exept.de>
Fri, 08 Feb 2013 15:45:04 +0100
changeset 14768 1da413c31c24
parent 14767 157f37c1a7f9
child 14769 898e4754340b
class: CharacterArray added: #expandPlaceholders:with:
CharacterArray.st
--- a/CharacterArray.st	Fri Feb 08 14:01:03 2013 +0100
+++ b/CharacterArray.st	Fri Feb 08 15:45:04 2013 +0100
@@ -4916,6 +4916,56 @@
 
 !CharacterArray methodsFor:'special string converting'!
 
+expandPlaceholders:escapeCharacter with:argArrayOrDictionary
+    "this is the generic version of the old %-escaping method, allowing for an arbitrary
+     escape character to be used (typically $$ or $% are effectively used).
+     Return a copy of the receiver, where all %i escapes are
+     replaced by corresponding arguments' printStrings from the argArrayOrDictionary.
+     I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results
+     in the new string 'hello world; how is this'.
+     As an extension, the argument may also be a dictionary, providing
+     values for symbolic keys.
+     In this case, %a .. %z and %(...) are also allowed.
+     (%1..%9 require a numeric key in the dictionary, however)
+     To get a '%' character, use a '%%'-escape.
+     To get an integer-indexed placeHolder followed by another digit,
+     or an index > 9, you must use %(digit).
+     See also bindWith:... for VisualAge compatibility."
+
+    |stream|
+
+    stream := (TextStream ? WriteStream) on:(self species new:self size + 20).
+    self expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:stream.
+    ^ stream contents.
+
+
+    "
+     'hello %1' expandPlaceholdersWith:#('world')
+     'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this')
+     'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this')
+     '%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9)
+     '%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123)
+     '%%10 gives %10' expandPlaceholdersWith:#(123)
+     '%%(10) gives %(10)' expandPlaceholdersWith:#(123)
+     '%test gives %1' expandPlaceholdersWith:#(123)
+     'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold }
+     'bla %1 bla' expandPlaceholdersWith:{ 'hello' }
+    "
+
+    "
+     |dict|
+
+     dict := Dictionary new.
+     dict at:1 put:'one'.
+     dict at:$a put:'AAAAA'.
+     dict at:$b put:[ Time now ].
+     dict at:'foo' put:[ Date today ].
+     'hello $1 %a $b %(foo)' expandPlaceholders:$$ with:dict       
+    "
+
+    "Modified: 1.7.1997 / 00:53:24 / cg"
+!
+
 expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream
     "this is the generic version of the old %-escaping method, allowing for an arbitrary
      escape character to be used (typically $$ or $% are effectively used).
@@ -6264,11 +6314,11 @@
 !CharacterArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.493 2013-02-08 09:51:37 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.494 2013-02-08 14:45:04 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.493 2013-02-08 09:51:37 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.494 2013-02-08 14:45:04 cg Exp $'
 ! !