{-# LANGUAGE FlexibleInstances #-}moduleWeb.Encodings.StringLike(StringLike(..))whereimportPrelude(Char,Bool(..),String,Int,Eq(..),Show,($),(.),(<=),(-),otherwise,Maybe(..),(&&),not,elem,(+),toEnum,fromEnum,map,(<),(||),(>=),fmap)importqualifiedPreludeasPimportqualifiedData.ListasLimportqualifiedWeb.Encodings.ListHelperasLHimportqualifiedData.ByteString.Char8asBSimportqualifiedData.ByteString.Lazy.Char8asBLimportqualifiedData.MonoidasMimportqualifiedData.TextasTSimportqualifiedData.Text.LazyasTLimportData.Maybe(fromMaybe)importData.Bits((.|.),(.&.),shiftL,shiftR)importData.Word(Word8)importqualifiedData.ByteStringclass(Eqa,Showa)=>StringLikeawherespan::(Char->Bool)->a->(a,a)null::a->BoolconcatMap::(Char->String)->a->adropWhile::(Char->Bool)->a->abreak::(Char->Bool)->a->(a,a)cons::Char->a->auncons::a->Maybe(Char,a)append::a->a->aintercalate::a->[a]->aisPrefixOf::a->a->Booltake::Int->a->ahead::a->Chartail::a->ainit::a->alast::a->Charempty::apack::String->aunpack::a->StringpackUtf8::String->aunpackUtf8::BS.ByteString->MaybeadropPrefix::a->a->MaybeadropPrefixporigsorig=helperporigsorigwherehelperps|nullp&&nulls=Justempty|nullp=Justs|nulls=Nothing|headp==heads=helper(tailp)(tails)|otherwise=NothingdropPrefix'::a->a->adropPrefix'pc=casedropPrefixpcofJustx->xNothing->cdropQuotes::a->adropQuotess|lengthGE2s&&heads=='"'&&lasts=='"'=tail$inits|otherwise=schomp::a->achomps|nulls=schomps=caselastsof'\n'->chomp$inits'\r'->chomp$inits_->schompStart::a->achompStarts=caseunconssofJust('\r',rest)->caseunconsrestofJust('\n',rest')->rest'_->sJust('\n',rest)->rest_->ssplit::Char->a->[a]splitcs=let(next,rest)=breakCharcsinifnullnextthen(ifnullrestthen[]else[rest])elsenext:splitcrestsplitOneOf::[Char]->a->[a]splitOneOfcss=let(next,rest)=breakCharscssinifnullnextthen(ifnullrestthen[]else[rest])elsenext:splitOneOfcsrestbreakCharsMaybe::[Char]->a->Maybe(a,a)breakCharsMaybecs|nulls=Nothing|heads`elem`c=Just(empty,tails)|otherwise=do(next,rest)<-breakCharsMaybec(tails)Just(cons(heads)next,rest)breakCharMaybe::Char->a->Maybe(a,a)breakCharMaybecs|nulls=Nothing|c==heads=Just(empty,tails)|otherwise=do(next,rest)<-breakCharMaybec(tails)Just(cons(heads)next,rest)breakChar::Char->a->(a,a)breakCharcs=fromMaybe(s,empty)$breakCharMaybecsbreakChars::[Char]->a->(a,a)breakCharscs=fromMaybe(s,empty)$breakCharsMaybecsbreakString::a->a->(a,a)breakString_c|nullc=(empty,empty)breakStringpc=casedropPrefixpcofJustx->(empty,x)Nothing->letx=headcxs=tailc(next,rest)=breakStringpxsin(consxnext,rest)takeLine::a->(a,a)takeLinea=let(x,y)=breakChar'\n'ax'=chompxin(x',y)takeLineMaybe::a->Maybe(a,a)takeLineMaybea=do(x,y)<-breakCharMaybe'\n'aJust(chompx,y)takeUntilBlank::a->([a],a)takeUntilBlanka=let(next,rest)=takeLineainifnullnextthen([],rest)elselet(nexts,rest')=takeUntilBlankrestin(next:nexts,rest')takeUntilBlankMaybe::a->Maybe([a],a)takeUntilBlankMaybea=do(next,rest)<-takeLineMaybeaifnullnextthenJust([],rest)elsedo(nexts,rest')<-takeUntilBlankMayberestJust(next:nexts,rest')lengthLT::Int->a->BoollengthLTi_|i<=0=FalselengthLTia|nulla=True|otherwise=lengthLT(i-1)$tailalengthGE::Int->a->BoollengthGEi=not.lengthLTi-- | UTF8 encode each character before passing to concatMap, if-- appropriate.concatMapUtf8::(Char->String)->a->ainstanceStringLike[Char]whereintercalate=L.intercalatenull=P.nullconcatMap=P.concatMaptail=P.tailhead=P.headcons=LH.consuncons[]=Nothinguncons(x:xs)=Just(x,xs)span=P.spandropWhile=P.dropWhilebreak=P.breakappend=M.mappendisPrefixOf=L.isPrefixOftake=P.takeempty=M.memptypack=P.idunpack=P.idpackUtf8=packunpackUtf8=utf8Decode.Data.ByteString.unpackinit=P.initlast=P.lastconcatMapUtf8f=concatMap(concatMapf.utf8EncodeChar)instanceStringLikeBS.ByteStringwherespan=BS.spannull=BS.nullconcatMapf=BS.concatMap$pack.fdropWhile=BS.dropWhilebreak=BS.breakcons=BS.consuncons=BS.unconsappend=BS.appendintercalate=BS.intercalateisPrefixOf=BS.isPrefixOftake=BS.takehead=BS.headtail=BS.tailempty=BS.emptypack=BS.packunpack=BS.unpackpackUtf8=pack.concatMaputf8EncodeCharunpackUtf8=Justinit=BS.initlast=BS.lastconcatMapUtf8=concatMapinstanceStringLikeBL.ByteStringwherespan=BL.spannull=BL.nullconcatMapf=BL.concatMap$pack.fdropWhile=BL.dropWhilebreak=BL.breakcons=BL.consuncons=BL.unconsappend=BL.appendintercalate=BL.intercalateisPrefixOf=BL.isPrefixOftakei=BL.take$P.fromIntegralihead=BL.headtail=BL.tailempty=BL.emptypack=BL.packunpack=BL.unpackpackUtf8=pack.concatMaputf8EncodeCharunpackUtf8bs=Just$BL.fromChunks[bs]init=BL.initlast=BL.lastconcatMapUtf8=concatMapinstanceStringLikeTS.Textwherespan=TS.spanBynull=TS.nullconcatMapf=TS.concatMap$pack.fdropWhile=TS.dropWhilebreak=TS.breakBycons=TS.consuncons=TS.unconsappend=TS.appendintercalate=TS.intercalateisPrefixOf=TS.isPrefixOftakei=TS.take$P.fromIntegralihead=TS.headtail=TS.tailempty=TS.emptypack=TS.packunpack=TS.unpackpackUtf8=packunpackUtf8=fmappack.unpackUtf8init=TS.initlast=TS.lastconcatMapUtf8f=concatMap(concatMapf.utf8EncodeChar)instanceStringLikeTL.Textwherespan=TL.spanBynull=TL.nullconcatMapf=TL.concatMap$pack.fdropWhile=TL.dropWhilebreak=TL.breakBycons=TL.consuncons=TL.unconsappend=TL.appendintercalate=TL.intercalateisPrefixOf=TL.isPrefixOftakei=TL.take$P.fromIntegralihead=TL.headtail=TL.tailempty=TL.emptypack=TL.packunpack=TL.unpackpackUtf8=packunpackUtf8=fmappack.unpackUtf8init=TL.initlast=TL.lastconcatMapUtf8f=concatMap(concatMapf.utf8EncodeChar)-- | Code taken from Codec.Binary.UTF8.String.decodeutf8Decode::[Word8]->MaybeStringutf8Decode[]=Just""utf8Decode(c:cs)|c<0x80=docs'<-utf8DecodecsJust$toEnum(fromEnumc):cs'|c<0xc0=Nothing|c<0xe0=multi1|c<0xf0=multi_byte20xf0x800|c<0xf8=multi_byte30x70x10000|c<0xfc=multi_byte40x30x200000|c<0xfe=multi_byte50x10x4000000|otherwise=Nothingwheremulti1=casecsofc1:ds|c1.&.0xc0==0x80->letd=((fromEnumc.&.0x1f)`shiftL`6).|.fromEnum(c1.&.0x3f)inifd>=0x000080then(dods'<-utf8DecodedsJust$toEnumd:ds')elseNothing_->Nothingmulti_byte::Int->Word8->Int->Maybe[Char]multi_byteimaskoverlong=auxics(fromEnum(c.&.mask))whereaux0rsacc|overlong<=acc&&acc<=0x10ffff&&(acc<0xd800||0xdfff<acc)&&(acc<0xfffe||0xffff<acc)=dors'<-utf8DecodersJust$toEnumacc:rs'|otherwise=Nothingauxn(r:rs)acc|r.&.0xc0==0x80=aux(n-1)rs$shiftLacc6.|.fromEnum(r.&.0x3f)aux__rs_=Nothingutf8EncodeChar::Char->Stringutf8EncodeChar=maptoEnum.utf8EncodeInt.fromEnum-- | Code taken from Codec.Binary.UTF8.String.encodeutf8EncodeInt::Int->[Int]utf8EncodeIntc|c<=0x7f=[c]|c<=0x7ff=[0xc0+(c`shiftR`6),0x80+c.&.0x3f]|c<=0xffff=[0xe0+(c`shiftR`12),0x80+((c`shiftR`6).&.0x3f),0x80+c.&.0x3f]|otherwise=[0xf0+(c`shiftR`18),0x80+((c`shiftR`12).&.0x3f),0x80+((c`shiftR`6).&.0x3f),0x80+c.&.0x3f]