I get into the patch and I think it's worth being committed and backpatched.
BTW I noticed that sometimes the same comparisons are done twice, and I made a very minor refactor of the code. PFA v2 of a patch if you don't mind.
As for the question on what to do with the additional segments if the first one failed to be truncated, I don't consider myself experienced enough and surely someone else's independent opinion is very much welcome.