[squeak-dev] The Trunk: Collections-ul.611.mcz

commits at source.squeak.org commits at source.squeak.org
Sun Apr 5 13:10:43 UTC 2015


Levente Uzonyi uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ul.611.mcz

==================== Summary ====================

Name: Collections-ul.611
Author: ul
Time: 5 April 2015, 3:02:11.144 pm
UUID: 8b7bf86a-b335-47a9-9c3b-c87148584c44
Ancestors: Collections-ul.610

- Stricter and faster implementation of String >> #format:.

=============== Diff against Collections-ul.610 ===============

Item was removed:
- ----- Method: String>>evaluateExpression:parameters: (in category 'private') -----
- evaluateExpression: aString parameters: aCollection 
- 	"private - evaluate the expression aString with  
- 	aCollection as the parameters and answer the  
- 	evaluation result as an string"
- 	| index |
- 	index := Integer readFrom: aString readStream base: 10.
- 
- 	index isZero
- 		ifTrue: [^ '[invalid subscript: {1}]' format: {aString}].
- 
- 	index > aCollection size
- 		ifTrue: [^ '[subscript is out of bounds: {1}]' format: {aString}].
- 
- 	^ (aCollection at: index) asString!

Item was changed:
  ----- Method: String>>format: (in category 'formatting') -----
  format: aCollection 
  	"format the receiver with aCollection  
  	 
  	simplest example:  
  	'foo {1} bar' format: {Date today}.
  	 
  	complete example:  
  	'\{ \} \\ foo {1} bar {2}' format: {12. 'string'}. 
  	"
+ 	^self class new: self size * 11 // 10 "+10%" streamContents: [ :output |
+ 		| lastIndex nextIndex |
+ 		lastIndex := 1.
+ 		[ (nextIndex := self indexOfAnyOf: FormatCharacterSet startingAt: lastIndex) = 0 ] whileFalse: [
+ 			nextIndex = lastIndex ifFalse: [
+ 				output next: nextIndex - lastIndex putAll: self startingAt: lastIndex ].
+ 			(self at: nextIndex) == $\
+ 				ifTrue: [ output nextPut: (self at: (nextIndex := nextIndex + 1)) ]
+ 				ifFalse: [ "${"
+ 					"Parse the index - a positive integer in base 10."
+ 					| digitValue collectionIndex |
+ 					collectionIndex := 0.
+ 					[ (digitValue := self basicAt: (nextIndex := nextIndex + 1)) between: 48 "$0 asciiValue" and: 57 "$9 asciiValue" ] whileTrue: [
+ 						collectionIndex := collectionIndex * 10 + digitValue - 48. "$0 asciiValue" ].
+ 					digitValue =  125 "$} asciiValue" ifFalse: [ self error: '$} expected' ].
+ 					output nextPutAll: (aCollection at: collectionIndex) asString ].
+ 			lastIndex := nextIndex + 1 ].
+ 		lastIndex <= self size ifTrue: [
+ 			output next: self size - lastIndex + 1 putAll: self startingAt: lastIndex ] ]!
- 	^String new: self size streamContents: [ :result |
- 		| stream currentChar |
- 		stream := self readStream.
- 		[ (currentChar := stream next) == nil ] whileFalse: [
- 			currentChar == ${
- 				ifTrue: [
- 					result nextPutAll: (
- 						self 
- 							evaluateExpression: (stream upTo: $}) withBlanksTrimmed 	
- 							parameters: aCollection) ]
- 				ifFalse: [
- 					currentChar == $\
- 						ifFalse: [ result nextPut: currentChar ]
- 						ifTrue: [
- 							(currentChar := stream next) ifNotNil: [
- 								result nextPut: currentChar ] ] ] ] ]!



More information about the Squeak-dev mailing list