module Data.String.Like
( StringLike(..)
, ToString(..)
, string
, text, ltext
, bs, lbs
, readFile
, writeFile
, appendFile
) where
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Word (Word, Word8, Word16, Word32, Word64)
import qualified Data.ByteString as SB
import qualified Data.ByteString.Lazy as LB
import Data.Text.Lazy.Builder (toLazyText)
import Data.Text.Lazy.Builder.Int (decimal)
import Data.Text.Lazy.Builder.RealFloat (realFloat)
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Encoding as LT
import qualified Data.Text as ST
import qualified Data.Text.Encoding as ST
class StringLike a where
fromLazyText :: LT.Text -> a
class ToString a where
toText :: a -> LT.Text
text :: ToString a => a -> ST.Text
text = string
ltext :: ToString a => a -> LT.Text
ltext = string
bs :: ToString a => a -> SB.ByteString
bs = string
lbs :: ToString a => a -> LB.ByteString
lbs = string
string :: (ToString a, StringLike b) => a -> b
string = fromLazyText . toText
instance StringLike ST.Text where
fromLazyText = LT.toStrict
instance StringLike LT.Text where
fromLazyText = id
instance StringLike LB.ByteString where
fromLazyText = LT.encodeUtf8
instance StringLike SB.ByteString where
fromLazyText = lazyByteStringToStrict . fromLazyText
instance ToString Int where
toText = toLazyText . decimal
instance ToString Int8 where
toText = toLazyText . decimal
instance ToString Int16 where
toText = toLazyText . decimal
instance ToString Int32 where
toText = toLazyText . decimal
instance ToString Int64 where
toText = toLazyText . decimal
instance ToString Word where
toText = toLazyText . decimal
instance ToString Word8 where
toText = toLazyText . decimal
instance ToString Word16 where
toText = toLazyText . decimal
instance ToString Word32 where
toText = toLazyText . decimal
instance ToString Word64 where
toText = toLazyText . decimal
instance ToString Integer where
toText = toLazyText . decimal
instance ToString Double where
toText = toLazyText . realFloat
instance ToString Float where
toText = toLazyText . realFloat
instance ToString String where
toText = LT.pack
instance ToString LT.Text where
toText = id
instance ToString ST.Text where
toText = LT.fromStrict
instance ToString SB.ByteString where
toText = LT.fromStrict . ST.decodeUtf8
instance ToString LB.ByteString where
toText = LT.decodeUtf8
lazyByteStringToStrict :: LB.ByteString -> SB.ByteString
#if MIN_VERSION_bytestring(0, 10, 0)
lazyByteStringToStrict = LB.toStrict
#else
lazyByteStringToStrict = SB.concat . LB.toChunks
#endif