Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Trustworthy #-}{-# LANGUAGE NoImplicitPrelude , BangPatterns , NondecreasingIndentation , MagicHash #-}{-# OPTIONS_GHC -funbox-strict-fields #-}------------------------------------------------------------------------------- |-- Module : GHC.IO.Encoding.UTF16-- Copyright : (c) The University of Glasgow, 2009-- License : see libraries/base/LICENSE---- Maintainer : libraries@haskell.org-- Stability : internal-- Portability : non-portable---- UTF-16 Codecs for the IO library---- Portions Copyright : (c) Tom Harper 2008-2009,-- (c) Bryan O'Sullivan 2009,-- (c) Duncan Coutts 2009-------------------------------------------------------------------------------moduleGHC.IO.Encoding.UTF16(utf16,mkUTF16,utf16_decode,utf16_encode,utf16be,mkUTF16be,utf16be_decode,utf16be_encode,utf16le,mkUTF16le,utf16le_decode,utf16le_encode,)whereimportGHC.BaseimportGHC.RealimportGHC.Num-- import GHC.IOimportGHC.IO.BufferimportGHC.IO.Encoding.FailureimportGHC.IO.Encoding.TypesimportGHC.WordimportData.BitsimportGHC.IORef-- ------------------------------------------------------------------------------- The UTF-16 codec: either UTF16BE or UTF16LE with a BOMutf16::TextEncodingutf16 :: TextEncodingutf16=CodingFailureMode -> TextEncodingmkUTF16CodingFailureModeErrorOnCodingFailure-- | @since 4.4.0.0mkUTF16::CodingFailureMode->TextEncodingmkUTF16 :: CodingFailureMode -> TextEncodingmkUTF16CodingFailureModecfm=TextEncoding :: forall dstate estate.String-> IO (TextDecoder dstate)-> IO (TextEncoder estate)-> TextEncodingTextEncoding{textEncodingName :: StringtextEncodingName=String"UTF-16",mkTextDecoder :: IO (TextDecoder (Maybe DecodeBuffer))mkTextDecoder=CodingFailureMode -> IO (TextDecoder (Maybe DecodeBuffer))utf16_DFCodingFailureModecfm,mkTextEncoder :: IO (TextEncoder Bool)mkTextEncoder=CodingFailureMode -> IO (TextEncoder Bool)utf16_EFCodingFailureModecfm}utf16_DF::CodingFailureMode->IO(TextDecoder(MaybeDecodeBuffer))utf16_DF :: CodingFailureMode -> IO (TextDecoder (Maybe DecodeBuffer))utf16_DFCodingFailureModecfm=doIORef (Maybe DecodeBuffer)seen_bom<-Maybe DecodeBuffer -> IO (IORef (Maybe DecodeBuffer))forall a. a -> IO (IORef a)newIORefMaybe DecodeBufferforall a. Maybe aNothingTextDecoder (Maybe DecodeBuffer)-> IO (TextDecoder (Maybe DecodeBuffer))forall (m :: * -> *) a. Monad m => a -> m areturn(BufferCodec :: forall from to state.CodeBuffer from to-> (Buffer from -> Buffer to -> IO (Buffer from, Buffer to))-> IO ()-> IO state-> (state -> IO ())-> BufferCodec from to stateBufferCodec{encode :: DecodeBufferencode=IORef (Maybe DecodeBuffer) -> DecodeBufferutf16_decodeIORef (Maybe DecodeBuffer)seen_bom,recover :: Buffer Word8 -> Buffer Char -> IO (Buffer Word8, Buffer Char)recover=CodingFailureMode-> Buffer Word8 -> Buffer Char -> IO (Buffer Word8, Buffer Char)recoverDecodeCodingFailureModecfm,close :: IO ()close=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn(),getState :: IO (Maybe DecodeBuffer)getState=IORef (Maybe DecodeBuffer) -> IO (Maybe DecodeBuffer)forall a. IORef a -> IO areadIORefIORef (Maybe DecodeBuffer)seen_bom,setState :: Maybe DecodeBuffer -> IO ()setState=IORef (Maybe DecodeBuffer) -> Maybe DecodeBuffer -> IO ()forall a. IORef a -> a -> IO ()writeIORefIORef (Maybe DecodeBuffer)seen_bom})utf16_EF::CodingFailureMode->IO(TextEncoderBool)utf16_EF :: CodingFailureMode -> IO (TextEncoder Bool)utf16_EFCodingFailureModecfm=doIORef Booldone_bom<-Bool -> IO (IORef Bool)forall a. a -> IO (IORef a)newIORefBoolFalseTextEncoder Bool -> IO (TextEncoder Bool)forall (m :: * -> *) a. Monad m => a -> m areturn(BufferCodec :: forall from to state.CodeBuffer from to-> (Buffer from -> Buffer to -> IO (Buffer from, Buffer to))-> IO ()-> IO state-> (state -> IO ())-> BufferCodec from to stateBufferCodec{encode :: CodeBuffer Char Word8encode=IORef Bool -> CodeBuffer Char Word8utf16_encodeIORef Booldone_bom,recover :: Buffer Char -> Buffer Word8 -> IO (Buffer Char, Buffer Word8)recover=CodingFailureMode-> Buffer Char -> Buffer Word8 -> IO (Buffer Char, Buffer Word8)recoverEncodeCodingFailureModecfm,close :: IO ()close=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn(),getState :: IO BoolgetState=IORef Bool -> IO Boolforall a. IORef a -> IO areadIORefIORef Booldone_bom,setState :: Bool -> IO ()setState=IORef Bool -> Bool -> IO ()forall a. IORef a -> a -> IO ()writeIORefIORef Booldone_bom})utf16_encode::IORefBool->EncodeBufferutf16_encode :: IORef Bool -> CodeBuffer Char Word8utf16_encodeIORef Booldone_bomBuffer Charinputoutput :: Buffer Word8output@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=RawBuffer Word8oraw,bufL :: forall e. Buffer e -> IntbufL=Int_,bufR :: forall e. Buffer e -> IntbufR=Intow,bufSize :: forall e. Buffer e -> IntbufSize=Intos}=doBoolb<-IORef Bool -> IO Boolforall a. IORef a -> IO areadIORefIORef Booldone_bomifBoolbthenCodeBuffer Char Word8utf16_native_encodeBuffer CharinputBuffer Word8outputelseifIntosInt -> Int -> Intforall a. Num a => a -> a -> a-IntowInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int2then(CodingProgress, Buffer Char, Buffer Word8)-> IO (CodingProgress, Buffer Char, Buffer Word8)forall (m :: * -> *) a. Monad m => a -> m areturn(CodingProgressOutputUnderflow,Buffer Charinput,Buffer Word8output)elsedoIORef Bool -> Bool -> IO ()forall a. IORef a -> a -> IO ()writeIORefIORef Booldone_bomBoolTrueRawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8orawIntowWord8bom1RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8oraw(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)Word8bom2CodeBuffer Char Word8utf16_native_encodeBuffer CharinputBuffer Word8output{bufR :: IntbufR=IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int2}utf16_decode::IORef(MaybeDecodeBuffer)->DecodeBufferutf16_decode :: IORef (Maybe DecodeBuffer) -> DecodeBufferutf16_decodeIORef (Maybe DecodeBuffer)seen_bominput :: Buffer Word8input@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=RawBuffer Word8iraw,bufL :: forall e. Buffer e -> IntbufL=Intir,bufR :: forall e. Buffer e -> IntbufR=Intiw,bufSize :: forall e. Buffer e -> IntbufSize=Int_}Buffer Charoutput=doMaybe DecodeBuffermb<-IORef (Maybe DecodeBuffer) -> IO (Maybe DecodeBuffer)forall a. IORef a -> IO areadIORefIORef (Maybe DecodeBuffer)seen_bomcaseMaybe DecodeBuffermbofJustDecodeBufferdecode->DecodeBufferdecodeBuffer Word8inputBuffer CharoutputMaybe DecodeBufferNothing->ifIntiwInt -> Int -> Intforall a. Num a => a -> a -> a-IntirInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int2then(CodingProgress, Buffer Word8, Buffer Char)-> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a. Monad m => a -> m areturn(CodingProgressInputUnderflow,Buffer Word8input,Buffer Charoutput)elsedoWord8c0<-RawBuffer Word8 -> Int -> IO Word8readWord8BufRawBuffer Word8irawIntirWord8c1<-RawBuffer Word8 -> Int -> IO Word8readWord8BufRawBuffer Word8iraw(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)case()of()_|Word8c0Word8 -> Word8 -> Boolforall a. Eq a => a -> a -> Bool==Word8bomBBool -> Bool -> Bool&&Word8c1Word8 -> Word8 -> Boolforall a. Eq a => a -> a -> Bool==Word8bomL->doIORef (Maybe DecodeBuffer) -> Maybe DecodeBuffer -> IO ()forall a. IORef a -> a -> IO ()writeIORefIORef (Maybe DecodeBuffer)seen_bom(DecodeBuffer -> Maybe DecodeBufferforall a. a -> Maybe aJustDecodeBufferutf16be_decode)DecodeBufferutf16be_decodeBuffer Word8input{bufL :: IntbufL=IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int2}Buffer Charoutput|Word8c0Word8 -> Word8 -> Boolforall a. Eq a => a -> a -> Bool==Word8bomLBool -> Bool -> Bool&&Word8c1Word8 -> Word8 -> Boolforall a. Eq a => a -> a -> Bool==Word8bomB->doIORef (Maybe DecodeBuffer) -> Maybe DecodeBuffer -> IO ()forall a. IORef a -> a -> IO ()writeIORefIORef (Maybe DecodeBuffer)seen_bom(DecodeBuffer -> Maybe DecodeBufferforall a. a -> Maybe aJustDecodeBufferutf16le_decode)DecodeBufferutf16le_decodeBuffer Word8input{bufL :: IntbufL=IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int2}Buffer Charoutput|Boolotherwise->doIORef (Maybe DecodeBuffer) -> Maybe DecodeBuffer -> IO ()forall a. IORef a -> a -> IO ()writeIORefIORef (Maybe DecodeBuffer)seen_bom(DecodeBuffer -> Maybe DecodeBufferforall a. a -> Maybe aJustDecodeBufferutf16_native_decode)DecodeBufferutf16_native_decodeBuffer Word8inputBuffer CharoutputbomB,bomL,bom1,bom2::Word8bomB :: Word8bomB=Word80xfebomL :: Word8bomL=Word80xff-- choose UTF-16BE by default for UTF-16 outpututf16_native_decode::DecodeBufferutf16_native_decode :: DecodeBufferutf16_native_decode=DecodeBufferutf16be_decodeutf16_native_encode::EncodeBufferutf16_native_encode :: CodeBuffer Char Word8utf16_native_encode=CodeBuffer Char Word8utf16be_encodebom1 :: Word8bom1=Word8bomBbom2 :: Word8bom2=Word8bomL-- ------------------------------------------------------------------------------- UTF16LE and UTF16BEutf16be::TextEncodingutf16be :: TextEncodingutf16be=CodingFailureMode -> TextEncodingmkUTF16beCodingFailureModeErrorOnCodingFailure-- | @since 4.4.0.0mkUTF16be::CodingFailureMode->TextEncodingmkUTF16be :: CodingFailureMode -> TextEncodingmkUTF16beCodingFailureModecfm=TextEncoding :: forall dstate estate.String-> IO (TextDecoder dstate)-> IO (TextEncoder estate)-> TextEncodingTextEncoding{textEncodingName :: StringtextEncodingName=String"UTF-16BE",mkTextDecoder :: IO (TextDecoder ())mkTextDecoder=CodingFailureMode -> IO (TextDecoder ())utf16be_DFCodingFailureModecfm,mkTextEncoder :: IO (TextEncoder ())mkTextEncoder=CodingFailureMode -> IO (TextEncoder ())utf16be_EFCodingFailureModecfm}utf16be_DF::CodingFailureMode->IO(TextDecoder())utf16be_DF :: CodingFailureMode -> IO (TextDecoder ())utf16be_DFCodingFailureModecfm=TextDecoder () -> IO (TextDecoder ())forall (m :: * -> *) a. Monad m => a -> m areturn(BufferCodec :: forall from to state.CodeBuffer from to-> (Buffer from -> Buffer to -> IO (Buffer from, Buffer to))-> IO ()-> IO state-> (state -> IO ())-> BufferCodec from to stateBufferCodec{encode :: DecodeBufferencode=DecodeBufferutf16be_decode,recover :: Buffer Word8 -> Buffer Char -> IO (Buffer Word8, Buffer Char)recover=CodingFailureMode-> Buffer Word8 -> Buffer Char -> IO (Buffer Word8, Buffer Char)recoverDecodeCodingFailureModecfm,close :: IO ()close=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn(),getState :: IO ()getState=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn(),setState :: () -> IO ()setState=IO () -> () -> IO ()forall a b. a -> b -> aconst(IO () -> () -> IO ()) -> IO () -> () -> IO ()forall a b. (a -> b) -> a -> b$() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn()})utf16be_EF::CodingFailureMode->IO(TextEncoder())utf16be_EF :: CodingFailureMode -> IO (TextEncoder ())utf16be_EFCodingFailureModecfm=TextEncoder () -> IO (TextEncoder ())forall (m :: * -> *) a. Monad m => a -> m areturn(BufferCodec :: forall from to state.CodeBuffer from to-> (Buffer from -> Buffer to -> IO (Buffer from, Buffer to))-> IO ()-> IO state-> (state -> IO ())-> BufferCodec from to stateBufferCodec{encode :: CodeBuffer Char Word8encode=CodeBuffer Char Word8utf16be_encode,recover :: Buffer Char -> Buffer Word8 -> IO (Buffer Char, Buffer Word8)recover=CodingFailureMode-> Buffer Char -> Buffer Word8 -> IO (Buffer Char, Buffer Word8)recoverEncodeCodingFailureModecfm,close :: IO ()close=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn(),getState :: IO ()getState=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn(),setState :: () -> IO ()setState=IO () -> () -> IO ()forall a b. a -> b -> aconst(IO () -> () -> IO ()) -> IO () -> () -> IO ()forall a b. (a -> b) -> a -> b$() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn()})utf16le::TextEncodingutf16le :: TextEncodingutf16le=CodingFailureMode -> TextEncodingmkUTF16leCodingFailureModeErrorOnCodingFailure-- | @since 4.4.0.0mkUTF16le::CodingFailureMode->TextEncodingmkUTF16le :: CodingFailureMode -> TextEncodingmkUTF16leCodingFailureModecfm=TextEncoding :: forall dstate estate.String-> IO (TextDecoder dstate)-> IO (TextEncoder estate)-> TextEncodingTextEncoding{textEncodingName :: StringtextEncodingName=String"UTF16-LE",mkTextDecoder :: IO (TextDecoder ())mkTextDecoder=CodingFailureMode -> IO (TextDecoder ())utf16le_DFCodingFailureModecfm,mkTextEncoder :: IO (TextEncoder ())mkTextEncoder=CodingFailureMode -> IO (TextEncoder ())utf16le_EFCodingFailureModecfm}utf16le_DF::CodingFailureMode->IO(TextDecoder())utf16le_DF :: CodingFailureMode -> IO (TextDecoder ())utf16le_DFCodingFailureModecfm=TextDecoder () -> IO (TextDecoder ())forall (m :: * -> *) a. Monad m => a -> m areturn(BufferCodec :: forall from to state.CodeBuffer from to-> (Buffer from -> Buffer to -> IO (Buffer from, Buffer to))-> IO ()-> IO state-> (state -> IO ())-> BufferCodec from to stateBufferCodec{encode :: DecodeBufferencode=DecodeBufferutf16le_decode,recover :: Buffer Word8 -> Buffer Char -> IO (Buffer Word8, Buffer Char)recover=CodingFailureMode-> Buffer Word8 -> Buffer Char -> IO (Buffer Word8, Buffer Char)recoverDecodeCodingFailureModecfm,close :: IO ()close=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn(),getState :: IO ()getState=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn(),setState :: () -> IO ()setState=IO () -> () -> IO ()forall a b. a -> b -> aconst(IO () -> () -> IO ()) -> IO () -> () -> IO ()forall a b. (a -> b) -> a -> b$() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn()})utf16le_EF::CodingFailureMode->IO(TextEncoder())utf16le_EF :: CodingFailureMode -> IO (TextEncoder ())utf16le_EFCodingFailureModecfm=TextEncoder () -> IO (TextEncoder ())forall (m :: * -> *) a. Monad m => a -> m areturn(BufferCodec :: forall from to state.CodeBuffer from to-> (Buffer from -> Buffer to -> IO (Buffer from, Buffer to))-> IO ()-> IO state-> (state -> IO ())-> BufferCodec from to stateBufferCodec{encode :: CodeBuffer Char Word8encode=CodeBuffer Char Word8utf16le_encode,recover :: Buffer Char -> Buffer Word8 -> IO (Buffer Char, Buffer Word8)recover=CodingFailureMode-> Buffer Char -> Buffer Word8 -> IO (Buffer Char, Buffer Word8)recoverEncodeCodingFailureModecfm,close :: IO ()close=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn(),getState :: IO ()getState=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn(),setState :: () -> IO ()setState=IO () -> () -> IO ()forall a b. a -> b -> aconst(IO () -> () -> IO ()) -> IO () -> () -> IO ()forall a b. (a -> b) -> a -> b$() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn()})utf16be_decode::DecodeBufferutf16be_decode :: DecodeBufferutf16be_decodeinput :: Buffer Word8input@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=RawBuffer Word8iraw,bufL :: forall e. Buffer e -> IntbufL=Intir0,bufR :: forall e. Buffer e -> IntbufR=Intiw,bufSize :: forall e. Buffer e -> IntbufSize=Int_}output :: Buffer Charoutput@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=RawBuffer Charoraw,bufL :: forall e. Buffer e -> IntbufL=Int_,bufR :: forall e. Buffer e -> IntbufR=Intow0,bufSize :: forall e. Buffer e -> IntbufSize=Intos}=letloop :: Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)loop!Intir!Intow|IntowInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intos=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneCodingProgressOutputUnderflowIntirIntow|IntirInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intiw=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneCodingProgressInputUnderflowIntirIntow|IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int1Int -> Int -> Boolforall a. Eq a => a -> a -> Bool==Intiw=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneCodingProgressInputUnderflowIntirIntow|Boolotherwise=doWord8c0<-RawBuffer Word8 -> Int -> IO Word8readWord8BufRawBuffer Word8irawIntirWord8c1<-RawBuffer Word8 -> Int -> IO Word8readWord8BufRawBuffer Word8iraw(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)letx1 :: Word16x1=Word8 -> Word16forall a b. (Integral a, Num b) => a -> bfromIntegralWord8c0Word16 -> Int -> Word16forall a. Bits a => a -> Int -> a`shiftL`Int8Word16 -> Word16 -> Word16forall a. Num a => a -> a -> a+Word8 -> Word16forall a b. (Integral a, Num b) => a -> bfromIntegralWord8c1ifWord16 -> Boolvalidate1Word16x1thendoIntow'<-RawBuffer Char -> Int -> Char -> IO IntwriteCharBufRawBuffer CharorawIntow(Int -> CharunsafeChr(Word16 -> Intforall a b. (Integral a, Num b) => a -> bfromIntegralWord16x1))Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)loop(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int2)Intow'elseifIntiwInt -> Int -> Intforall a. Num a => a -> a -> a-IntirInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int4thenCodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneCodingProgressInputUnderflowIntirIntowelsedoWord8c2<-RawBuffer Word8 -> Int -> IO Word8readWord8BufRawBuffer Word8iraw(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int2)Word8c3<-RawBuffer Word8 -> Int -> IO Word8readWord8BufRawBuffer Word8iraw(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int3)letx2 :: Word16x2=Word8 -> Word16forall a b. (Integral a, Num b) => a -> bfromIntegralWord8c2Word16 -> Int -> Word16forall a. Bits a => a -> Int -> a`shiftL`Int8Word16 -> Word16 -> Word16forall a. Num a => a -> a -> a+Word8 -> Word16forall a b. (Integral a, Num b) => a -> bfromIntegralWord8c3ifBool -> Boolnot(Word16 -> Word16 -> Boolvalidate2Word16x1Word16x2)thenIO (CodingProgress, Buffer Word8, Buffer Char)invalidelsedoIntow'<-RawBuffer Char -> Int -> Char -> IO IntwriteCharBufRawBuffer CharorawIntow(Word16 -> Word16 -> Charchr2Word16x1Word16x2)Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)loop(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int4)Intow'whereinvalid :: IO (CodingProgress, Buffer Word8, Buffer Char)invalid=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneCodingProgressInvalidSequenceIntirIntow-- lambda-lifted, to avoid thunks being built in the inner-loop:done :: a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneawhy!Intir!Intow=(a, Buffer Word8, Buffer Char) -> m (a, Buffer Word8, Buffer Char)forall (m :: * -> *) a. Monad m => a -> m areturn(awhy,ifIntirInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==IntiwthenBuffer Word8input{bufL :: IntbufL=Int0,bufR :: IntbufR=Int0}elseBuffer Word8input{bufL :: IntbufL=Intir},Buffer Charoutput{bufR :: IntbufR=Intow})inInt -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)loopIntir0Intow0utf16le_decode::DecodeBufferutf16le_decode :: DecodeBufferutf16le_decodeinput :: Buffer Word8input@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=RawBuffer Word8iraw,bufL :: forall e. Buffer e -> IntbufL=Intir0,bufR :: forall e. Buffer e -> IntbufR=Intiw,bufSize :: forall e. Buffer e -> IntbufSize=Int_}output :: Buffer Charoutput@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=RawBuffer Charoraw,bufL :: forall e. Buffer e -> IntbufL=Int_,bufR :: forall e. Buffer e -> IntbufR=Intow0,bufSize :: forall e. Buffer e -> IntbufSize=Intos}=letloop :: Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)loop!Intir!Intow|IntowInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intos=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneCodingProgressOutputUnderflowIntirIntow|IntirInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intiw=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneCodingProgressInputUnderflowIntirIntow|IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int1Int -> Int -> Boolforall a. Eq a => a -> a -> Bool==Intiw=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneCodingProgressInputUnderflowIntirIntow|Boolotherwise=doWord8c0<-RawBuffer Word8 -> Int -> IO Word8readWord8BufRawBuffer Word8irawIntirWord8c1<-RawBuffer Word8 -> Int -> IO Word8readWord8BufRawBuffer Word8iraw(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)letx1 :: Word16x1=Word8 -> Word16forall a b. (Integral a, Num b) => a -> bfromIntegralWord8c1Word16 -> Int -> Word16forall a. Bits a => a -> Int -> a`shiftL`Int8Word16 -> Word16 -> Word16forall a. Num a => a -> a -> a+Word8 -> Word16forall a b. (Integral a, Num b) => a -> bfromIntegralWord8c0ifWord16 -> Boolvalidate1Word16x1thendoIntow'<-RawBuffer Char -> Int -> Char -> IO IntwriteCharBufRawBuffer CharorawIntow(Int -> CharunsafeChr(Word16 -> Intforall a b. (Integral a, Num b) => a -> bfromIntegralWord16x1))Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)loop(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int2)Intow'elseifIntiwInt -> Int -> Intforall a. Num a => a -> a -> a-IntirInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int4thenCodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneCodingProgressInputUnderflowIntirIntowelsedoWord8c2<-RawBuffer Word8 -> Int -> IO Word8readWord8BufRawBuffer Word8iraw(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int2)Word8c3<-RawBuffer Word8 -> Int -> IO Word8readWord8BufRawBuffer Word8iraw(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int3)letx2 :: Word16x2=Word8 -> Word16forall a b. (Integral a, Num b) => a -> bfromIntegralWord8c3Word16 -> Int -> Word16forall a. Bits a => a -> Int -> a`shiftL`Int8Word16 -> Word16 -> Word16forall a. Num a => a -> a -> a+Word8 -> Word16forall a b. (Integral a, Num b) => a -> bfromIntegralWord8c2ifBool -> Boolnot(Word16 -> Word16 -> Boolvalidate2Word16x1Word16x2)thenIO (CodingProgress, Buffer Word8, Buffer Char)invalidelsedoIntow'<-RawBuffer Char -> Int -> Char -> IO IntwriteCharBufRawBuffer CharorawIntow(Word16 -> Word16 -> Charchr2Word16x1Word16x2)Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)loop(IntirInt -> Int -> Intforall a. Num a => a -> a -> a+Int4)Intow'whereinvalid :: IO (CodingProgress, Buffer Word8, Buffer Char)invalid=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneCodingProgressInvalidSequenceIntirIntow-- lambda-lifted, to avoid thunks being built in the inner-loop:done :: a -> Int -> Int -> m (a, Buffer Word8, Buffer Char)doneawhy!Intir!Intow=(a, Buffer Word8, Buffer Char) -> m (a, Buffer Word8, Buffer Char)forall (m :: * -> *) a. Monad m => a -> m areturn(awhy,ifIntirInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==IntiwthenBuffer Word8input{bufL :: IntbufL=Int0,bufR :: IntbufR=Int0}elseBuffer Word8input{bufL :: IntbufL=Intir},Buffer Charoutput{bufR :: IntbufR=Intow})inInt -> Int -> IO (CodingProgress, Buffer Word8, Buffer Char)loopIntir0Intow0utf16be_encode::EncodeBufferutf16be_encode :: CodeBuffer Char Word8utf16be_encodeinput :: Buffer Charinput@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=RawBuffer Chariraw,bufL :: forall e. Buffer e -> IntbufL=Intir0,bufR :: forall e. Buffer e -> IntbufR=Intiw,bufSize :: forall e. Buffer e -> IntbufSize=Int_}output :: Buffer Word8output@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=RawBuffer Word8oraw,bufL :: forall e. Buffer e -> IntbufL=Int_,bufR :: forall e. Buffer e -> IntbufR=Intow0,bufSize :: forall e. Buffer e -> IntbufSize=Intos}=letdone :: a -> Int -> Int -> m (a, Buffer Char, Buffer Word8)doneawhy!Intir!Intow=(a, Buffer Char, Buffer Word8) -> m (a, Buffer Char, Buffer Word8)forall (m :: * -> *) a. Monad m => a -> m areturn(awhy,ifIntirInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==IntiwthenBuffer Charinput{bufL :: IntbufL=Int0,bufR :: IntbufR=Int0}elseBuffer Charinput{bufL :: IntbufL=Intir},Buffer Word8output{bufR :: IntbufR=Intow})loop :: Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)loop!Intir!Intow|IntirInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intiw=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Char, Buffer Word8)doneCodingProgressInputUnderflowIntirIntow|IntosInt -> Int -> Intforall a. Num a => a -> a -> a-IntowInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int2=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Char, Buffer Word8)doneCodingProgressOutputUnderflowIntirIntow|Boolotherwise=do(Charc,Intir')<-RawBuffer Char -> Int -> IO (Char, Int)readCharBufRawBuffer CharirawIntircaseChar -> IntordCharcofIntx|IntxInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0x10000->ifChar -> BoolisSurrogateCharcthenCodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Char, Buffer Word8)doneCodingProgressInvalidSequenceIntirIntowelsedoRawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8orawIntow(Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegral(IntxInt -> Int -> Intforall a. Bits a => a -> Int -> a`shiftR`Int8))RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8oraw(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)(Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegralIntx)Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)loopIntir'(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int2)|Boolotherwise->doifIntosInt -> Int -> Intforall a. Num a => a -> a -> a-IntowInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int4thenCodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Char, Buffer Word8)doneCodingProgressOutputUnderflowIntirIntowelsedoletn1 :: Intn1=IntxInt -> Int -> Intforall a. Num a => a -> a -> a-Int0x10000c1 :: Word8c1=Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegral(Intn1Int -> Int -> Intforall a. Bits a => a -> Int -> a`shiftR`Int18Int -> Int -> Intforall a. Num a => a -> a -> a+Int0xD8)c2 :: Word8c2=Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegral(Intn1Int -> Int -> Intforall a. Bits a => a -> Int -> a`shiftR`Int10)n2 :: Intn2=Intn1Int -> Int -> Intforall a. Bits a => a -> a -> a.&.Int0x3FFc3 :: Word8c3=Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegral(Intn2Int -> Int -> Intforall a. Bits a => a -> Int -> a`shiftR`Int8Int -> Int -> Intforall a. Num a => a -> a -> a+Int0xDC)c4 :: Word8c4=Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegralIntn2--RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8orawIntowWord8c1RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8oraw(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)Word8c2RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8oraw(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int2)Word8c3RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8oraw(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int3)Word8c4Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)loopIntir'(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int4)inInt -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)loopIntir0Intow0utf16le_encode::EncodeBufferutf16le_encode :: CodeBuffer Char Word8utf16le_encodeinput :: Buffer Charinput@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=RawBuffer Chariraw,bufL :: forall e. Buffer e -> IntbufL=Intir0,bufR :: forall e. Buffer e -> IntbufR=Intiw,bufSize :: forall e. Buffer e -> IntbufSize=Int_}output :: Buffer Word8output@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=RawBuffer Word8oraw,bufL :: forall e. Buffer e -> IntbufL=Int_,bufR :: forall e. Buffer e -> IntbufR=Intow0,bufSize :: forall e. Buffer e -> IntbufSize=Intos}=letdone :: a -> Int -> Int -> m (a, Buffer Char, Buffer Word8)doneawhy!Intir!Intow=(a, Buffer Char, Buffer Word8) -> m (a, Buffer Char, Buffer Word8)forall (m :: * -> *) a. Monad m => a -> m areturn(awhy,ifIntirInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==IntiwthenBuffer Charinput{bufL :: IntbufL=Int0,bufR :: IntbufR=Int0}elseBuffer Charinput{bufL :: IntbufL=Intir},Buffer Word8output{bufR :: IntbufR=Intow})loop :: Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)loop!Intir!Intow|IntirInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intiw=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Char, Buffer Word8)doneCodingProgressInputUnderflowIntirIntow|IntosInt -> Int -> Intforall a. Num a => a -> a -> a-IntowInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int2=CodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Char, Buffer Word8)doneCodingProgressOutputUnderflowIntirIntow|Boolotherwise=do(Charc,Intir')<-RawBuffer Char -> Int -> IO (Char, Int)readCharBufRawBuffer CharirawIntircaseChar -> IntordCharcofIntx|IntxInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0x10000->ifChar -> BoolisSurrogateCharcthenCodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Char, Buffer Word8)doneCodingProgressInvalidSequenceIntirIntowelsedoRawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8orawIntow(Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegralIntx)RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8oraw(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)(Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegral(IntxInt -> Int -> Intforall a. Bits a => a -> Int -> a`shiftR`Int8))Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)loopIntir'(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int2)|Boolotherwise->ifIntosInt -> Int -> Intforall a. Num a => a -> a -> a-IntowInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int4thenCodingProgress-> Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)forall (m :: * -> *) a.Monad m =>a -> Int -> Int -> m (a, Buffer Char, Buffer Word8)doneCodingProgressOutputUnderflowIntirIntowelsedoletn1 :: Intn1=IntxInt -> Int -> Intforall a. Num a => a -> a -> a-Int0x10000c1 :: Word8c1=Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegral(Intn1Int -> Int -> Intforall a. Bits a => a -> Int -> a`shiftR`Int18Int -> Int -> Intforall a. Num a => a -> a -> a+Int0xD8)c2 :: Word8c2=Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegral(Intn1Int -> Int -> Intforall a. Bits a => a -> Int -> a`shiftR`Int10)n2 :: Intn2=Intn1Int -> Int -> Intforall a. Bits a => a -> a -> a.&.Int0x3FFc3 :: Word8c3=Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegral(Intn2Int -> Int -> Intforall a. Bits a => a -> Int -> a`shiftR`Int8Int -> Int -> Intforall a. Num a => a -> a -> a+Int0xDC)c4 :: Word8c4=Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegralIntn2--RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8orawIntowWord8c2RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8oraw(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)Word8c1RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8oraw(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int2)Word8c4RawBuffer Word8 -> Int -> Word8 -> IO ()writeWord8BufRawBuffer Word8oraw(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int3)Word8c3Int -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)loopIntir'(IntowInt -> Int -> Intforall a. Num a => a -> a -> a+Int4)inInt -> Int -> IO (CodingProgress, Buffer Char, Buffer Word8)loopIntir0Intow0chr2::Word16->Word16->Charchr2 :: Word16 -> Word16 -> Charchr2(W16#Word#a#)(W16#Word#b#)=Char# -> CharC#(Int# -> Char#chr#(Int#upper#Int# -> Int# -> Int#+#Int#lower#Int# -> Int# -> Int#+#Int#0x10000#))where!x# :: Int#x#=Word# -> Int#word2Int#Word#a#!y# :: Int#y#=Word# -> Int#word2Int#Word#b#!upper# :: Int#upper#=Int# -> Int# -> Int#uncheckedIShiftL#(Int#x#Int# -> Int# -> Int#-#Int#0xD800#)Int#10#!lower# :: Int#lower#=Int#y#Int# -> Int# -> Int#-#Int#0xDC00#{-# INLINEchr2#-}validate1::Word16->Boolvalidate1 :: Word16 -> Boolvalidate1Word16x1=(Word16x1Word16 -> Word16 -> Boolforall a. Ord a => a -> a -> Bool>=Word160Bool -> Bool -> Bool&&Word16x1Word16 -> Word16 -> Boolforall a. Ord a => a -> a -> Bool<Word160xD800)Bool -> Bool -> Bool||Word16x1Word16 -> Word16 -> Boolforall a. Ord a => a -> a -> Bool>Word160xDFFF{-# INLINEvalidate1#-}validate2::Word16->Word16->Boolvalidate2 :: Word16 -> Word16 -> Boolvalidate2Word16x1Word16x2=Word16x1Word16 -> Word16 -> Boolforall a. Ord a => a -> a -> Bool>=Word160xD800Bool -> Bool -> Bool&&Word16x1Word16 -> Word16 -> Boolforall a. Ord a => a -> a -> Bool<=Word160xDBFFBool -> Bool -> Bool&&Word16x2Word16 -> Word16 -> Boolforall a. Ord a => a -> a -> Bool>=Word160xDC00Bool -> Bool -> Bool&&Word16x2Word16 -> Word16 -> Boolforall a. Ord a => a -> a -> Bool<=Word160xDFFF{-# INLINEvalidate2#-}
[8]ページ先頭