Clean up and reduce memory allocations in string escaping