on master help sign in
Add Internal.Unicode.Utf8
commitf2906948daf8c5f1dd8fc0582319fe93f5094cb0 authorAdithya Kumar <adithya@composewell.com> dateThu 29 Jul 2021, 17:07 (5 years ago) parentde14835
2 files changed+68
src/Streamly/Internal/Unicode/Utf8.hs+67 streamly.cabal+1
src/Streamly/Internal/Unicode/Utf8.hsnew file+67
@@ -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
streamly.cabal+1
@@ -462,6 +462,7 @@ library
462462
463463 -- streamly-unicode
464464 , Streamly.Internal.Unicode.Stream
465+ , Streamly.Internal.Unicode.Utf8
465466 , Streamly.Internal.Unicode.Char
466467 , Streamly.Internal.Unicode.Array.Char
467468 , Streamly.Internal.Unicode.Array.Prim.Pinned