def update(res, field, desired_flavor, detected_flavors):
    """Process RES by matching FIELD.attrib['name'] against RES.PATTERN.
    If there is a match M, M.group(2) must evaluate to the name of the
    flavor of the field.  If this flavor matches the specified flavor,
    we have an exact match and RES.value is set to field.text, RES.name
    is set to field.attrib['name'].
    If the flavor of the field is empty and RES.value is None we have a
    default match and RES is updated like for an exact match.
    DETECTED_FLAVOR is a set of flavors found.
    """
    m = res.pattern.match(field.attrib['name'])
    if m is None:
        return
    this_flavor = None
    if m.lastindex is not None:
        this_flavor = m.group(2)
        detected_flavors |= {this_flavor}
    if (this_flavor == desired_flavor or this_flavor is None and res.value is
        None):
        res.name = field.attrib['name']
        res.value = field.text
