-- ------------------------------------------------------------

{- |
   Module     : Data.String.UTF8Decoding
   Copyright  : Copyright (C) 2010- Uwe Schmidt
   License    : MIT

   Maintainer : Uwe Schmidt (uwe@fh-wedel.de)
   Stability  : stable
   Portability: portable

   Interface for Data.Char.UTF8 funtions

-}

-- ------------------------------------------------------------

module Data.String.UTF8Decoding (
   decodeUtf8,
   decodeUtf8EmbedErrors,
   decodeUtf8IgnoreErrors,
   )
where

import qualified Data.String.UTF8 as UTF8
import           Data.Word (Word8)

-- | calls 'Data.Char.UTF8.decode' for parsing and decoding UTF-8

decodeUtf8      :: String -> (String, [String])
decodeUtf8 :: String -> (String, [String])
decodeUtf8 String
str
    = (String
res, forall a b. (a -> b) -> [a] -> [b]
map (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Error -> Int -> String
toErrStr) [(Error, Int)]
errs)
    where
    (String
res, [(Error, Int)]
errs) = [Word8] -> (String, [(Error, Int)])
UTF8.decode forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [Word8]
stringToByteString forall a b. (a -> b) -> a -> b
$ String
str

decodeUtf8IgnoreErrors  :: String -> String
decodeUtf8IgnoreErrors :: String -> String
decodeUtf8IgnoreErrors
    = forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> (String, [String])
decodeUtf8

decodeUtf8EmbedErrors   :: String -> [Either String Char]
decodeUtf8EmbedErrors :: String -> [Either String Char]
decodeUtf8EmbedErrors String
str
    = forall a b. (a -> b) -> [a] -> [b]
map (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> Either a b
Left forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Error -> Int -> String
toErrStr) forall a b. b -> Either a b
Right) forall a b. (a -> b) -> a -> b
$
      [Word8] -> [Either (Error, Int) Char]
UTF8.decodeEmbedErrors forall a b. (a -> b) -> a -> b
$ String -> [Word8]
stringToByteString forall a b. (a -> b) -> a -> b
$ String
str

stringToByteString :: String -> [Word8]
stringToByteString :: String -> [Word8]
stringToByteString = forall a b. (a -> b) -> [a] -> [b]
map (forall a. Enum a => Int -> a
toEnum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum)

toErrStr :: UTF8.Error -> Int -> String
toErrStr :: Error -> Int -> String
toErrStr Error
err Int
pos
        = String
" at input position " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
pos forall a. [a] -> [a] -> [a]
++ String
": " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Error
err

-- ------------------------------------------------------------