another go at removing redundant code from common

This commit is contained in:
rupa committed 2011-08-03 19:12:22 -04:00
1 parent 1870f27162
commit 476d469783
1 file changed
+7 -9
+7 -9
View File
@@ -127,17 +127,15 @@ _z() {
print toopen print toopen
} }
} }
function common(matches, fnd, nc) { function common(matches) {
# shortest match
for( i in matches ) { for( i in matches ) {
if( matches[i] && (!short || length(i) < length(short)) ) short = i if( matches[i] && (!short || length(i) < length(short)) ) short = i
} }
if( short == "/" ) return if( short == "/" ) return
for( i in matches ) if( matches[i] && i !~ short ) x = 1 # shortest match must be common to each match
if( x ) return for( i in matches ) if( matches[i] && i !~ short ) return
if( nc ) { return short
for( i in fnd ) if( tolower(short) !~ tolower(fnd[i]) ) x = 1
} else for( i in fnd ) if( short !~ fnd[i] ) x = 1
if( !x ) return short
} }
BEGIN { split(q, a, " ") } BEGIN { split(q, a, " ") }
{ {
@@ -162,8 +160,8 @@ _z() {
} }
END { END {
if( cx ) { if( cx ) {
output(wcase, cx, common(wcase, a, 0)) output(wcase, cx, common(wcase))
} else if( ncx ) output(nocase, ncx, common(nocase, a, 1)) } else if( ncx ) output(nocase, ncx, common(nocase))
} }
' "$datafile")" ' "$datafile")"
[ $? -gt 0 ] && return [ $? -gt 0 ] && return