code: def collapse_segments (path):
      
      # replace backslashes
      # note: this is _against_ the specification (which would require
      # backslashes to be left alone, and finally quoted with '%5C')
      # But replacing has several positive effects:
      # - Prevents path attacks on Windows systems (using \.. parent refs)
      # - Fixes bad URLs where users used backslashes instead of slashes.
      #   This is a far more probable case than users having an intentional
      #   backslash in the path name.
      path = path.replace('\\', '/')
      # shrink multiple slashes to one slash
      path = _slashes_ro.sub("/", path)
      # collapse redundant path segments
      path = _thisdir_ro.sub("", path)
      path = _samedir_ro.sub("/", path)
      # collapse parent path segments
      # note: here we exploit the fact that the replacements happen
      # to be from left to right (see also _parentdir_ro above)
      newpath = _parentdir_ro.sub("/", path)
      while newpath != path:
          path = newpath
          newpath = _parentdir_ro.sub("/", path)
      # collapse parent path segments of relative paths
      # (ie. without leading slash)
      newpath = _relparentdir_ro.sub("", path)
      while newpath != path:
          path = newpath
          newpath = _relparentdir_ro.sub("", path)
      return path
docs: Remove all redundant segments from the given URL path.
      Precondition: path is an unquoted url path