{-# LANGUAGE ExistentialQuantification #-}moduleData.Encoding.BasewhereimportData.Encoding.ExceptionimportData.Encoding.ByteSourceimportData.Encoding.ByteSinkimportControl.ThrowsimportData.Array.UnboxedasArrayimportData.MapasMaphiding((!))importData.WordimportData.CharimportData.Typeable{- | The base class for all encodings. At least decodeChar, encodeChar and encodeable must be implemented.
-}classEncodingencwhere-- | Read a single character of a ByteSourcedecodeChar::ByteSourcem=>enc->mChar-- | Encode a single character and write it to a ByteSinkencodeChar::ByteSinkm=>enc->Char->m()-- | Read characters from a ByteSource until it is emptydecode::ByteSourcem=>enc->mStringdecodee=untilMsourceEmpty(decodeChare)-- | Encode a String and write it to a ByteSinkencode::ByteSinkm=>enc->String->m()encodee=mapM_(encodeChare)-- | Tests whether a given character is representable in the Encoding.-- If this yields True, encodeChar must not fail.-- If it yields False, encodeChar _must_ throw an exception.encodeable::enc->Char->Bool{- | Wraps all possible encoding types into one data type.
Used when a function needs to return an encoding.
-}dataDynEncoding=forallenc.(Encodingenc,Eqenc,Typeableenc,Showenc)=>DynEncodingencinstanceShowDynEncodingwhereshow(DynEncodingenc)=showencinstanceEncodingDynEncodingwheredecodeChar(DynEncodinge)=decodeChareencodeChar(DynEncodinge)=encodeCharedecode(DynEncodinge)=decodeeencode(DynEncodinge)=encodeeencodeable(DynEncodinge)=encodeableeinstanceEqDynEncodingwhere(DynEncodinge1)==(DynEncodinge2)=casecaste2ofNothing->FalseJuste2'->e1==e2'untilM::Monadm=>mBool->ma->m[a]untilMcheckact=doend<-checkifendthenreturn[]else(dox<-actxs<-untilMcheckactreturn(x:xs))untilM_::Monadm=>mBool->ma->m()untilM_checkact=untilMcheckact>>return()encodeWithMap::ByteSinkm=>MapCharWord8->Char->m()encodeWithMapmpc=caseMap.lookupcmpofNothing->throwException$HasNoRepresentationcJustv->pushWord8vencodeWithMap2::ByteSinkm=>MapChar(Word8,Word8)->Char->m()encodeWithMap2mpc=caseMap.lookupcmpofNothing->throwException$HasNoRepresentationcJust(w1,w2)->dopushWord8w1pushWord8w2encodeableWithMap::MapChara->Char->BoolencodeableWithMap=flipMap.memberdecodeWithArray::ByteSourcem=>UArrayWord8Int->mChardecodeWithArrayarr=dow<-fetchWord8letres=arr!wifres<0thenthrowException$IllegalCharacterwelsereturn$chrresdecodeWithArray2::ByteSourcem=>UArray(Word8,Word8)Int->mChardecodeWithArray2arr=dow1<-fetchWord8w2<-fetchWord8ifinRange(boundsarr)(w1,w2)then(doletres=arr!(w1,w2)ifres<0thenthrowException$IllegalCharacterw1elsereturn$chrres)elsethrowException$IllegalCharacterw1