[Pkg] The Trunk: Collections-nice.194.mcz

commits at source.squeak.org commits at source.squeak.org
Mon Nov 16 19:51:47 UTC 2009


Nicolas Cellier uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-nice.194.mcz

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

Name: Collections-nice.194
Author: nice
Time: 16 November 2009, 8:51:26 am
UUID: e20ffd97-6350-4cf9-9424-d1576bc1ffb2
Ancestors: Collections-nice.193

Avoid an off end line index
Add cr/lf/crlf support for one more message (with speed up versus simple cr version...)


=============== Diff against Collections-nice.193 ===============

Item was changed:
  ----- Method: String>>withNoLineLongerThan: (in category 'converting') -----
  withNoLineLongerThan: aNumber
  	"Answer a string with the same content as receiver, but rewrapped so that no line has more characters than the given number"
- 	| listOfLines currentLast currentStart resultString putativeLast putativeLine crPosition |
  	aNumber isNumber not | (aNumber < 1) ifTrue: [self error: 'too narrow'].
+ 	^self class
+ 		new: self size * (aNumber + 1) // aNumber "provision for supplementary line breaks"
+ 		streamContents: [ :stream |
+ 			self lineIndicesDo: [ :start :endWithoutDelimiters :end | | lineStart |
+ 				lineStart := self indexOfAnyOf: CSNonSeparators startingAt: start ifAbsent: [endWithoutDelimiters + 1].
+ 				[| lineEnd spacePosition |
+ 				lineEnd := 0.
+ 				spacePosition := lineStart.
+ 				[spacePosition := self indexOfAnyOf: CSSeparators startingAt: spacePosition + 1 ifAbsent: [lineStart + aNumber + 1].
+ 				spacePosition - lineStart <= aNumber]
+ 						whileTrue: [lineEnd := spacePosition].
+ 				lineEnd = 0
+ 					ifTrue: ["no space - split arbitrarily"
+ 						lineEnd := lineStart + aNumber - 1 min: endWithoutDelimiters.
+ 						stream nextPutAll: (self copyFrom: lineStart to: lineEnd).
+ 						lineStart := lineEnd + 1]
+ 					ifFalse: ["split before space"
+ 						stream nextPutAll: (self copyFrom: lineStart to: lineEnd - 1).
+ 						"eliminate conscutive spaces at split"
+ 						lineStart := self indexOfAnyOf: CSNonSeparators startingAt: lineEnd + 1 ifAbsent: [endWithoutDelimiters + 1] ].
+ 				lineStart <= endWithoutDelimiters ]
+ 					whileTrue: [stream cr].
+ 				stream nextPutAll: (self copyFrom: endWithoutDelimiters + 1 to: end) ] ]!
- 	listOfLines := OrderedCollection new.
- 	currentLast := 0.
- 	[currentLast < self size] whileTrue:
- 		[currentStart := currentLast + 1.
- 		putativeLast := (currentStart + aNumber - 1) min: self size.
- 		putativeLine := self copyFrom: currentStart to: putativeLast.
- 		(crPosition := putativeLine indexOf: Character cr) > 0 ifTrue:
- 			[putativeLast := currentStart + crPosition - 1.
- 			putativeLine := self copyFrom: currentStart to: putativeLast].
- 		currentLast := putativeLast == self size
- 			ifTrue:
- 				[putativeLast]
- 			ifFalse:
- 				[currentStart + putativeLine lastSpacePosition - 1].
- 		currentLast <= currentStart ifTrue:
- 			["line has NO spaces; baleout!!"
- 			currentLast := putativeLast].
- 		listOfLines add: (self copyFrom: currentStart to: currentLast) withBlanksTrimmed].
- 
- 	listOfLines size > 0 ifFalse: [^ ''].
- 	resultString := listOfLines first.
- 	2 to: listOfLines size do:
- 		[:i | resultString := resultString, String cr, (listOfLines at: i)].
- 	^ resultString
- 
- "#(5 7 20) collect:
- 	[:i | 'Fred the bear went down to the brook to read his book in silence' withNoLineLongerThan: i]"!

Item was changed:
  ----- Method: String>>lineIndicesDo: (in category 'accessing') -----
  lineIndicesDo: aBlock
  	"execute aBlock with 3 arguments for each line:
  	- start index of line
  	- end index of line without line delimiter
  	- end index of line including line delimiter(s) CR, LF or CRLF"
  	
  	| start end endWithoutDelimiters |
  	start := 1.
  	[ start <= self size ] whileTrue: [
+ 		end := self indexOfAnyOf: CSLineEnders startingAt: start ifAbsent: [ 0 ].
+ 		end = 0
+ 			ifTrue: [endWithoutDelimiters := end := self size]
+ 			ifFalse: [endWithoutDelimiters := end - 1.
+ 				(end < self size
+ 					and: [(self at: end + 1) = Character lf
+ 					and: [(self at: end)	= Character cr]])
+ 				ifTrue: [end := end + 1]].
- 		end := self indexOfAnyOf: CSLineEnders startingAt: start ifAbsent: [ self size + 1 ].
- 		endWithoutDelimiters := end - 1.
- 		(end < self size
- 				and: [(self at: end + 1) = Character lf
- 				and: [(self at: end)	= Character cr]])
- 			ifTrue: [end := end + 1].
  		aBlock value: start value: endWithoutDelimiters value: end.
  		start := end + 1]!



More information about the Packages mailing list