Created
February 25, 2010 10:02
-
-
Save lilyball/314424 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Match(pattern, name string) (matched bool, err os.Error) { | |
Pattern: | |
for len(pattern) > 0 { | |
var star bool | |
var chunk string | |
star, chunk, pattern = scanChunk(pattern) | |
if star && chunk == "" { | |
// Trailing * matches rest of string unless it has a /. | |
return strings.Index(name, "/") < 0, nil | |
} | |
// Look for match at current position. | |
t, ok, err := matchChunk(chunk, name) | |
// if we're the last chunk, make sure we've exhausted the name | |
// otherwise we'll give a false result even if we could still match | |
// using the star | |
if ok && (len(t) == 0 || len(pattern) > 0) { | |
name = t | |
continue | |
} | |
if err != nil { | |
return false, err | |
} | |
if star { | |
// Look for match skipping i+1 bytes. | |
// Cannot skip /. | |
for i := 0; i < len(name) && name[i] != '/'; i++ { | |
t, ok, err := matchChunk(chunk, name[i+1:]) | |
if ok { | |
// if we're the last chunk, make sure we exhausted the name | |
if len(pattern) == 0 && len(t) > 0 { | |
continue | |
} | |
name = t | |
continue Pattern | |
} | |
if err != nil { | |
return false, err | |
} | |
} | |
} | |
return false, nil | |
} | |
return len(name) == 0, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment