Fixes h() to not return empty string when input contains invalid code unit sequence. Fixes #3561

Resubmitting #1080 for 3.0.