import re


def parsequoted(args, line):
    """
    Split a line containing quoted strings into columns.
    """
    fields = []
    rs = re.compile(args.separator)
    dquotes = '|""' if args.dquoted else ''
    squotes = "|''" if args.dquoted else ''
    rqq = re.compile(r'"(?:[^"\\]|\\(?:x\w{2}|u\w{4}|U\w{8}|u\{\w+\}|[^xuU])%s)*"' % dquotes)
    rq = re.compile(r'\'(?:[^\'\\]|\\(?:x\w{2}|u\w{4}|U\w{8}|u\{\w+\}|[^xuU])%s)*\'' % squotes)
    o = 0
    needseparator = False

    while o < len(line):
        if needseparator:
            m = rs.match(line, o)
            if not m:
                raise Exception("expected separator at pos %d in %s" % (o, line))
            o += len(m.group(0))
            needseparator = False
            continue
        m = rqq.match(line, o)
        if m:
            fields.append(m.group(0))
            o += len(m.group(0))
            needseparator = True
            continue
        m = rq.match(line, o)
        if m:
            fields.append(m.group(0))
            o += len(m.group(0))
            needseparator = True
            continue
        m = rs.search(line, o)
        if m:
            fields.append(line[o:m.start(0)])
            o = m.end(0)
        else:
            fields.append(line[o:])
            break
    return fields