[ENH] Collection>>includesSubStringAnywhere:

Richard A. O'Keefe ok at atlas.otago.ac.nz
Fri Apr 27 03:11:08 UTC 2001


Collection currently includes

   includesSubstringAnywhere: testString
      "Answer whether the receiver includes, anywhere in its nested
       structure, a string that has testString as a substring"
      self do:
         [:element |
            (element isKindOf: String)
               ifTrue:
                  [(element includesSubString: testString) ifTrue: [^true]].
            (element isKindOf: Collection)
               ifTrue:
                  [(element includesSubstringAnywhere: testString)
                                                         ifTrue: [^true]]].
       ^false

I find it somewhat confusing that we have
    substrings
    findSubstring:in:startingAt:matchTable:
    includesSubstringAnywhere:
    includesSubstring:caseSensitive:
but includesSubString:
               ^
That is the ONLY method which spells "Substring" with an internal capital.
Part 1 of this enhancement is to add a copy of the includesSubString message,
spelled consistently.

The main point is that anything which is a kind of String is thereby a
kind of Collection, but there is no point in checking a bunch of
Characters to see if they are Strings or Collections.  It turns out that
I have a use for this method.  So part 2 of this enhancement is to skip
the Collection check for Strings.

This changeset should also work in 2.7 and 2.8.

'From Squeak3.0 of 4 February 2001 [latest update: #3552] on 27 April 2001 at 2:53:08 pm'!

!Collection methodsFor: 'testing' stamp: 'raok 4/27/2001 14:49'!
includesSubstringAnywhere: testString 
	"Answer whether the receiver includes, anywhere in its nested  
	structure, a string that has testString as a substring"
	self
		do: [:element | (element isKindOf: String)
				ifTrue: [(element includesSubString: testString)
						ifTrue: [^ true]]
				ifFalse: [(element isKindOf: Collection)
						ifTrue: [(element includesSubstringAnywhere: testString)
								ifTrue: [^ true]]]].
	^ false
     "#(first (second third) ((allSentMessages ('Elvis' includes:))))  
	includesSubstringAnywhere: 'lvi'"! !


!String methodsFor: 'accessing' stamp: 'raok 4/27/2001 14:43'!
includesSubstring: subString
	^ (self findString: subString startingAt: 1) > 0! !





More information about the Squeak-dev mailing list