def _globre(pat):
    r"""Convert an extended glob string to a regexp string.

    >>> from . import pycompat
    >>> def bprint(s):
    ...     print(s)
    >>> bprint(_globre(br'?'))
    .
    >>> bprint(_globre(br'*'))
    [^/]*
    >>> bprint(_globre(br'**'))
    .*
    >>> bprint(_globre(br'**/a'))
    (?:.*/)?a
    >>> bprint(_globre(br'a/**/b'))
    a/(?:.*/)?b
    >>> bprint(_globre(br'[a*?!^][^b][!c]'))
    [a*?!^][\^b][^c]
    >>> bprint(_globre(br'{a,b}'))
    (?:a|b)
    >>> bprint(_globre(br'.\*\?'))
    \.\*\?
    """
    i, n = 0, len(pat)
    res = ""
    group = 0
    escape = util.re.escape

    def peek():
        return i < n and pat[i : i + 1]

    while i < n:
        c = pat[i : i + 1]
        i += 1
        if c not in "*?[{},\\":
            res += escape(c)
        elif c == "*":
            if peek() == "*":
                i += 1
                if peek() == "/":
                    i += 1
                    res += "(?:.*/)?"
                else:
                    res += ".*"
            else:
                res += "[^/]*"
        elif c == "?":
            res += "."
        elif c == "[":
            j = i
            if j < n and pat[j : j + 1] in "!]":
                j += 1
            while j < n and pat[j : j + 1] != "]":
                j += 1
            if j >= n:
                res += "\\["
            else:
                stuff = pat[i:j].replace("\\", "\\\\")
                i = j + 1
                if stuff[0:1] == "!":
                    stuff = "^" + stuff[1:]
                elif stuff[0:1] == "^":
                    stuff = "\\" + stuff
                res = "%s[%s]" % (res, stuff)
        elif c == "{":
            group += 1
            res += "(?:"
        elif c == "}" and group:
            res += ")"
            group -= 1
        elif c == "," and group:
            res += "|"
        elif c == "\\":
            p = peek()
            if p:
                i += 1
                res += escape(p)
            else:
                res += escape(c)
        else:
            res += escape(c)
    return res