changed: #expandPlaceholdersWith:on:
care for character vs. single-char-string keys
--- a/CharacterArray.st Wed Jan 27 15:10:32 2010 +0100
+++ b/CharacterArray.st Wed Jan 27 15:14:42 2010 +0100
@@ -4054,6 +4054,7 @@
! !
+
!CharacterArray methodsFor:'padded copying'!
centerPaddedTo:newSize
@@ -4652,7 +4653,7 @@
See also bindWith:... for VisualAge compatibility.
Use %<cr> to insert a CR and %<tab> to insert a TAB."
- |next v key keyAsSymbol
+ |next v key
idx "{ SmallInteger }"
idx2 "{ SmallInteger }"
start "{ SmallInteger }"
@@ -4661,81 +4662,87 @@
stop := self size.
start := 1.
[start <= stop] whileTrue:[
- idx := self indexOf:$% startingAt:start.
- (idx == 0 or:[idx == stop]) ifTrue:[
- aStream nextPutAll:self startingAt:start to:stop.
- ^ self.
- ].
- "found a %"
- aStream nextPutAll:self startingAt:start to:(idx - 1).
- next := self at:(idx + 1).
- (next == $%) ifTrue:[
- aStream nextPut:$%.
- ] ifFalse:[
- next == $< ifTrue:[
- idx2 := self indexOf:$> startingAt:idx+2.
- key := self copyFrom:idx+2 to:idx2-1.
- idx := idx2 - 1.
- key := key asSymbolIfInterned.
- (#(cr tab nl return lf ff null) includesIdentical:key) ifTrue:[
- aStream nextPut:(Character perform:key).
- ].
- ] ifFalse:[
- (next between:$1 and:$9) ifTrue:[
- v := argArrayOrDictionary at:(next digitValue) ifAbsent:nil
- ] ifFalse:[next == $( ifTrue:[
- idx2 := self indexOf:$) startingAt:idx+2.
- key := self copyFrom:idx+2 to:idx2-1.
- idx := idx2 - 1.
- key:= key asSymbolIfInterned ? key.
- (keyAsSymbol notNil and:[ argArrayOrDictionary includesKey:keyAsSymbol ]) ifTrue:[
- v := argArrayOrDictionary at:keyAsSymbol
- ] ifFalse:[
- key isNumeric ifTrue:[
- key := Integer readFrom:key onError:nil.
- ].
- v := argArrayOrDictionary at:key ifAbsent:nil
- ].
- ] ifFalse:[
- "so next is a non-numeric single character.
- This would not work with Arrays, since they have integer indizes"
- argArrayOrDictionary isSequenceable ifFalse:[
- v := argArrayOrDictionary at:next ifAbsent:[
- "try symbol instead of character"
- argArrayOrDictionary at:next asSymbol ifAbsent:[String with:$% with:next].
- ].
- ] ifTrue:[
- v := String with:$% with:next. "No match, keep original sequence"
- ].
- ]].
- v notNil ifTrue:[
- v isBlock ifTrue:[
- v := v value
- ].
-
- v printOn:aStream.
- ].
- ]].
- start := idx + 2
+ idx := self indexOf:$% startingAt:start.
+ (idx == 0 or:[idx == stop]) ifTrue:[
+ aStream nextPutAll:self startingAt:start to:stop.
+ ^ self.
+ ].
+ "found a %"
+ aStream nextPutAll:self startingAt:start to:(idx - 1).
+ next := self at:(idx + 1).
+ (next == $%) ifTrue:[
+ aStream nextPut:$%.
+ ] ifFalse:[
+ next == $< ifTrue:[
+ idx2 := self indexOf:$> startingAt:idx+2.
+ key := self copyFrom:idx+2 to:idx2-1.
+ idx := idx2 - 1.
+ key := key asSymbolIfInterned.
+ (#(cr tab nl return lf ff null) includesIdentical:key) ifTrue:[
+ aStream nextPut:(Character perform:key).
+ ].
+ ] ifFalse:[
+ (next between:$1 and:$9) ifTrue:[
+ v := argArrayOrDictionary at:(next digitValue) ifAbsent:nil
+ ] ifFalse:[
+ next == $( ifTrue:[
+ idx2 := self indexOf:$) startingAt:idx+2.
+ key := self copyFrom:idx+2 to:idx2-1.
+ idx := idx2 - 1.
+ key := key asSymbolIfInterned ? key.
+ (argArrayOrDictionary includesKey:key) ifTrue:[
+ v := argArrayOrDictionary at:key
+ ] ifFalse:[
+ (key size == 1 and:[ argArrayOrDictionary includesKey:(key at:1)]) ifTrue:[
+ v := argArrayOrDictionary at:(key at:1)
+ ] ifFalse:[
+ key isNumeric ifTrue:[
+ key := Integer readFrom:key onError:nil.
+ ].
+ v := argArrayOrDictionary at:key ifAbsent:nil
+ ]
+ ].
+ ] ifFalse:[
+ "so next is a non-numeric single character.
+ This would not work with Arrays, since they have integer indizes"
+ argArrayOrDictionary isSequenceable ifFalse:[
+ v := argArrayOrDictionary at:next ifAbsent:[
+ "try symbol instead of character"
+ argArrayOrDictionary at:next asSymbol ifAbsent:[String with:$% with:next].
+ ].
+ ] ifTrue:[
+ v := String with:$% with:next. "No match, keep original sequence"
+ ].
+ ]
+ ].
+ v notNil ifTrue:[
+ v isBlock ifTrue:[
+ v := v value
+ ].
+
+ v printOn:aStream.
+ ].
+ ]].
+ start := idx + 2
].
"
String streamContents:[:s|
- 'hello %1' expandPlaceholdersWith:#('world') on:s.
- s cr.
- 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s.
- s cr.
- 'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s.
- s cr.
- '%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s.
- s cr.
- '%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s.
- s cr.
- '%%10 gives %10' expandPlaceholdersWith:#(123) on:s.
- s cr.
- '%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s.
- s cr.
- '%test gives %1' expandPlaceholdersWith:#(123) on:s.
+ 'hello %1' expandPlaceholdersWith:#('world') on:s.
+ s cr.
+ 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s.
+ s cr.
+ 'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s.
+ s cr.
+ '%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s.
+ s cr.
+ '%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s.
+ s cr.
+ '%%10 gives %10' expandPlaceholdersWith:#(123) on:s.
+ s cr.
+ '%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s.
+ s cr.
+ '%test gives %1' expandPlaceholdersWith:#(123) on:s.
]
"
@@ -4747,7 +4754,7 @@
dict at:$a put:'AAAAA'.
dict at:$b put:[ Time now ].
String streamContents:[:s|
- 'hello %1 %a %b' expandPlaceholdersWith:dict on:s.
+ 'hello %1 %a %b' expandPlaceholdersWith:dict on:s.
].
"
!
@@ -5746,11 +5753,11 @@
!CharacterArray class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.427 2010-01-26 11:30:29 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.428 2010-01-27 14:14:42 sr Exp $'
!
version_CVS
- ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.427 2010-01-26 11:30:29 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.428 2010-01-27 14:14:42 sr Exp $'
! !
CharacterArray initialize!