Fixed PHP-807: Rewrite to_index_string to use smart_str and a real C function

I've also added some additional test cases.