Skip to content

Commit

Permalink
another go at removing redundant code from common
Browse files Browse the repository at this point in the history
  • Loading branch information
rupa committed Aug 3, 2011
1 parent 1870f27 commit 476d469
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions z.sh
Expand Up @@ -127,17 +127,15 @@ _z() {
print toopen
}
}
function common(matches, fnd, nc) {
function common(matches) {
# shortest match
for( i in matches ) {
if( matches[i] && (!short || length(i) < length(short)) ) short = i
}
if( short == "/" ) return
for( i in matches ) if( matches[i] && i !~ short ) x = 1
if( x ) return
if( nc ) {
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
# shortest match must be common to each match
for( i in matches ) if( matches[i] && i !~ short ) return
return short
}
BEGIN { split(q, a, " ") }
{
Expand All @@ -162,8 +160,8 @@ _z() {
}
END {
if( cx ) {
output(wcase, cx, common(wcase, a, 0))
} else if( ncx ) output(nocase, ncx, common(nocase, a, 1))
output(wcase, cx, common(wcase))
} else if( ncx ) output(nocase, ncx, common(nocase))
}
' "$datafile")"
[ $? -gt 0 ] && return
Expand Down

0 comments on commit 476d469

Please sign in to comment.