{-# LANGUAGE BangPatterns #-}{-|
An immutable Unicode text type, optimized for low memory overhead. A
'SmallString' typically uses less memory than the corresponding 'T.Text'. Use
'SmallString' when storing a large number of short texts, for example when
indexing a map using words or short phrases.
To manipulate a 'SmallString', first convert it into a 'T.Text'. For more
information on working with 'T.Text', see the @text@ package:
http://hackage.haskell.org/package/text The Ord instance is not guaranteed to be the same as that of the corresponding
string.
-}moduleData.SmallString(SmallString,fromString,toString,fromText,toText)whereimportqualifiedData.SmallArrayasAimportqualifiedData.SmallArray.UnsafeasAimportqualifiedCodec.Binary.UTF8.StringasUTF8importData.Word(Word8)importqualifiedData.StringasS(IsString(..))importqualifiedData.TextasTimportqualifiedData.Text.EncodingasTimportqualifiedData.ByteStringasBimportqualifiedData.ByteString.InternalasBimportqualifiedData.ByteString.UnsafeasBimportControl.DeepSeqimportForeign.Ptr(Ptr,plusPtr)importForeign.Storable(poke)importData.HashableasH-- | A space efficient representation of Unicode text.newtypeSmallString=SmallString(A.ArrayWord8)instanceEqSmallStringwhere(==)=eqSmallStringinstanceOrdSmallStringwherecompare=compareSmallStringinstanceShowSmallStringwhereshow=show.toStringinstanceNFDataSmallStringwherernf(SmallStringarr)=rnfarrinstanceS.IsStringSmallStringwherefromString=fromStringinstanceH.HashableSmallStringwherehash(SmallStringarr)=H.hasharrcompareSmallString::SmallString->SmallString->OrderingcompareSmallString(SmallStringlhsAry)(SmallStringrhsAry)=comparelhsAryrhsAryeqSmallString::SmallString->SmallString->BooleqSmallString(SmallStringlhs)(SmallStringrhs)=lhs==rhs-- | Convert a 'String' into a 'SmallString'.fromString::String->SmallStringfromString=SmallString.A.fromList.UTF8.encode-- | Convert a 'SmallString' into a 'String'.toString::SmallString->StringtoString(SmallStringary)=UTF8.decode.A.toList$ary-- | Convert a 'T.Text' into a 'SmallString'.fromText::T.Text->SmallStringfromText=fromBS.T.encodeUtf8-- | Convert a 'SmallString' into a 'T.Text'.toText::SmallString->T.TexttoText=T.decodeUtf8.toBS-- | Convert a 'B.ByteString' into a 'SmallString', assuming that the-- 'B.ByteString' contains UTF-8 encoded text. This assumption is not checked.fromBS::B.ByteString->SmallStringfromBSbs=SmallString$letlen=B.lengthbsinA.run$doarr<-A.unsafeNewlenmapM_(\ix->A.unsafeWritearrix(B.unsafeIndexbsix))[0..len]returnarr-- | Convert a 'SmallString' into a 'B.ByteString'. The 'B.ByteString' will-- contain UTF-8 encoded text.toBS::SmallString->B.ByteStringtoBS(SmallStringary)=B.unsafeCreatelen(goary0)wherelen=A.lengtharygo::A.ArrayWord8->Int->PtrWord8->IO()go!ary!i!p|i<len=dopokep(A.unsafeIndexaryi)goary(i+1)(p`plusPtr`1)|otherwise=return()