Data.Monoid.Instances.ByteString.UTF8

monoid-subclasses-0.4.3.1: Subclasses of Monoid

Safe Haskell | None
Language | Haskell2010

Data.Monoid.Instances.ByteString.UTF8

Description

This module defines the `ByteStringUTF8` newtype wrapper around `ByteString`, together with its `TextualMonoid` instance. The `FactorialMonoid` instance of a wrapped `ByteStringUTF8` value differs from the original `ByteString`: the prime `factors` of the original value are its bytes, and for the wrapped value the prime `factors` are its valid UTF8 byte sequences. The following example session demonstrates the relationship:

    > let utf8@(ByteStringUTF8 bs) = fromString "E=mc\xb2"
    > bs
    "E=mc\194\178"
    > factors bs
    ["E","=","m","c","\194","\178"]
    > utf8
    "E=mc²"
    > factors utf8
    ["E","=","m","c","²"]

The `TextualMonoid` instance follows the same logic, but it also decodes all valid UTF8 sequences into characters. Any invalid UTF8 byte sequence from the original `ByteString` is preserved as a single prime factor:

    > let utf8'@(ByteStringUTF8 bs') = ByteStringUTF8 (Data.ByteString.map pred bs)
    > bs'
    "D<lb\193\177"
    > factors bs'
    ["D","<","l","b","\193","\177"]
    > utf8'
    "D<lb\[193,177]"
    > factors utf8'
    ["D","<","l","b","\[193,177]"]

Synopsis

- newtype ByteStringUTF8 = ByteStringUTF8 ByteString
- decode :: ByteString -> (ByteStringUTF8, ByteString)

# Documentation

newtype ByteStringUTF8 #

Constructors

ByteStringUTF8 ByteString |

Instances

