mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
One of our providers gives us messages with special characters escaped, ie a newline comes through as `\n`, not a literal newline. We shouldn’t be showing these backslashes to any of our users. We also have examples of real inbound messages containing `👍` and `’`, so we should continue to display these properly. It’s a bit tricky, because the strings we get from this provider are a mixture of escape sequences (eg `\n`) and unicode characters (eg `😨`). So we have to first convert the unicode character `😨` into an escape sequence, `\U0001f628` in this example. We do this by encoding with the `raw_unicode_escape` codec: > Latin-1 encoding with \uXXXX and \UXXXXXXXX for other code points. > Existing backslashes are not escaped in any way. It is used in the > Python pickle protocol. – https://docs.python.org/3/library/codecs.html#text-encodings Then we turn this back into a string using the `unicode_escape` codec, which transforms all escape sequences into their literal representations (eg `\U0001f628` becomes `😨` and `\n` becomes a newline).
5.2 KiB
5.2 KiB