I decided to give this a go. This is what I came up with:
It has the usual consistency issues of mesecons/voxelibre-redstone, but seems to work the majority of the time. It won't return empty carts though, so make sure to send them in with at least 1 item.
Edit: much simpler version:
(Remember to right click the second comparator to put it into subtraction mode)