[Pkg] The Trunk: Collections-nice.434.mcz
commits at source.squeak.org
commits at source.squeak.org
Fri Mar 25 22:03:21 UTC 2011
Nicolas Cellier uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-nice.434.mcz
==================== Summary ====================
Name: Collections-nice.434
Author: nice
Time: 25 March 2011, 11:02:55.749 pm
UUID: 73a1fe72-b312-4586-aff0-f7fd557cc3fc
Ancestors: Collections-ul.433
Fix a remaining problem for:
String crlf withNoLineLongerThan: 5
While at it, simplify a bit the logic.
=============== Diff against Collections-ul.433 ===============
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"
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 |
+ | pastEnd lineStart |
+ pastEnd := endWithoutDelimiters + 1.
+ "eliminate spaces at beginning of line"
+ lineStart := (self indexOfAnyOf: CSNonSeparators startingAt: start ifAbsent: [pastEnd]) min: pastEnd.
- self lineIndicesDo: [ :start :endWithoutDelimiters :end | | lineStart |
- lineStart := (self indexOfAnyOf: CSNonSeparators startingAt: start ifAbsent: [endWithoutDelimiters + 1]) min: endWithoutDelimiters.
[| lineStop lineEnd spacePosition |
+ lineEnd := lineStop := lineStart + aNumber min: pastEnd..
- lineEnd := 0.
spacePosition := lineStart.
+ [spacePosition < lineStop] whileTrue: [
+ spacePosition := self indexOfAnyOf: CSSeparators startingAt: spacePosition + 1 ifAbsent: [pastEnd].
+ spacePosition <= lineStop ifTrue: [lineEnd := spacePosition].
- lineStop := (lineStart + aNumber - 1) min: endWithoutDelimiters.
- [spacePosition <= lineStop] whileTrue:[
- spacePosition := self indexOfAnyOf: CSSeparators startingAt: spacePosition + 1 ifAbsent: [endWithoutDelimiters+1].
- spacePosition <= (lineStop+1) ifTrue:[lineEnd := spacePosition].
].
+ "split before space or before lineStop if no space"
+ stream nextPutAll: (self copyFrom: lineStart to: lineEnd - 1).
+ "eliminate spaces at beginning of next line"
+ lineStart := self indexOfAnyOf: CSNonSeparators startingAt: lineEnd ifAbsent: [pastEnd].
- 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: pastEnd to: end) ] ]!
- stream nextPutAll: (self copyFrom: endWithoutDelimiters + 1 to: end) ] ]!
More information about the Packages
mailing list