2013-02-04 2 views

답변

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 
+0

고맙습니다! –

관련 문제