Can you add a comment here? It was hard to figure that:

we first initialise shape, lower, upper from slices information, indices from long information (I don't really understand if they are compacted at the beginning of the matching array or not.)
Then, for each slice, you change the shape (why?)
Then you fix shape to report previous slicing information I think.