on master help sign in
Move lengthWord16 to a more useful place

--HG-- extra : convert_revision : cdaa8561723c349e18e161b1932f1b8dc0aaa634

commit0bfcdf8af91e969976158bf5b5f8c72751e0d6c5 authorBryan O'Sullivan <bos@serpentine.com> dateThu 26 Aug 2010, 18:26 (16 years ago) parentc051a63
3 files changed+11−8
Data/Text.hs+2−1 Data/Text/Foreign.hs+1−7 Data/Text/Unsafe.hs+8
Data/Text.hs+2−1
@@ -195,7 +195,8 @@ import qualified Data.Text.Fusion.Common as S
195195import Data.Text.Fusion (stream, reverseStream, unstream)
196196import Data.Text.Internal (Text(..), empty, text, textP)
197197import 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)
199200import Data.Text.UnsafeChar (unsafeChr)
200201import qualified Data.Text.Encoding.Utf16 as U16
201202import Data.Text.Search (indices)
Data/Text/Foreign.hs+1−7
@@ -34,6 +34,7 @@ import Control.Exception (assert)
3434#endif
3535import Control.Monad.ST (unsafeIOToST)
3636import Data.Text.Internal (Text(..), empty)
37+import Data.Text.Unsafe (lengthWord16)
3738import qualified Data.Text.Array as A
3839import Data.Word (Word16)
3940import Foreign.Marshal.Alloc (allocaBytes)
@@ -73,13 +74,6 @@ fromPtr ptr len =
7374 A.unsafeWrite marr i =<< unsafeIOToST (peek p)
7475 loop (p `plusPtr` 2) (i + 1)
7576
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−
8377-- $lowlevel
8478--
8579-- Foreign functions that use UTF-16 internally may return indices in
Data/Text/Unsafe.hs+8
@@ -20,6 +20,7 @@ module Data.Text.Unsafe
2020 , reverseIter
2121 , unsafeHead
2222 , unsafeTail
23+ , lengthWord16
2324 ) where
2425
2526#if defined(ASSERTS)
@@ -124,3 +125,10 @@ inlineInterleaveST :: ST s a -> ST s a
124125inlineInterleaveST (ST m) = ST $ \ s ->
125126 let r = case m s of (# _, res #) -> res in (# s, r #)
126127{-# 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 #-}