@@ -0,0 +1,67 @@
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+--
9+module Streamly.Internal.Unicode.Utf8
10+ (
11+ -- * Type
12+ Utf8
13+
14+ -- * Creation and elimination
15+ , pack
16+ , unpack
17+ , toArray
18+ )
19+where
20+
21+--------------------------------------------------------------------------------
22+-- Imports
23+--------------------------------------------------------------------------------
24+
25+import Control.DeepSeq (NFData)
26+import Data.Word (Word8)
27+import Streamly.Internal.Data.Array.Foreign.Type (Array)
28+import System.IO.Unsafe (unsafePerformIO)
29+
30+import qualified Streamly.Internal.Data.Array.Foreign as Array
31+import qualified Streamly.Internal.Data.Stream.IsStream as Stream
32+import qualified Streamly.Internal.Unicode.Stream as Unicode
33+
34+--------------------------------------------------------------------------------
35+-- Type
36+--------------------------------------------------------------------------------
37+
38+-- | A space efficient, packed, unboxed Unicode container.
39+newtype Utf8 =
40+ Utf8 (Array Word8)
41+ deriving (NFData)
42+
43+--------------------------------------------------------------------------------
44+-- Functions
45+--------------------------------------------------------------------------------
46+
47+{-# INLINE toArray #-}
48+toArray :: Utf8 -> Array Word8
49+toArray (Utf8 arr) = arr
50+
51+
52+{-# INLINEABLE pack #-}
53+pack :: String -> Utf8
54+pack s =
55+ Utf8
56+ $ unsafePerformIO
57+ $ Array.fromStreamN len $ Unicode.encodeUtf8' $ Stream.fromList s
58+
59+ where
60+
61+ len = length s
62+
63+{-# INLINEABLE unpack #-}
64+unpack :: Utf8 -> String
65+unpack u =
66+ unsafePerformIO
67+ $ Stream.toList $ Unicode.decodeUtf8' $ Array.toStream $ toArray u