def splitesc_quoted(string, split_char, escape_char='\\', quote_chars='\'"'):
    """split a string at split_char, but respect (and preserve) all the
    characters inside a quote_chars pair (including escaped quote_chars and
    split_chars). split_char can also be escaped when outside of a
    quote_chars pair."""
    out = []
    i = 0
    l = len(string)
    prev = 0
    while i < l:
        is_escaped = i > 0 and string[i - 1] == escape_char
        c = string[i]
        if c in quote_chars:
            j = i + 1
            closes_quotes = False
            while j < l:
                d = string[j]
                is_escaped_j = j > 0 and string[j - 1] == escape_char
                if d == c and not is_escaped_j:
                    j += 1
                    closes_quotes = True
                    break
                j += 1
            if not closes_quotes:
                i += 1
            else:
                s = string[prev:j]
                if j < l and string[j] != split_char:
                    s += string[j]
                if s:
                    if prev > 0 and string[prev - 1] == split_char:
                        prev = j + 1
                        i = prev
                        out.append(s)
                    else:
                        prev = j + 1
                        i = prev
                        if j < l and string[j] != split_char:
                            s += string[j]
                        if out:
                            out[-1] += s
                        else:
                            out.append(s)
        elif c == split_char and not is_escaped:
            if i > 0 and i < l and i > prev:
                s = string[prev:i]
                if s:
                    out.append(s)
            prev = i + 1
            i = prev
        else:
            i += 1
    if prev < l:
        s = string[prev:l]
        if s:
            out.append(s)
    return out
