'Match this item against request (uses GET vars)
@param request: the request object (defaults to current.request)
@return: the match level (integer):
0=no match
1=controller
2=controller+function
3=controller+function+args
4=controller+function+args+vars
@note: currently ignores numerical arguments in the request,
which is though subject to change (in order to support
numerical arguments in the item)'

def match(self, request=None):

    level = 0
    args = self.args
    link_vars = self.vars
    if ((self.application is not None) and (self.application != request.application)):
        return 0
    if (self.opts.selectable is False):
        return 0
    check = self.check_hook()
    if check:
        enabled = self.check_enabled()
        if (not enabled):
            check = False
    if (not check):
        return 0
    if (request is None):
        request = current.request
    c = self.get('controller')
    mc = self.get('match_controller')
    if ((not c) and (self.parent is None)):
        return 1
    rvars = request.get_vars
    controller = request.controller
    function = request.function
    if ('viewing' in rvars):
        try:
            (tn, record_id) = rvars['viewing'].split('.')
            (controller, function) = tn.split('_', 1)
        except:
            pass
    if ((controller == c) or (controller in mc)):
        level = 1
    if (level == 1):
        f = self.get('function')
        mf = self.get('match_function')
        if ((function == f) or (function in mf)):
            level = 2
        elif (f == 'index'):
            return 1
        elif (f is not None):
            return 0
    if (level == 2):
        extra = 1
        for (k, v) in link_vars.iteritems():
            if ((k not in rvars) or ((k in rvars) and (rvars[k] != s3_unicode(v)))):
                extra = 0
                break
            else:
                extra = 2
        rargs = request.args
        if rargs:
            if args:
                largs = [a for a in request.args if (not a.isdigit())]
                if ((len(args) == len(largs)) and all([(args[i] == largs[i]) for i in xrange(len(args))])):
                    level = 5
                elif ((len(rargs) >= len(args) > 0) and rargs[(len(args) - 1)].isdigit() and (not str(args[(-1)]).isdigit())):
                    level = 1
                else:
                    return 0
            else:
                level = 3
        elif args:
            return 0
        else:
            level = 5
        level += extra
    return level
