2108 |
2108 |
2109 "Modified: 2.3.1997 / 00:21:41 / cg" |
2109 "Modified: 2.3.1997 / 00:21:41 / cg" |
2110 ! |
2110 ! |
2111 |
2111 |
2112 longestCommonPrefixIgnoreCase:ignoreCase |
2112 longestCommonPrefixIgnoreCase:ignoreCase |
2113 "return the longest common prefix of my elements. |
2113 "return the longest common prefix of my elements (which must be sequenceableCollections). |
2114 Typically used with string collections." |
2114 Typically used with string collections, |
2115 |
2115 especially with completion of selectors or filenames." |
2116 |try allMatching matchLen| |
2116 |
2117 |
2117 |longest| |
2118 " |
2118 |
2119 find the longest common prefix |
2119 self do:[:eachCollection | |
2120 " |
2120 longest isNil ifTrue:[ |
2121 matchLen := 0. |
2121 longest := eachCollection |
2122 self do:[:aName | |
2122 ] ifFalse:[ |
2123 aName size > matchLen ifTrue:[ |
2123 longest := longest commonPrefixWith:eachCollection ignoreCase:ignoreCase |
2124 matchLen := aName size. |
|
2125 try := aName |
|
2126 ] |
2124 ] |
2127 ]. |
2125 ]. |
2128 |
2126 ^ longest. |
2129 allMatching := true. |
2127 |
2130 |
2128 "/ |longest try allMatching matchLen| |
2131 [true] whileTrue:[ |
2129 "/ |
2132 allMatching := true. |
2130 "/ " |
2133 self do:[:aName | |
2131 "/ find the longest common prefix |
2134 ((ignoreCase not and:[aName startsWith:try]) |
2132 "/ " |
2135 or:[ignoreCase and:[aName asLowercase startsWith:try asLowercase]]) ifFalse:[ |
2133 "/ matchLen := 0. |
2136 allMatching := false |
2134 "/ self do:[:aName | |
2137 ] |
2135 "/ aName size > matchLen ifTrue:[ |
2138 ]. |
2136 "/ matchLen := aName size. |
2139 allMatching ifTrue:[ |
2137 "/ try := aName |
2140 ^ try |
2138 "/ ] |
2141 ]. |
2139 "/ ]. |
2142 matchLen := matchLen - 1. |
2140 "/ |
2143 matchLen == 0 ifTrue:[^ '']. |
2141 "/ allMatching := true. |
2144 try := try copyTo:matchLen. |
2142 "/ |
2145 ]. |
2143 "/ [true] whileTrue:[ |
2146 ^ try |
2144 "/ allMatching := true. |
|
2145 "/ self do:[:aName | |
|
2146 "/ ((ignoreCase not and:[aName startsWith:try]) |
|
2147 "/ or:[ignoreCase and:[aName asLowercase startsWith:try asLowercase]]) ifFalse:[ |
|
2148 "/ allMatching := false |
|
2149 "/ ] |
|
2150 "/ ]. |
|
2151 "/ allMatching ifTrue:[ |
|
2152 "/ ^ try |
|
2153 "/ ]. |
|
2154 "/ matchLen := matchLen - 1. |
|
2155 "/ matchLen == 0 ifTrue:[^ '']. |
|
2156 "/ try := try copyTo:matchLen. |
|
2157 "/ ]. |
|
2158 "/ ^ try |
2147 |
2159 |
2148 " |
2160 " |
2149 #('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:true |
2161 #('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:true |
2150 #('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
2162 #('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
2151 #('Array' 'ArayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
2163 #('Array' 'ArayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
|
2164 #('AAA' 'A11' 'AA2') longestCommonPrefixIgnoreCase:false |
|
2165 #('AAA' 'BBB' 'CCC') longestCommonPrefixIgnoreCase:false |
|
2166 " |
|
2167 ! |
|
2168 |
|
2169 longestCommonSuffixIgnoreCase:ignoreCase |
|
2170 "return the longest common suffix (tail) of my elements |
|
2171 (which must be sequenceableCollections)." |
|
2172 |
|
2173 |longest| |
|
2174 |
|
2175 self do:[:eachCollection | |
|
2176 longest isNil ifTrue:[ |
|
2177 longest := eachCollection |
|
2178 ] ifFalse:[ |
|
2179 longest := longest commonSuffixWith:eachCollection ignoreCase:ignoreCase |
|
2180 ] |
|
2181 ]. |
|
2182 ^ longest. |
|
2183 |
|
2184 " |
|
2185 #('Array' 'ByteArray' 'BigArray') longestCommonSuffixIgnoreCase:true |
|
2186 #('AAA' 'BBBAA' 'CCCAAAA') longestCommonSuffixIgnoreCase:false |
|
2187 #('AAA' 'BBB' 'CCC') longestCommonSuffixIgnoreCase:false |
2152 " |
2188 " |
2153 ! |
2189 ! |
2154 |
2190 |
2155 size |
2191 size |
2156 "return the number of elements in the receiver. |
2192 "return the number of elements in the receiver. |