on 7bc450d help sign in
73 lines 2.1 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
9-- XXX We can move this to stream-core/streamly-unicode-core, and provide an
10-- additional module in streamly-unicode for case conversions (because it
11-- depends on unicode-data). Or just keep all of it in streamly-unicode
12-- which will have a dependency on unicode-data.
14module Streamly.Internal.Unicode.Utf8
15 (
16 -- * Type
17 Utf8
19 -- * Creation and elimination
20 , pack
21 , unpack
22 , toArray
23 )
24where
26--------------------------------------------------------------------------------
27-- Imports
28--------------------------------------------------------------------------------
30import Data.Word (Word8)
31import Streamly.Data.Array (Array)
32import System.IO.Unsafe (unsafePerformIO)
34import qualified Streamly.Data.Fold as Fold
35import qualified Streamly.Data.Stream as Stream
36import qualified Streamly.Internal.Data.Array as Array
37 ( fromStreamN
38 , read
39 )
40import qualified Streamly.Internal.Unicode.Stream as Unicode
42--------------------------------------------------------------------------------
43-- Type
44--------------------------------------------------------------------------------
46-- | A space efficient, packed, unboxed Unicode container.
47newtype Utf8 = Utf8 (Array Word8)
49--------------------------------------------------------------------------------
50-- Functions
51--------------------------------------------------------------------------------
53{-# INLINE toArray #-}
54toArray :: Utf8 -> Array Word8
55toArray (Utf8 arr) = arr
58{-# INLINEABLE pack #-}
59pack :: String -> Utf8
60pack s =
61 Utf8
62 $ unsafePerformIO
63 $ Array.fromStreamN len $ Unicode.encodeUtf8' $ Stream.fromList s
65 where
67 len = length s
69{-# INLINEABLE unpack #-}
70unpack :: Utf8 -> String
71unpack u =
72 unsafePerformIO
73 $ Stream.fold Fold.toList $ Unicode.decodeUtf8' $ Array.read $ toArray u