Refactors the escaping function

This is the first step to reduce the memory usage. The escaping function was
changed to: given a string it is capable to compute the amount of bytes that
will be needed to really ecape it.