The above logic would ignore trailing nulls, as it doesn't write them until a non null value is found, which is what we want. Storing trailing logic would cause the comparison to be incorrect.

The offsets should be fine - you just need to subtract nNulls from i when you exit the loop to know how many elements you have.

Can you update your pull request so I can take a look? There are about 15 lines of code added - doesn't look to bad yet.