[Enh] string substitution methods

Stephan B. Wessels stephan.wessels at sdrc.com
Wed Nov 10 15:27:39 UTC 1999


--------------78E6C14BB814C517D9A68431
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


This change set provides a simple extension to String which allows easy
substitution of tokens in a string.  For example,

'Error: <#1> has happened on workstation <#2>.  Please restart workstation
<#2>.' substituteParameters: #('32' 'Alpha').

Causes the tokens identified by <#n> to be replaced with the proper tags.
Result would be as follows:

 'Error: 32 has happened on workstation Alpha.  Please restart workstation
Alpha.'

I hope someone besides me finds this use full.

  - Steve


--------------78E6C14BB814C517D9A68431
Content-Type: text/plain; charset=us-ascii;
 name="string functions_24Oct1115am.cs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="string functions_24Oct1115am.cs"


'From Squeak2.6 of 11 October 1999 [latest update: #1559] on 24 October 1999 at 11:15:43 am'!

!String methodsFor: 'substitution'!
substituteParameters: aList

	"...

		'Error: <#1> has happened on workstation <#2>.  Please restart workstation <#2>.' substituteParameters: #('32' 'Alpha').

	..."



	| dict param |

	dict _ Dictionary new.

	1 to: aList size do: [:index |

		param _ (aList at: index) printString.

		dict at: index printString put: param].

	^self substituteParameters: dict startToken: '<#' endToken: '>'! !

!String methodsFor: 'substitution'!
substituteParameters: aList startToken: startTokenString endToken: endTokenString

	"...

		paramDict _ Dictionary new.

		paramDict at: 'foo' put: 32 printString.

		paramDict at: '2' put: 'Alpha'.

		'Error: <#foo> has happened on workstation <#2>.  Please restart workstation <#2>.' substituteParameters: paramDict startToken: '<#' endToken: '>'.

	..."



	| index limit dict deltaStartToken deltaEndToken startPos endPos paramKey array stream pair pKey ePos value |



	self isEmpty ifTrue: [^self].

	index _ 1.

	limit _ self size.

	dict _ Dictionary new.

	deltaStartToken _ startTokenString size.

	deltaEndToken _ endTokenString size.

	[startPos _ self findAnySubStr: (Array with: startTokenString) startingAt: index.

	startPos > limit] whileFalse: [

		index _ startPos + deltaStartToken.

		endPos _ self findAnySubStr: endTokenString startingAt: index.

		paramKey _ self copyFrom: index to: (endPos - deltaEndToken).

		array _ Array with: paramKey with: endPos.

		dict at: startPos put: array.

		index _ endPos + 1].

	stream _ ReadWriteStream on: ''.

	index _ 1.

	dict keys asSortedCollection do: [:key |

		pair _ dict at: key.

		pKey _ pair at: 1.

		ePos _ pair at: 2.

		key > index ifTrue: [stream nextPutAll: (self copyFrom: index to: (key - 1))].

		value _ aList at: pKey.

		stream nextPutAll: value.

		index _ ePos + 1].

	index <= limit ifTrue: [stream nextPutAll: (self copyFrom: index to: limit)].

	^stream contents! !



--------------78E6C14BB814C517D9A68431--





More information about the Squeak-dev mailing list