'From Squeak6.0alpha of 29 April 2021 [latest update: #20483] on 6 May 2021 at 10:24:39 pm'!

!String methodsFor: 'converting' stamp: 'ct 5/6/2021 21:56'!
withBlanksTrimmed
	"Return a copy of the receiver from which leading and trailing blanks have been trimmed."

	| first last |
	first := (self findFirst: [:character | character isSeparator not]).
	first = 0 ifTrue: [^ ''].
	"no non-separator character"
	last := self findLast: [:character | character isSeparator not].
	last = 0 ifTrue: [last := self size].
	(first = 1 and: [last = self size]) ifTrue: [^ self copy].
	^ self copyFrom: first to: last! !


!StringTest methodsFor: 'tests - converting' stamp: 'ct 5/6/2021 22:00'!
testWithBlanksTrimmed

	| s |
	self assert: ' abc  d   ' withBlanksTrimmed = 'abc  d'.
	self assert: 'abc  d   ' withBlanksTrimmed = 'abc  d'.
	self assert: ' abc  d' withBlanksTrimmed = 'abc  d'.
	
	self assert: (((0 to: 255) collect: [:each | each asCharacter] thenSelect: [:each | each isSeparator]) as: String) withBlanksTrimmed = ''.
	
	self assert: ' nbsps around ' withBlanksTrimmed = 'nbsps around'.
	
	s := 'abcd'.
	self assert: s withBlanksTrimmed = s.
	self assert: s withBlanksTrimmed ~~ s! !


!Text methodsFor: 'converting' stamp: 'ct 5/6/2021 21:57'!
withBlanksTrimmed
	"Return a copy of the receiver from which leading and trailing blanks have been trimmed."

	| first last |
	first := string findFirst: [:character | character isSeparator not].
	first = 0 ifTrue: [^ ''].
	"no non-separator character"
	last := string findLast: [:character | character isSeparator not].
	last = 0 ifTrue: [last := self size].
	(first = 1 and: [last = self size]) ifTrue: [^ self copy].
	^ self copyFrom: first to: last! !

