This is what I've come up with. I didn't do any speed testing or serious unit testing,
so I'd be happy if somebody could give me a hand.
The main "issue" with Danilo's procedure is that it iterates through the same string multiple times.
I put the replacement logic into a separate procedure in order to speed up cases
where no escape sequences occur.
I'm not sure if putting a #NUL character into the middle of a string confuses PB,
but it seems to work so far (possible memory leak?).
The Select statement could be optimized by removing it entirely and replacing
the insertion character assignment with a lookup table. I don't believe PB's selects actually work that way (I could be wrong).
In other languages this would be the normal way how select works.