Escape control characters in a C string
JavaScript (ES6) 152
First time ever that .reduceRight seems useful. (But probably the good old .map can do better). The point is, I need to see the next character before deciding whether to output a short or long octal escape.
f=s=&gt;[...s].reduceRight((a,c)=&gt;
((x=c.charCodeAt())==34|c==(q='\\')?q+c:x&gt;31&amp;x&lt;127?c:q+('abtnvvr'[x-7]||(a&gt;='0'&amp;a&lt;'8'?512+x:x).toString(8).slice(-3)))+a
)