Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If you want to make string safe, filter out or escape all bytes / characters in 0-31 range. Maybe keep newlines if you expect multiple lines.

Done -- as simple as it gets.



Some terminals support the C1 control set, and in particular 0x9b as the CSI is dangerous (it's a single character version of the ESC-[ CSI). So you have to watch out for that too.


Interesting: https://man7.org/linux/man-pages/man4/console_codes.4.html

I'll definitely need to add support for that. Possibly annoying to strip that with utf8 involved. Any idea how many terminals support 0x9b?


With UTF-8 you need to UTF-8 encode the 9b.

For example this shows red on xfce4-terminal in UTF-8 mode:

  printf "\xc2\x9b31mHi\e[0m\n"


Hm, but I won't know if the text/terminal is utf8 or not going in.


Right... and you have the Windows-1252 versus iso-8859-1 situation, which only differ in that the former puts printable characters where the latter has C1 control codes.

Of course your code is a library so you have the library author's prerogative to punt that problem to the users :)


...interesting




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: