Convert Text to Unicode Escape Sequence

余生颓废 提交于 2021-02-08 04:37:25

问题


I have a Text object that contains some number of Latin characters that needs to be converted to a unicode escape sequence of the format \u#### with # being hex digits

As described here, haskell easily converts strings to escape sequences and vice versa. However, it will only go to the decimal representation. For example,

> let s = "Ñ"
> s
"\209"

Is there a way to specify the escape sequence encoding to force it to spit out in the correct format? i.e

> let s = encodeUnicode16 "Ñ"
> s
"\u00d1"

回答1:


How about this:

import Text.Printf (printf)

encodeUnicode16 :: String -> String
encodeUnicode16 = concatMap escapeChar
  where
    escapeChar c
        | ' ' <= c && c <= 'z' = [c]
        | otherwise =
            printf "\\u%04x" (fromEnum c)

I ghci, you can use it as follows:

> putStrLn $ encodeUnicode16 "Ñ"
\u00d1

Note that if you don't use putStrLn it will get escaped twice:

> encodeUnicode16 "Ñ"
"\\u00d1"

This is because ghci will implicitly add a print in front of the command.

Edit: I missed that part that you have a Text and not a String. Here's the same code for Text:

import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Text.Printf (printf)

encodeUnicode16 :: Text -> Text
encodeUnicode16 = T.concatMap escapeChar
  where
    escapeChar c
        | ' ' <= c && c <= 'z' = T.singleton c
        | otherwise =
            T.pack $ printf "\\u%04x" (fromEnum c)

Again, you want to use T.putStrLn to avoid double escaping everything.



来源:https://stackoverflow.com/questions/39233848/convert-text-to-unicode-escape-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!