on 43dda21 help sign in
67 lines 1.8 kB history raw
1-- |
2-- Module : Streamly.Internal.Unicode.Utf8
3-- Copyright : (c) 2021 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
7-- Portability : GHC
8--
9module Streamly.Internal.Unicode.Utf8
10 (
11 -- * Type
12 Utf8
14 -- * Creation and elimination
15 , pack
16 , unpack
17 , toArray
18 )
19where
21--------------------------------------------------------------------------------
22-- Imports
23--------------------------------------------------------------------------------
25import Control.DeepSeq (NFData)
26import Data.Word (Word8)
27import Streamly.Internal.Data.Array.Foreign.Type (Array)
28import System.IO.Unsafe (unsafePerformIO)
30import qualified Streamly.Internal.Data.Array.Foreign as Array
31import qualified Streamly.Internal.Data.Stream.IsStream as Stream
32import qualified Streamly.Internal.Unicode.Stream as Unicode
34--------------------------------------------------------------------------------
35-- Type
36--------------------------------------------------------------------------------
38-- | A space efficient, packed, unboxed Unicode container.
39newtype Utf8 =
40 Utf8 (Array Word8)
41 deriving (NFData)
43--------------------------------------------------------------------------------
44-- Functions
45--------------------------------------------------------------------------------
47{-# INLINE toArray #-}
48toArray :: Utf8 -> Array Word8
49toArray (Utf8 arr) = arr
52{-# INLINEABLE pack #-}
53pack :: String -> Utf8
54pack s =
55 Utf8
56 $ unsafePerformIO
57 $ Array.fromStreamN len $ Unicode.encodeUtf8' $ Stream.fromList s
59 where
61 len = length s
63{-# INLINEABLE unpack #-}
64unpack :: Utf8 -> String
65unpack u =
66 unsafePerformIO
67 $ Stream.toList $ Unicode.decodeUtf8' $ Array.toStream $ toArray u