import re


def _match (term, name, case_sensitive, whole_name, regex):
    """Used by search_tree to check if a name matches.

_match(term, name, case_sensitive, whole_name, regex) -> matches

term, case_sensitive, whole_name, regex: as taken by search_tree.
name: the file/directory name to match against.

matches: whether name is a match for term.

If case_sensitive is False, term should be lower-case.

"""
    if regex:
        flags = 0 if case_sensitive else re.I
        if whole_name:
            match = re.match(term, name, flags)
            return match is not None and match.end() == len(name)
        else:
            return re.search(term, name, flags) is not None
    else:
        if not case_sensitive:
            name = name.lower()
        if whole_name:
            return term == name
        else:
            return name.find(term) != -1