{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
{-# LANGUAGE MultiParamTypeClasses, UndecidableInstances #-}
module Data.String.UTF8
(
UTF8
, UTF8Bytes()
, fromString
, toString
, fromRep
, toRep
, G.replacement_char
, uncons
, splitAt
, take
, drop
, span
, break
, foldl
, foldr
, length
, lines
, lines'
, null
, decode
, byteSplitAt
, byteTake
, byteDrop
) where
import Prelude hiding (null,take,drop,span,break
,foldl,foldr,length,lines,splitAt)
import qualified Codec.Binary.UTF8.Generic as G
import Codec.Binary.UTF8.Generic (UTF8Bytes)
newtype UTF8 string = Str string deriving (Eq,Ord)
instance UTF8Bytes string index => Show (UTF8 string) where
show x = show (toString x)
fromRep :: string -> UTF8 string
fromRep = Str
toRep :: UTF8 string -> string
toRep (Str x) = x
fromString :: UTF8Bytes string index => String -> UTF8 string
fromString xs = Str (G.fromString xs)
toString :: UTF8Bytes string index => UTF8 string -> String
toString (Str xs) = G.toString xs
null :: UTF8Bytes string index => UTF8 string -> Bool
null (Str x) = G.null x
splitAt :: UTF8Bytes string index
=> index -> UTF8 string -> (UTF8 string, UTF8 string)
splitAt x (Str bs) = case G.splitAt x bs of
(s1,s2) -> (Str s1, Str s2)
byteSplitAt :: UTF8Bytes string index
=> index -> UTF8 string -> (UTF8 string, UTF8 string)
byteSplitAt n (Str x) = case G.bsplit n x of
(as,bs) -> (Str as, Str bs)
byteTake :: UTF8Bytes string index => index -> UTF8 string -> UTF8 string
byteTake n (Str x) = Str (fst (G.bsplit n x))
byteDrop :: UTF8Bytes string index => index -> UTF8 string -> UTF8 string
byteDrop n (Str x) = Str (G.bdrop n x)
take :: UTF8Bytes string index => index -> UTF8 string -> UTF8 string
take n (Str bs) = Str (G.take n bs)
drop :: UTF8Bytes string index => index -> UTF8 string -> UTF8 string
drop n (Str bs) = Str (G.drop n bs)
span :: UTF8Bytes string index
=> (Char -> Bool) -> UTF8 string -> (UTF8 string, UTF8 string)
span p (Str bs) = case G.span p bs of
(s1,s2) -> (Str s1, Str s2)
break :: UTF8Bytes string index
=> (Char -> Bool) -> UTF8 string -> (UTF8 string, UTF8 string)
break p (Str bs) = case G.break p bs of
(s1,s2) -> (Str s1, Str s2)
uncons :: UTF8Bytes string index
=> UTF8 string -> Maybe (Char, UTF8 string)
uncons (Str x) = do (c,y) <- G.uncons x
return (c, Str y)
decode :: UTF8Bytes string index => UTF8 string -> Maybe (Char, index)
decode (Str x) = G.decode x
foldr :: UTF8Bytes string index => (Char -> a -> a) -> a -> UTF8 string -> a
foldr cons nil (Str cs) = G.foldr cons nil cs
foldl :: UTF8Bytes string index => (a -> Char -> a) -> a -> UTF8 string -> a
foldl add acc (Str cs) = G.foldl add acc cs
length :: UTF8Bytes string index => UTF8 string -> index
length (Str b) = G.length b
lines :: UTF8Bytes string index => UTF8 string -> [UTF8 string]
lines (Str b) = map Str (G.lines b)
lines' :: UTF8Bytes string index => UTF8 string -> [UTF8 string]
lines' (Str x) = map Str (G.lines' x)