def get_unaligned_spans(x1, x2, ptrs):
  """Get unaligned spans from an edit distance pointer matrix."""
  n1, n2 = len(x1), len(x2)
  i1, i2 = n1, n2
  spans = []
  end1, end2 = None, None
  while i1 != 0 or i2 != 0:
    i1_new, i2_new = ptrs[i1][i2]
    if i1_new == i1 or i2_new == i2 or x1[i1_new] != x2[i2_new]:  # mismatch/gap
      if end1 is None:
        end1, end2 = i1, i2
    else:
      if end1 is not None:
        spans.append(((i1, end1), (i2, end2)))
        end1, end2 = None, None
    i1, i2 = i1_new, i2_new
  if end1 is not None:
    spans.append(((0, end1), (0, end2)))
  return spans