Attached is v2 of the patch, now based on r3357, as v1 causes a conflict.
I've also added a unit test and a bit of documentation.
If I hear no complains I'll commit this on Monday.

Gerd