Login  Register

Re: self subStrings:'||'

Posted by Ron Teitelbaum on Oct 24, 2019; 5:25pm
URL: https://forum.world.st/self-subStrings-tp5105928p5105936.html

You could use my methods that never get accepted.  I use them all the time!

Collection >> explode: aDelimiter
     "explode the collection into a collection of collections broken by aDelimiter"
     "(#(#(1 2) #(3 4)) mergeDelimited: Character tab ) explode: Character tab = an OrderedCollection(#(1 2) #(3 4))
     'abcdef' explode: 'cd' = an OrderedCollection('ab' 'ef')"
     | resultCollection starting aDelimiterPosition aDelimiterSize |
     self ifEmpty: [^self].
     resultCollection := OrderedCollection new.
     aDelimiterSize := aDelimiter isCollection ifTrue: [aDelimiter size] ifFalse: [1].
     starting := 1.
     [aDelimiterPosition := aDelimiter isCollection ifTrue: [self indexOfSubCollection: aDelimiter startingAt: starting] ifFalse: [self indexOf: aDelimiter startingAt: starting ifAbsent: [0]].
     aDelimiterPosition > 0] whileTrue: [
          resultCollection add: (self copyFrom: starting to: aDelimiterPosition - 1).
          starting := aDelimiterPosition + aDelimiterSize.
     ].
     resultCollection add: (self copyFrom: starting to: self size).
     ^resultCollection

Collection >> mergeDelimited: anObject
     "return to receiver a collection with each element concatenated to remove embedded collections"
     "#(#(1 2) #(3 4)) mergeDelimited: Character tab = #(1 2 Character tab 3 4),  #('ab' 'cd') mergeDelimited: Character cr = 'ab
     cd' "
     | returnCollection aSeperator |
     self ifEmpty: [^self].
     aSeperator := anObject isCollection ifTrue: [anObject] ifFalse: [Array with: anObject].  
     returnCollection := self first species new.
     self copy from: 1 to: self size -1 do: [:a |
          a ifNotNil: [
               returnCollection := returnCollection, a, aSeperator
          ].
     ].
     ^returnCollection, self last


' Middle-left-cell || style="width: 14em;" | Middle-center-cell || Middle-right-cell' explode: $| 

an OrderedCollection(
' Middle-left-cell ' 
'' 
' style="width: 14em;" ' 
' Middle-center-cell ' 
'' 
' Middle-right-cell')

All the best,

Ron Teitelbaum

On Thu, Oct 24, 2019 at 11:38 AM gettimothy via Squeak-dev <[hidden email]> wrote:
Hi all.

' Middle-left-cell || style="width: 14em;" | Middle-center-cell || Middle-right-cell' self subStrings:'|'
' Middle-left-cell || style="width: 14em;" | Middle-center-cell || Middle-right-cell' self subStrings:'||'
inspect both, they are identical.

Is this expected behavior?

Looking at the method:
char := sourceStream next.

is the "problem" in that a match is had for the single pipe character.

Is there a better way to split that string along double pipes '||'  ?

thank you in advance.