3

我有一个输入,其中“Divinités”(9 个字符)将表示为“Divinit\303\251s”(16 个字符长的实际文本数据),如何将其转换为 Haskell 的正确编码Text(或ByteString,或String)?

4

1 回答 1

2

首先,您需要将每个转义序列转换为一个Char. 然后使用utf8-string包将结果解码为实际的 utf8 字符串。

import Data.Char
import Codec.Binary.UTF8.String (decodeString)

input :: String
input = "Divinit\\303\\251s"

main = maybe (return ()) putStrLn $ convertString input

convertString :: [Char] -> Maybe [Char]
convertString = fmap decodeString . unescape

unescape :: [Char] -> Maybe [Char]
unescape [] = Just []
unescape ('\\' : tail) = do
  headResult <- fmap toEnum . octalDigitsToInt . take 3 $ tail
  tailResult <- unescape . drop 3 $ tail
  return $ headResult : tailResult
unescape (head : tail) = fmap (head :) . unescape $ tail

octalDigitsToInt :: [Char] -> Maybe Int
octalDigitsToInt = 
  fmap sum . sequence .
    map (\(i, c) -> fmap (8^i*) $ octalDigitToInt c) .
      zip [0..] . reverse

octalDigitToInt :: Char -> Maybe Int
octalDigitToInt c | isOctDigit c = Just $ digitToInt c
octalDigitToInt _ = Nothing
于 2013-02-04T11:27:02.323 回答