Eq ByteStringUTF8 # |
Methods (==) :: ByteStringUTF8 -> ByteStringUTF8 -> Bool # (/=) :: ByteStringUTF8 -> ByteStringUTF8 -> Bool #
Ord ByteStringUTF8 # |
Methods compare :: ByteStringUTF8 -> ByteStringUTF8 -> Ordering # (<) :: ByteStringUTF8 -> ByteStringUTF8 -> Bool # (<=) :: ByteStringUTF8 -> ByteStringUTF8 -> Bool # (>) :: ByteStringUTF8 -> ByteStringUTF8 -> Bool # (>=) :: ByteStringUTF8 -> ByteStringUTF8 -> Bool # max :: ByteStringUTF8 -> ByteStringUTF8 -> ByteStringUTF8 # min :: ByteStringUTF8 -> ByteStringUTF8 -> ByteStringUTF8 #
Show ByteStringUTF8 # |
Methods showsPrec :: Int -> ByteStringUTF8 -> ShowS # show :: ByteStringUTF8 -> String # showList :: [ByteStringUTF8] -> ShowS #
IsString ByteStringUTF8 # |
Methods fromString :: String -> ByteStringUTF8 #
Monoid ByteStringUTF8 # |
Methods mempty :: ByteStringUTF8 # mappend :: ByteStringUTF8 -> ByteStringUTF8 -> ByteStringUTF8 # mconcat :: [ByteStringUTF8] -> ByteStringUTF8 #
PositiveMonoid ByteStringUTF8 # |
MonoidNull ByteStringUTF8 # |
Methods null :: ByteStringUTF8 -> Bool #
FactorialMonoid ByteStringUTF8 # |
Methods factors :: ByteStringUTF8 -> [ByteStringUTF8] # primePrefix :: ByteStringUTF8 -> ByteStringUTF8 # primeSuffix :: ByteStringUTF8 -> ByteStringUTF8 # splitPrimePrefix :: ByteStringUTF8 -> Maybe (ByteStringUTF8, ByteStringUTF8) # splitPrimeSuffix :: ByteStringUTF8 -> Maybe (ByteStringUTF8, ByteStringUTF8) # inits :: ByteStringUTF8 -> [ByteStringUTF8] # tails :: ByteStringUTF8 -> [ByteStringUTF8] # foldl :: (a -> ByteStringUTF8 -> a) -> a -> ByteStringUTF8 -> a # foldl' :: (a -> ByteStringUTF8 -> a) -> a -> ByteStringUTF8 -> a # foldr :: (ByteStringUTF8 -> a -> a) -> a -> ByteStringUTF8 -> a # length :: ByteStringUTF8 -> Int # foldMap :: Monoid n => (ByteStringUTF8 -> n) -> ByteStringUTF8 -> n # span :: (ByteStringUTF8 -> Bool) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8) # break :: (ByteStringUTF8 -> Bool) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8) # split :: (ByteStringUTF8 -> Bool) -> ByteStringUTF8 -> [ByteStringUTF8] # takeWhile :: (ByteStringUTF8 -> Bool) -> ByteStringUTF8 -> ByteStringUTF8 # dropWhile :: (ByteStringUTF8 -> Bool) -> ByteStringUTF8 -> ByteStringUTF8 # spanMaybe :: s -> (s -> ByteStringUTF8 -> Maybe s) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8, s) # spanMaybe' :: s -> (s -> ByteStringUTF8 -> Maybe s) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8, s) # splitAt :: Int -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8) # drop :: Int -> ByteStringUTF8 -> ByteStringUTF8 # take :: Int -> ByteStringUTF8 -> ByteStringUTF8 # reverse :: ByteStringUTF8 -> ByteStringUTF8 #
LeftGCDMonoid ByteStringUTF8 # |
Methods commonPrefix :: ByteStringUTF8 -> ByteStringUTF8 -> ByteStringUTF8 # stripCommonPrefix :: ByteStringUTF8 -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8, ByteStringUTF8) #
LeftCancellativeMonoid ByteStringUTF8 # |
LeftReductiveMonoid ByteStringUTF8 # |
Methods isPrefixOf :: ByteStringUTF8 -> ByteStringUTF8 -> Bool # stripPrefix :: ByteStringUTF8 -> ByteStringUTF8 -> Maybe ByteStringUTF8 #
TextualMonoid ByteStringUTF8 # |
Methods fromText :: Text -> ByteStringUTF8 # singleton :: Char -> ByteStringUTF8 # splitCharacterPrefix :: ByteStringUTF8 -> Maybe (Char, ByteStringUTF8) # characterPrefix :: ByteStringUTF8 -> Maybe Char # map :: (Char -> Char) -> ByteStringUTF8 -> ByteStringUTF8 # concatMap :: (Char -> ByteStringUTF8) -> ByteStringUTF8 -> ByteStringUTF8 # toString :: (ByteStringUTF8 -> String) -> ByteStringUTF8 -> String # any :: (Char -> Bool) -> ByteStringUTF8 -> Bool # all :: (Char -> Bool) -> ByteStringUTF8 -> Bool # foldl :: (a -> ByteStringUTF8 -> a) -> (a -> Char -> a) -> a -> ByteStringUTF8 -> a # foldl' :: (a -> ByteStringUTF8 -> a) -> (a -> Char -> a) -> a -> ByteStringUTF8 -> a # foldr :: (ByteStringUTF8 -> a -> a) -> (Char -> a -> a) -> a -> ByteStringUTF8 -> a # scanl :: (Char -> Char -> Char) -> Char -> ByteStringUTF8 -> ByteStringUTF8 # scanl1 :: (Char -> Char -> Char) -> ByteStringUTF8 -> ByteStringUTF8 # scanr :: (Char -> Char -> Char) -> Char -> ByteStringUTF8 -> ByteStringUTF8 # scanr1 :: (Char -> Char -> Char) -> ByteStringUTF8 -> ByteStringUTF8 # mapAccumL :: (a -> Char -> (a, Char)) -> a -> ByteStringUTF8 -> (a, ByteStringUTF8) # mapAccumR :: (a -> Char -> (a, Char)) -> a -> ByteStringUTF8 -> (a, ByteStringUTF8) # takeWhile :: (ByteStringUTF8 -> Bool) -> (Char -> Bool) -> ByteStringUTF8 -> ByteStringUTF8 # dropWhile :: (ByteStringUTF8 -> Bool) -> (Char -> Bool) -> ByteStringUTF8 -> ByteStringUTF8 # break :: (ByteStringUTF8 -> Bool) -> (Char -> Bool) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8) # span :: (ByteStringUTF8 -> Bool) -> (Char -> Bool) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8) # spanMaybe :: s -> (s -> ByteStringUTF8 -> Maybe s) -> (s -> Char -> Maybe s) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8, s) # spanMaybe' :: s -> (s -> ByteStringUTF8 -> Maybe s) -> (s -> Char -> Maybe s) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8, s) # split :: (Char -> Bool) -> ByteStringUTF8 -> [ByteStringUTF8] # find :: (Char -> Bool) -> ByteStringUTF8 -> Maybe Char # elem :: Char -> ByteStringUTF8 -> Bool # foldl_ :: (a -> Char -> a) -> a -> ByteStringUTF8 -> a # foldl_' :: (a -> Char -> a) -> a -> ByteStringUTF8 -> a # foldr_ :: (Char -> a -> a) -> a -> ByteStringUTF8 -> a # takeWhile_ :: Bool -> (Char -> Bool) -> ByteStringUTF8 -> ByteStringUTF8 # dropWhile_ :: Bool -> (Char -> Bool) -> ByteStringUTF8 -> ByteStringUTF8 # break_ :: Bool -> (Char -> Bool) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8) # span_ :: Bool -> (Char -> Bool) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8) # spanMaybe_ :: s -> (s -> Char -> Maybe s) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8, s) # spanMaybe_' :: s -> (s -> Char -> Maybe s) -> ByteStringUTF8 -> (ByteStringUTF8, ByteStringUTF8, s) #

decode :: ByteString -> (ByteStringUTF8, ByteString) #

Takes a raw `ByteString` chunk and returns a pair of `ByteStringUTF8` decoding the prefix of the chunk and the remaining suffix that is either null or contains the incomplete last character of the chunk.