The function was written by Remi-C and slightly modified by me to avoid the check for NULL and be const-correct. Also I've added a unit test for the new library function.
much optimization left to do