Created
December 14, 2012 03:12
-
-
Save mmitou/4282402 to your computer and use it in GitHub Desktop.
unicode escapeされた文字をunicodeの文字に変換する。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE NoMonomorphismRestriction, OverloadedStrings #-} | |
| import qualified Data.ByteString.Char8 as BS | |
| import qualified Data.Attoparsec.ByteString as AB | |
| import qualified Data.Attoparsec.ByteString.Char8 as AC | |
| import Control.Applicative ((<*), (*>), (<$>), (<|>)) | |
| import Data.Conduit.Attoparsec | |
| import Data.Conduit | |
| import Data.Char | |
| unicodeChar :: AB.Parser Char | |
| unicodeChar = do | |
| AC.string "\\u" | |
| code <- AC.count 4 hexDigit | |
| return $ toChar code | |
| where | |
| hexDigit = AC.satisfy isHexDigit | |
| toChar x = chr $ read $ "0x" ++ x | |
| escapedChar :: AB.Parser Char | |
| escapedChar = toChar <$> AC.char '\\' *> oneOf "\"\\/nrftb" | |
| where | |
| oneOf s = AC.satisfy $ \c -> elem c s | |
| toChar '\"' = '"' | |
| toChar '\\' = '\\' | |
| toChar '/' = '/' | |
| toChar 'n' = '\n' | |
| toChar 'r' = '\r' | |
| toChar 'f' = '\f' | |
| toChar 't' = '\t' | |
| toChar 'b' = '\b' | |
| unicodeString :: AB.Parser String | |
| unicodeString = do | |
| isEnd <- AC.atEnd | |
| if isEnd then | |
| return [] | |
| else do | |
| c <- unicodeChar <|> escapedChar <|> AC.anyChar | |
| s <- unicodeString | |
| return (c:s) | |
| sample = "Coq\\u307f\\u305f\\u3044\\u306a\\u611f\\u3058\\u306b\n\\u6570\\u5f0f\\u5909\\u5f62\\u3092\\u88dc\\u4f50\\u3057\\u3066\\u304f\\u308c\\u308b\\u30b7\t\\u30b9\\u30c6\\u30e0\\u304c\\u6b32\\u3057\\u3044\\u3002YO!" | |
| main = do | |
| let (Right x) = AB.parseOnly unicodeString sample | |
| putStrLn x | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment