better longestCommonPrefix; added longestCommonSuffix
authorClaus Gittinger <cg@exept.de>
Sat, 13 Apr 2002 14:47:35 +0200
changeset 6505 a86bdc3d8e66
parent 6504 8001aa813fac
child 6506 6239f9a0c012
better longestCommonPrefix; added longestCommonSuffix
Collection.st
--- a/Collection.st	Sat Apr 13 14:41:59 2002 +0200
+++ b/Collection.st	Sat Apr 13 14:47:35 2002 +0200
@@ -2110,45 +2110,81 @@
 !
 
 longestCommonPrefixIgnoreCase:ignoreCase
-    "return the longest common prefix of my elements.
-     Typically used with string collections."
-
-    |try allMatching matchLen|
-
-    "
-     find the longest common prefix
-    "
-    matchLen := 0.
-    self do:[:aName |
-        aName size > matchLen ifTrue:[
-            matchLen := aName size.
-            try := aName
+    "return the longest common prefix of my elements (which must be sequenceableCollections).
+     Typically used with string collections, 
+     especially with completion of selectors or filenames."
+
+    |longest|
+
+    self do:[:eachCollection |
+        longest isNil ifTrue:[
+            longest := eachCollection
+        ] ifFalse:[
+            longest := longest commonPrefixWith:eachCollection ignoreCase:ignoreCase
         ]
     ].
-
-    allMatching := true.
-
-    [true] whileTrue:[
-        allMatching := true.
-        self do:[:aName |
-            ((ignoreCase not and:[aName startsWith:try])
-            or:[ignoreCase and:[aName asLowercase startsWith:try asLowercase]]) ifFalse:[
-                allMatching := false
-            ]
-        ].
-        allMatching ifTrue:[
-            ^ try
-        ].
-        matchLen := matchLen - 1.
-        matchLen == 0 ifTrue:[^ ''].
-        try := try copyTo:matchLen.
-    ].
-    ^ try
+    ^ longest.
+
+"/    |longest try allMatching matchLen|
+"/
+"/    "
+"/     find the longest common prefix
+"/    "
+"/    matchLen := 0.
+"/    self do:[:aName |
+"/        aName size > matchLen ifTrue:[
+"/            matchLen := aName size.
+"/            try := aName
+"/        ]
+"/    ].
+"/
+"/    allMatching := true.
+"/
+"/    [true] whileTrue:[
+"/        allMatching := true.
+"/        self do:[:aName |
+"/            ((ignoreCase not and:[aName startsWith:try])
+"/            or:[ignoreCase and:[aName asLowercase startsWith:try asLowercase]]) ifFalse:[
+"/                allMatching := false
+"/            ]
+"/        ].
+"/        allMatching ifTrue:[
+"/            ^ try
+"/        ].
+"/        matchLen := matchLen - 1.
+"/        matchLen == 0 ifTrue:[^ ''].
+"/        try := try copyTo:matchLen.
+"/    ].
+"/    ^ try
 
     "
      #('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:true 
      #('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false 
      #('Array' 'ArayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false   
+     #('AAA' 'A11' 'AA2') longestCommonPrefixIgnoreCase:false   
+     #('AAA' 'BBB' 'CCC') longestCommonPrefixIgnoreCase:false   
+    "
+!
+
+longestCommonSuffixIgnoreCase:ignoreCase
+    "return the longest common suffix (tail) of my elements
+     (which must be sequenceableCollections)."
+
+    |longest|
+
+    self do:[:eachCollection |
+        longest isNil ifTrue:[
+            longest := eachCollection
+        ] ifFalse:[
+            longest := longest commonSuffixWith:eachCollection ignoreCase:ignoreCase
+        ]
+    ].
+    ^ longest.
+
+    "
+     #('Array' 'ByteArray' 'BigArray') longestCommonSuffixIgnoreCase:true 
+     #('AAA' 'BBBAA' 'CCCAAAA') longestCommonSuffixIgnoreCase:false       
+     #('AAA' 'BBB' 'CCC') longestCommonSuffixIgnoreCase:false             
     "
 !
 
@@ -2337,6 +2373,6 @@
 !Collection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.120 2002-02-25 20:02:32 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.121 2002-04-13 12:47:35 cg Exp $'
 ! !
 Collection initialize!