on master help sign in
Move lengthWord16 to a more useful place
0bfcdf8Bryan O'Sullivan
Data/Text.hs+2−1
@@ -195,7 +195,8 @@ import qualified Data.Text.Fusion.Common as S
195import Data.Text.Fusion (stream, reverseStream, unstream)
195import Data.Text.Fusion (stream, reverseStream, unstream)
196import Data.Text.Internal (Text(..), empty, text, textP)
196import Data.Text.Internal (Text(..), empty, text, textP)
197import qualified Prelude as P
197import qualified Prelude as P
198−import Data.Text.Unsafe (Iter(..), iter, iter_, reverseIter, unsafeHead, unsafeTail)
198+import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
199+ unsafeHead, unsafeTail)
199import Data.Text.UnsafeChar (unsafeChr)
200import Data.Text.UnsafeChar (unsafeChr)
200import qualified Data.Text.Encoding.Utf16 as U16
201import qualified Data.Text.Encoding.Utf16 as U16
201import Data.Text.Search (indices)
202import Data.Text.Search (indices)
Data/Text/Foreign.hs+1−7
@@ -34,6 +34,7 @@ import Control.Exception (assert)
34#endif
34#endif
35import Control.Monad.ST (unsafeIOToST)
35import Control.Monad.ST (unsafeIOToST)
36import Data.Text.Internal (Text(..), empty)
36import Data.Text.Internal (Text(..), empty)
37+import Data.Text.Unsafe (lengthWord16)
37import qualified Data.Text.Array as A
38import qualified Data.Text.Array as A
38import Data.Word (Word16)
39import Data.Word (Word16)
39import Foreign.Marshal.Alloc (allocaBytes)
40import Foreign.Marshal.Alloc (allocaBytes)
@@ -73,13 +74,6 @@ fromPtr ptr len =
73 A.unsafeWrite marr i =<< unsafeIOToST (peek p)
74 A.unsafeWrite marr i =<< unsafeIOToST (peek p)
74 loop (p `plusPtr` 2) (i + 1)
75 loop (p `plusPtr` 2) (i + 1)
75
76
76−-- | /O(1)/ Return the length of a 'Text' in units of 'Word16'. This
77−-- is useful for sizing a target array appropriately before using
78−-- 'unsafeCopyToPtr'.
79−lengthWord16 :: Text -> Int
80−lengthWord16 (Text _arr _off len) = len
81−{-# INLINE lengthWord16 #-}
82−
83-- $lowlevel
77-- $lowlevel
84--
78--
85-- Foreign functions that use UTF-16 internally may return indices in
79-- Foreign functions that use UTF-16 internally may return indices in
Data/Text/Unsafe.hs+8
@@ -20,6 +20,7 @@ module Data.Text.Unsafe
20 , reverseIter
20 , reverseIter
21 , unsafeHead
21 , unsafeHead
22 , unsafeTail
22 , unsafeTail
23+ , lengthWord16
23 ) where
24 ) where
24
25
25#if defined(ASSERTS)
26#if defined(ASSERTS)
@@ -124,3 +125,10 @@ inlineInterleaveST :: ST s a -> ST s a
124inlineInterleaveST (ST m) = ST $ \ s ->
125inlineInterleaveST (ST m) = ST $ \ s ->
125 let r = case m s of (# _, res #) -> res in (# s, r #)
126 let r = case m s of (# _, res #) -> res in (# s, r #)
126{-# INLINE inlineInterleaveST #-}
127{-# INLINE inlineInterleaveST #-}
128+
129+-- | /O(1)/ Return the length of a 'Text' in units of 'Word16'. This
130+-- is useful for sizing a target array appropriately before using
131+-- 'unsafeCopyToPtr'.
132+lengthWord16 :: Text -> Int
133+lengthWord16 (Text _arr _off len) = len
134+{-# INLINE lengthWord16 #-}