# HG changeset patch # User Claus Gittinger # Date 1433064689 -7200 # Node ID db49c54b84d2cfbc31f692fa1f6183db8b8ccb3e # Parent 280626c612bcad4887c8ce743af5f0834699adcc class: CharacterArray comment/format in: #expandPlaceholders:with:on: #expandPlaceholdersWith: #expandPlaceholdersWith:on: diff -r 280626c612bc -r db49c54b84d2 CharacterArray.st --- a/CharacterArray.st Sun May 31 11:24:57 2015 +0200 +++ b/CharacterArray.st Sun May 31 11:31:29 2015 +0200 @@ -757,7 +757,6 @@ ^ self == CharacterArray ! ! - !CharacterArray methodsFor:'Compatibility-ANSI'! addLineDelimiters @@ -5909,17 +5908,21 @@ 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). + Write the receiver to aStream, 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) Also, the values in argArrayOrDictionary may be blocks. + 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. Use % to insert a CR and % to insert a TAB." @@ -5932,97 +5935,97 @@ stop := self size. start := 1. [start <= stop] whileTrue:[ - idx := self indexOf:escapeCharacter startingAt:start. - (idx == 0 or:[idx == stop]) ifTrue:[ - aStream nextPutAll:self startingAt:start to:stop. - ^ self. - ]. - "found an escapeCharacter" - aStream nextPutAll:self startingAt:start to:(idx - 1). - next := self at:(idx + 1). - (next == escapeCharacter) ifTrue:[ - aStream nextPut:escapeCharacter. - ] 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 isDigit ifTrue:[ - v := argArrayOrDictionary at:(next digitValue) ifAbsent:'' - ] ifFalse:[ - next == $( ifTrue:[ - idx2 := self indexOf:$) startingAt:idx+2. - key := self copyFrom:idx+2 to:idx2-1. - idx := idx2 - 1. - (argArrayOrDictionary includesKey:key) ifTrue:[ - v := argArrayOrDictionary at:key - ] ifFalse:[ - 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:'' - ] - ]. - ]. - ] ifFalse:[ - (next isLetter and:[argArrayOrDictionary isSequenceable not "is a Dictionary"]) ifTrue:[ - "so next is a non-numeric single character." - v := argArrayOrDictionary - at:next - ifAbsent:[ - "try symbol instead of character" - argArrayOrDictionary - at:next asSymbol - ifAbsent:[String with:escapeCharacter with:next]. - ]. - ] ifFalse:[ - v := String with:$% with:next. - ]. - ] - ]. - "/ v notNil ifTrue:[ - v isBlock ifTrue:[ - v := v value - ]. - - v printOn:aStream. - "/ ]. - ] - ]. - start := idx + 2 + idx := self indexOf:escapeCharacter startingAt:start. + (idx == 0 or:[idx == stop]) ifTrue:[ + aStream nextPutAll:self startingAt:start to:stop. + ^ self. + ]. + "found an escapeCharacter" + aStream nextPutAll:self startingAt:start to:(idx - 1). + next := self at:(idx + 1). + (next == escapeCharacter) ifTrue:[ + aStream nextPut:escapeCharacter. + ] 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 isDigit ifTrue:[ + v := argArrayOrDictionary at:(next digitValue) ifAbsent:'' + ] ifFalse:[ + next == $( ifTrue:[ + idx2 := self indexOf:$) startingAt:idx+2. + key := self copyFrom:idx+2 to:idx2-1. + idx := idx2 - 1. + (argArrayOrDictionary includesKey:key) ifTrue:[ + v := argArrayOrDictionary at:key + ] ifFalse:[ + 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:'' + ] + ]. + ]. + ] ifFalse:[ + (next isLetter and:[argArrayOrDictionary isSequenceable not "is a Dictionary"]) ifTrue:[ + "so next is a non-numeric single character." + v := argArrayOrDictionary + at:next + ifAbsent:[ + "try symbol instead of character" + argArrayOrDictionary + at:next asSymbol + ifAbsent:[String with:escapeCharacter with:next]. + ]. + ] ifFalse:[ + v := String with:$% with:next. + ]. + ] + ]. + "/ v notNil ifTrue:[ + v isBlock ifTrue:[ + v := v value + ]. + + v printOn:aStream. + "/ ]. + ] + ]. + start := idx + 2 ]. " String streamContents:[:s| - 'hello %1' expandPlaceholders:$% with:#('world') on:s. - s cr. - 'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. - s cr. - 'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. - s cr. - '%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. - s cr. - '%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. - s cr. - '%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. - s cr. - '%%(10) gives %(10) %%next line' expandPlaceholders:$% with:#(123) on:s. - s cr. - '%%test gives %test' expandPlaceholders:$% with:#(123) on:s. - s cr. - '|%%|%%1|%%| gives |%|%1|%|' expandPlaceholders:$% with:#(foo) on:s. + 'hello %1' expandPlaceholders:$% with:#('world') on:s. + s cr. + 'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. + s cr. + 'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. + s cr. + '%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. + s cr. + '%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. + s cr. + '%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. + s cr. + '%%(10) gives %(10) %%next line' expandPlaceholders:$% with:#(123) on:s. + s cr. + '%%test gives %test' expandPlaceholders:$% with:#(123) on:s. + s cr. + '|%%|%%1|%%| gives |%|%1|%|' expandPlaceholders:$% with:#(foo) on:s. ] " @@ -6034,10 +6037,21 @@ dict at:$a put:'AAAAA'. dict at:$b put:[ Time now ]. String streamContents:[:s| - 'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. + 'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. ]. " + "using blocks: + |dict| + + dict := Dictionary new. + dict at:'time' put:[Time now printString]. + dict at:'date' put:[Date today printString]. + String streamContents:[:s| + 'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. + ]. + " + "Modified: / 18-11-2010 / 15:43:28 / cg" ! @@ -6046,10 +6060,13 @@ 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) + Also, the values in argArrayOrDictionary may be blocks. + 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). @@ -6094,13 +6111,17 @@ 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) + Also, the values in argArrayOrDictionary may be blocks. + 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. Use % to insert a CR and % to insert a TAB." @@ -6108,21 +6129,21 @@ " 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) %%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) %%next line' expandPlaceholdersWith:#(123) on:s. + s cr. + '%test gives %1' expandPlaceholdersWith:#(123) on:s. ] " @@ -6134,7 +6155,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. ]. " @@ -6829,7 +6850,6 @@ " ! ! - !CharacterArray methodsFor:'substring searching'! findRangeOfString:subString @@ -7433,11 +7453,11 @@ !CharacterArray class methodsFor:'documentation'! version - ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.588 2015-05-23 12:47:44 cg Exp $' + ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.589 2015-05-31 09:31:29 cg Exp $' ! version_CVS - ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.588 2015-05-23 12:47:44 cg Exp $' + ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.589 2015-05-31 09:31:29 cg Exp $' ! !