Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Trustworthy #-}{-# LANGUAGE NoImplicitPrelude, ExistentialQuantification #-}{-# OPTIONS_GHC -funbox-strict-fields #-}{-# LANGUAGE BangPatterns #-}{-# LANGUAGE PatternSynonyms, ViewPatterns #-}{-# LANGUAGE UnboxedTuples, MagicHash #-}------------------------------------------------------------------------------- |-- Module : GHC.IO.Encoding.Types-- Copyright : (c) The University of Glasgow, 2008-2009-- License : see libraries/base/LICENSE---- Maintainer : libraries@haskell.org-- Stability : internal-- Portability : non-portable---- Types for text encoding/decoding-------------------------------------------------------------------------------moduleGHC.IO.Encoding.Types(BufferCodec(..,BufferCodec,encode,recover,close,getState,setState),TextEncoding(..),TextEncoder,TextDecoder,CodeBuffer,EncodeBuffer,DecodeBuffer,CodingProgress(..),DecodeBuffer#,EncodeBuffer#,DecodingBuffer#,EncodingBuffer#)whereimportGHC.BaseimportGHC.WordimportGHC.Show-- import GHC.IOimportGHC.IO.Buffer-- ------------------------------------------------------------------------------- Text encoders/decodersdataBufferCodecfromtostate=BufferCodec#{forall from to state.BufferCodec from to state -> CodeBuffer# from toencode#::CodeBuffer#fromto,-- ^ The @encode@ function translates elements of the buffer @from@-- to the buffer @to@. It should translate as many elements as possible-- given the sizes of the buffers, including translating zero elements-- if there is either not enough room in @to@, or @from@ does not-- contain a complete multibyte sequence.---- If multiple CodingProgress returns are possible, OutputUnderflow must be-- preferred to InvalidSequence. This allows GHC's IO library to assume that-- if we observe InvalidSequence there is at least a single element available-- in the output buffer.---- The fact that as many elements as possible are translated is used by the IO-- library in order to report translation errors at the point they-- actually occur, rather than when the buffer is translated.forall from to state.BufferCodec from to state-> Buffer from-> Buffer to-> State# RealWorld-> (# State# RealWorld, Buffer from, Buffer to #)recover#::Bufferfrom->Bufferto->State#RealWorld->(#State#RealWorld,Bufferfrom,Bufferto#),-- ^ The @recover@ function is used to continue decoding-- in the presence of invalid or unrepresentable sequences. This includes-- both those detected by @encode@ returning @InvalidSequence@ and those-- that occur because the input byte sequence appears to be truncated.---- Progress will usually be made by skipping the first element of the @from@-- buffer. This function should only be called if you are certain that you-- wish to do this skipping and if the @to@ buffer has at least one element-- of free space. Because this function deals with decoding failure, it assumes-- that the from buffer has at least one element.---- @recover@ may raise an exception rather than skipping anything.---- Currently, some implementations of @recover@ may mutate the input buffer.-- In particular, this feature is used to implement transliteration.---- @since 4.4.0.0forall from to state. BufferCodec from to state -> IO ()close#::IO(),-- ^ Resources associated with the encoding may now be released.-- The @encode@ function may not be called again after calling-- @close@.forall from to state. BufferCodec from to state -> IO stategetState#::IOstate,-- ^ Return the current state of the codec.---- Many codecs are not stateful, and in these case the state can be-- represented as '()'. Other codecs maintain a state. For-- example, UTF-16 recognises a BOM (byte-order-mark) character at-- the beginning of the input, and remembers thereafter whether to-- use big-endian or little-endian mode. In this case, the state-- of the codec would include two pieces of information: whether we-- are at the beginning of the stream (the BOM only occurs at the-- beginning), and if not, whether to use the big or little-endian-- encoding.forall from to state. BufferCodec from to state -> state -> IO ()setState#::state->IO()-- restore the state of the codec using the state from a previous-- call to 'getState'.}typeCodeBufferfromto=Bufferfrom->Bufferto->IO(CodingProgress,Bufferfrom,Bufferto)typeDecodeBuffer=CodeBufferWord8ChartypeEncodeBuffer=CodeBufferCharWord8typeCodeBuffer#fromto=Bufferfrom->Bufferto->State#RealWorld->(#State#RealWorld,CodingProgress,Bufferfrom,Bufferto#)typeDecodeBuffer#=CodeBuffer#Word8ChartypeEncodeBuffer#=CodeBuffer#CharWord8typeCodingBuffer#fromto=State#RealWorld->(#State#RealWorld,CodingProgress,Bufferfrom,Bufferto#)typeDecodingBuffer#=CodingBuffer#Word8ChartypeEncodingBuffer#=CodingBuffer#CharWord8typeTextDecoderstate=BufferCodecWord8CharBufElemstatetypeTextEncoderstate=BufferCodecCharBufElemWord8state-- | A 'TextEncoding' is a specification of a conversion scheme-- between sequences of bytes and sequences of Unicode characters.---- For example, UTF-8 is an encoding of Unicode characters into a sequence-- of bytes. The 'TextEncoding' for UTF-8 is 'System.IO.utf8'.dataTextEncoding=foralldstateestate.TextEncoding{TextEncoding -> StringtextEncodingName::String,-- ^ a string that can be passed to 'System.IO.mkTextEncoding' to-- create an equivalent 'TextEncoding'.()mkTextDecoder::IO(TextDecoderdstate),-- ^ Creates a means of decoding bytes into characters: the result must not-- be shared between several byte sequences or simultaneously across threads()mkTextEncoder::IO(TextEncoderestate)-- ^ Creates a means of encode characters into bytes: the result must not-- be shared between several character sequences or simultaneously across threads}-- | @since 4.3.0.0instanceShowTextEncodingwhereshow :: TextEncoding -> StringshowTextEncodingte=TextEncoding -> StringtextEncodingNameTextEncodingte-- | @since 4.4.0.0dataCodingProgress=InputUnderflow-- ^ Stopped because the input contains insufficient available elements,-- or all of the input sequence has been successfully translated.|OutputUnderflow-- ^ Stopped because the output contains insufficient free elements|InvalidSequence-- ^ Stopped because there are sufficient free elements in the output-- to output at least one encoded ASCII character, but the input contains-- an invalid or unrepresentable sequencederiving(CodingProgress -> CodingProgress -> Bool(CodingProgress -> CodingProgress -> Bool)-> (CodingProgress -> CodingProgress -> Bool) -> Eq CodingProgressforall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a$c== :: CodingProgress -> CodingProgress -> Bool== :: CodingProgress -> CodingProgress -> Bool$c/= :: CodingProgress -> CodingProgress -> Bool/= :: CodingProgress -> CodingProgress -> BoolEq-- ^ @since 4.4.0.0,Int -> CodingProgress -> ShowS[CodingProgress] -> ShowSCodingProgress -> String(Int -> CodingProgress -> ShowS)-> (CodingProgress -> String)-> ([CodingProgress] -> ShowS)-> Show CodingProgressforall a.(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a$cshowsPrec :: Int -> CodingProgress -> ShowSshowsPrec :: Int -> CodingProgress -> ShowS$cshow :: CodingProgress -> Stringshow :: CodingProgress -> String$cshowList :: [CodingProgress] -> ShowSshowList :: [CodingProgress] -> ShowSShow-- ^ @since 4.4.0.0){-# COMPLETEBufferCodec#-}patternBufferCodec::CodeBufferfromto->(Bufferfrom->Bufferto->IO(Bufferfrom,Bufferto))->IO()->IOstate->(state->IO())->BufferCodecfromtostatepattern$mBufferCodec :: forall {r} {from} {to} {state}.BufferCodec from to state-> (CodeBuffer from to -> (Buffer from -> Buffer to -> IO (Buffer from, Buffer to)) -> IO () -> IO state -> (state -> IO ()) -> r)-> ((# #) -> r)-> r$bBufferCodec :: 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{forall from to state.BufferCodec from to state -> CodeBuffer from toencode,forall from to state.BufferCodec from to state-> Buffer from -> Buffer to -> IO (Buffer from, Buffer to)recover,forall from to state. BufferCodec from to state -> IO ()close,forall from to state. BufferCodec from to state -> IO stategetState,forall from to state. BufferCodec from to state -> state -> IO ()setState}<-BufferCodec#(getEncode->encode)(getRecover->recover)closegetStatesetStatewhereBufferCodecCodeBuffer from toeBuffer from -> Buffer to -> IO (Buffer from, Buffer to)rIO ()cIO stategstate -> IO ()s=CodeBuffer# from to-> (Buffer from -> Buffer to -> State# RealWorld -> (# State# RealWorld, Buffer from, Buffer to #))-> IO ()-> IO state-> (state -> IO ())-> BufferCodec from to stateforall from to state.CodeBuffer# from to-> (Buffer from -> Buffer to -> State# RealWorld -> (# State# RealWorld, Buffer from, Buffer to #))-> IO ()-> IO state-> (state -> IO ())-> BufferCodec from to stateBufferCodec#(CodeBuffer from to -> CodeBuffer# from toforall from to. CodeBuffer from to -> CodeBuffer# from tomkEncodeCodeBuffer from toe)((Buffer from -> Buffer to -> IO (Buffer from, Buffer to))-> Buffer from-> Buffer to-> State# RealWorld-> (# State# RealWorld, Buffer from, Buffer to #)forall from to.(Buffer from -> Buffer to -> IO (Buffer from, Buffer to))-> Buffer from-> Buffer to-> State# RealWorld-> (# State# RealWorld, Buffer from, Buffer to #)mkRecoverBuffer from -> Buffer to -> IO (Buffer from, Buffer to)r)IO ()cIO stategstate -> IO ()sgetEncode::CodeBuffer#fromto->CodeBufferfromtogetEncode :: forall from to. CodeBuffer# from to -> CodeBuffer from togetEncodeCodeBuffer# from toeBuffer fromiBuffer too=(State# RealWorld -> (# State# RealWorld, (CodingProgress, Buffer from, Buffer to) #))-> IO (CodingProgress, Buffer from, Buffer to)forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO aIO((State# RealWorld -> (# State# RealWorld, (CodingProgress, Buffer from, Buffer to) #)) -> IO (CodingProgress, Buffer from, Buffer to))-> (State# RealWorld -> (# State# RealWorld, (CodingProgress, Buffer from, Buffer to) #))-> IO (CodingProgress, Buffer from, Buffer to)forall a b. (a -> b) -> a -> b$\State# RealWorldst->let!(#State# RealWorldst',CodingProgressprog,Buffer fromi',Buffer too'#)=CodeBuffer# from toeBuffer fromiBuffer tooState# RealWorldstin(#State# RealWorldst',(CodingProgressprog,Buffer fromi',Buffer too')#)getRecover::(Bufferfrom->Bufferto->State#RealWorld->(#State#RealWorld,Bufferfrom,Bufferto#))->(Bufferfrom->Bufferto->IO(Bufferfrom,Bufferto))getRecover :: forall from to.(Buffer from -> Buffer to -> State# RealWorld -> (# State# RealWorld, Buffer from, Buffer to #))-> Buffer from -> Buffer to -> IO (Buffer from, Buffer to)getRecoverBuffer from-> Buffer to-> State# RealWorld-> (# State# RealWorld, Buffer from, Buffer to #)rBuffer fromiBuffer too=(State# RealWorld -> (# State# RealWorld, (Buffer from, Buffer to) #))-> IO (Buffer from, Buffer to)forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO aIO((State# RealWorld -> (# State# RealWorld, (Buffer from, Buffer to) #)) -> IO (Buffer from, Buffer to))-> (State# RealWorld -> (# State# RealWorld, (Buffer from, Buffer to) #))-> IO (Buffer from, Buffer to)forall a b. (a -> b) -> a -> b$\State# RealWorldst->let!(#State# RealWorldst',Buffer fromi',Buffer too'#)=Buffer from-> Buffer to-> State# RealWorld-> (# State# RealWorld, Buffer from, Buffer to #)rBuffer fromiBuffer tooState# RealWorldstin(#State# RealWorldst',(Buffer fromi',Buffer too')#)mkEncode::CodeBufferfromto->CodeBuffer#fromtomkEncode :: forall from to. CodeBuffer from to -> CodeBuffer# from tomkEncodeCodeBuffer from toeBuffer fromiBuffer tooState# RealWorldst=let!(#State# RealWorldst',(CodingProgressprog,Buffer fromi',Buffer too')#)=IO (CodingProgress, Buffer from, Buffer to)-> State# RealWorld-> (# State# RealWorld, (CodingProgress, Buffer from, Buffer to) #)forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)unIO(CodeBuffer from toeBuffer fromiBuffer too)State# RealWorldstin(#State# RealWorldst',CodingProgressprog,Buffer fromi',Buffer too'#)mkRecover::(Bufferfrom->Bufferto->IO(Bufferfrom,Bufferto))->(Bufferfrom->Bufferto->State#RealWorld->(#State#RealWorld,Bufferfrom,Bufferto#))mkRecover :: forall from to.(Buffer from -> Buffer to -> IO (Buffer from, Buffer to))-> Buffer from-> Buffer to-> State# RealWorld-> (# State# RealWorld, Buffer from, Buffer to #)mkRecoverBuffer from -> Buffer to -> IO (Buffer from, Buffer to)rBuffer fromiBuffer tooState# RealWorldst=let!(#State# RealWorldst',(Buffer fromi',Buffer too')#)=IO (Buffer from, Buffer to)-> State# RealWorld-> (# State# RealWorld, (Buffer from, Buffer to) #)forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)unIO(Buffer from -> Buffer to -> IO (Buffer from, Buffer to)rBuffer fromiBuffer too)State# RealWorldstin(#State# RealWorldst',Buffer fromi',Buffer too'#)
[8]ページ先頭