CharacterArray.st
changeset 23067 a9b2aa8f652a
parent 23061 82d596e2eb9c
child 23069 27007bdd5c17
equal deleted inserted replaced
23066:caf3599f703e 23067:a9b2aa8f652a
  6586      See also bindWith:... for VisualAge compatibility."
  6586      See also bindWith:... for VisualAge compatibility."
  6587 
  6587 
  6588     |stream|
  6588     |stream|
  6589 
  6589 
  6590     stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20).
  6590     stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20).
  6591     self expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:stream.
  6591     self expandPlaceholders:escapeCharacter with:argArrayOrDictionary ignoreNumericEscapes:false on:stream.
  6592     ^ stream contents.
  6592     ^ stream contents.
  6593 
  6593 
  6594 
  6594 
  6595     "
  6595     "
  6596      'hello %1' expandPlaceholdersWith:#('world')
  6596      'hello %1' expandPlaceholdersWith:#('world')
  6615      dict at:'foo' put:[ Date today ].
  6615      dict at:'foo' put:[ Date today ].
  6616      'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict.
  6616      'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict.
  6617      'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict.
  6617      'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict.
  6618     "
  6618     "
  6619 
  6619 
  6620     "Modified: 1.7.1997 / 00:53:24 / cg"
  6620     "Modified: / 01-07-1997 / 00:53:24 / cg"
  6621 !
  6621     "Modified: / 14-06-2018 / 11:46:18 / Claus Gittinger"
  6622 
  6622 !
  6623 expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream
  6623 
       
  6624 expandPlaceholders:escapeCharacter with:argArrayOrDictionary ignoreNumericEscapes:ignoreNumericEscapes on:aStream
  6624     "this is the generic version of the old %-escaping method, allowing for an arbitrary
  6625     "this is the generic version of the old %-escaping method, allowing for an arbitrary
  6625      escape character to be used (typically $$ or $% are effectively used).
  6626      escape character to be used (typically $$ or $% are effectively used).
  6626 
  6627 
  6627      Write the receiver to aStream, where all %i escapes are
  6628      Write the receiver to aStream, where all %i escapes are
  6628      replaced by corresponding arguments' printStrings from the argArrayOrDictionary.
  6629      replaced by corresponding arguments' printStrings from the argArray.
  6629      I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results
  6630      I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results
  6630      in the new string 'hello world; how is this'.
  6631      in the new string 'hello world; how is this'.
  6631 
  6632 
  6632      As an extension, the argument may also be a dictionary, providing values for symbolic keys.
  6633      As an extension, the argument may also be a dictionary, providing values for symbolic keys.
  6633      In this case, %a .. %z and %(...) are also allowed.
  6634      In this case, %a .. %z and %(...) are also allowed.
  6634      (%1..%9 require a numeric key in the dictionary, however)
  6635      (%1..%9 require a numeric key in the dictionary, however)
  6635      Also, the values in argArrayOrDictionary may be blocks.
  6636 
       
  6637      Also, values in argArrayOrDictionary may be blocks.
  6636 
  6638 
  6637      To get a '%' character, use a '%%'-escape.
  6639      To get a '%' character, use a '%%'-escape.
  6638      To get an integer-indexed placeHolder followed by another digit,
  6640      To get an integer-indexed placeHolder followed by another digit,
  6639      or an index > 9, you must use %(digit).
  6641      or an index > 9, you must use %(digit).
  6640 
  6642 
  6641      See also bindWith:... for VisualAge compatibility.
  6643      See also bindWith:... for VisualAge compatibility.
  6642      Use %<cr> to insert a CR and %<tab> to insert a TAB."
  6644      Use %<cr> to insert a CR and %<tab> to insert a TAB.
       
  6645 
       
  6646      ignoreNumericEscapes controls if %<nr> escapes are expanded or not.
       
  6647      This is required for Windows batch-script expansion, where %<nr> should be left
       
  6648      unchanged."
  6643 
  6649 
  6644     |next v key numericKey
  6650     |next v key numericKey
  6645      idx   "{ SmallInteger }"
  6651      idx   "{ SmallInteger }"
  6646      idx2  "{ SmallInteger }"
  6652      idx2  "{ SmallInteger }"
  6647      start "{ SmallInteger }"
  6653      start "{ SmallInteger }"
  6769      String streamContents:[:s|
  6775      String streamContents:[:s|
  6770          'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s.
  6776          'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s.
  6771      ].
  6777      ].
  6772     "
  6778     "
  6773 
  6779 
       
  6780     "Created: / 14-06-2018 / 11:44:08 / Claus Gittinger"
       
  6781 !
       
  6782 
       
  6783 expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream
       
  6784     "this is the generic version of the old %-escaping method, allowing for an arbitrary
       
  6785      escape character to be used (typically $$ or $% are effectively used).
       
  6786 
       
  6787      Write the receiver to aStream, where all %i escapes are
       
  6788      replaced by corresponding arguments' printStrings from the argArrayOrDictionary.
       
  6789      I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results
       
  6790      in the new string 'hello world; how is this'.
       
  6791 
       
  6792      As an extension, the argument may also be a dictionary, providing values for symbolic keys.
       
  6793      In this case, %a .. %z and %(...) are also allowed.
       
  6794      (%1..%9 require a numeric key in the dictionary, however)
       
  6795      Also, the values in argArrayOrDictionary may be blocks.
       
  6796 
       
  6797      To get a '%' character, use a '%%'-escape.
       
  6798      To get an integer-indexed placeHolder followed by another digit,
       
  6799      or an index > 9, you must use %(digit).
       
  6800 
       
  6801      See also bindWith:... for VisualAge compatibility.
       
  6802      Use %<cr> to insert a CR and %<tab> to insert a TAB."
       
  6803 
       
  6804     ^ self
       
  6805         expandPlaceholders:escapeCharacter 
       
  6806         with:argArrayOrDictionary 
       
  6807         ignoreNumericEscapes:false 
       
  6808         on:aStream
       
  6809 
       
  6810     "
       
  6811      String streamContents:[:s|
       
  6812         'hello %1' expandPlaceholders:$% with:#('world') on:s.
       
  6813         s cr.
       
  6814         'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s.
       
  6815         s cr.
       
  6816         'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s.
       
  6817         s cr.
       
  6818         '%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s.
       
  6819         s cr.
       
  6820         '%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s.
       
  6821         s cr.
       
  6822         '%%10 gives %10' expandPlaceholders:$% with:#(123) on:s.
       
  6823         s cr.
       
  6824         '%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s.
       
  6825         s cr.
       
  6826         '%%test gives %test' expandPlaceholders:$% with:#(123) on:s.
       
  6827         s cr.
       
  6828         '|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s.
       
  6829      ]
       
  6830     "
       
  6831 
       
  6832     "
       
  6833      |dict|
       
  6834 
       
  6835      dict := Dictionary new.
       
  6836      dict at:1 put:'one'.
       
  6837      dict at:$a put:'AAAAA'.
       
  6838      dict at:$b put:[ Time now ].
       
  6839      String streamContents:[:s|
       
  6840          'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s.
       
  6841      ].
       
  6842     "
       
  6843 
       
  6844     "using blocks:
       
  6845      |dict|
       
  6846 
       
  6847      dict := Dictionary new.
       
  6848      dict at:'time' put:[Time now printString].
       
  6849      dict at:'date' put:[Date today printString].
       
  6850      String streamContents:[:s|
       
  6851          'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s.
       
  6852      ].
       
  6853     "
       
  6854 
  6774     "Modified: / 18-11-2010 / 15:43:28 / cg"
  6855     "Modified: / 18-11-2010 / 15:43:28 / cg"
       
  6856     "Modified: / 14-06-2018 / 11:44:32 / Claus Gittinger"
  6775 !
  6857 !
  6776 
  6858 
  6777 expandPlaceholdersWith:argArrayOrDictionary
  6859 expandPlaceholdersWith:argArrayOrDictionary
  6778     "return a copy of the receiver, where all %i escapes are
  6860     "return a copy of the receiver, where all %i escapes are
  6779      replaced by corresponding arguments' printStrings from the argArrayOrDictionary.
  6861      replaced by corresponding arguments' printStrings from the argArrayOrDictionary.
  6845      or an index > 9, you must use %(digit).
  6927      or an index > 9, you must use %(digit).
  6846 
  6928 
  6847      See also bindWith:... for VisualAge compatibility.
  6929      See also bindWith:... for VisualAge compatibility.
  6848      Use %<cr> to insert a CR and %<tab> to insert a TAB."
  6930      Use %<cr> to insert a CR and %<tab> to insert a TAB."
  6849 
  6931 
  6850     ^ self expandPlaceholders:$% with:argArrayOrDictionary on:aStream
  6932     ^ self expandPlaceholders:$% with:argArrayOrDictionary ignoreNumericEscapes:false on:aStream
  6851 
  6933 
  6852     "
  6934     "
  6853      String streamContents:[:s|
  6935      String streamContents:[:s|
  6854         'hello %1' expandPlaceholdersWith:#('world') on:s.
  6936         'hello %1' expandPlaceholdersWith:#('world') on:s.
  6855         s cr.
  6937         s cr.
  6880          'hello %1 %a %b' expandPlaceholdersWith:dict on:s.
  6962          'hello %1 %a %b' expandPlaceholdersWith:dict on:s.
  6881      ].
  6963      ].
  6882     "
  6964     "
  6883 
  6965 
  6884     "Modified: / 18-11-2010 / 15:43:28 / cg"
  6966     "Modified: / 18-11-2010 / 15:43:28 / cg"
       
  6967     "Modified: / 14-06-2018 / 11:45:36 / Claus Gittinger"
  6885 !
  6968 !
  6886 
  6969 
  6887 extractPlaceHolders:escapeCharacter
  6970 extractPlaceHolders:escapeCharacter
  6888     "returns a list of placeholder keys of the form %i,
  6971     "returns a list of placeholder keys of the form %i,
  6889      where i is either a single digit (as in %1, %2)
  6972      where i is either a single digit (as in %1, %2)