Seeing how you do this twice (and then later on do the reverse operation twice as well), wouldn't it be cleaner to move this into dedicated escape/unescape functions?