on master help sign in
Add the I16 type

--HG-- extra : convert_revision : 06e75bd8f8a8ac8bd30c4b9f51e7b68fea58a4be

commite32437ecd47a256ed83af826fa104529ab1ffad1 authorBryan O'Sullivan <bos@serpentine.com> dateMon 20 Sep 2010, 03:59 (16 years ago) parentd0c216b
1 file changed+16−12
Data/Text/Foreign.hs+16−12
Data/Text/Foreign.hs+16−12
@@ -1,4 +1,4 @@
1−{-# LANGUAGE BangPatterns, CPP #-}
1+{-# LANGUAGE BangPatterns, CPP, GeneralizedNewtypeDeriving #-}
22-- |
33-- Module : Data.Text.Foreign
44-- Copyright : (c) 2009, 2010 Bryan O'Sullivan
@@ -16,9 +16,9 @@ module Data.Text.Foreign
1616 (
1717 -- * Interoperability with native code
1818 -- $interop
19−
19+ I16
2020 -- * Safe conversion functions
21− fromPtr
21+ , fromPtr
2222 , useAsPtr
2323 -- * Unsafe conversion code
2424 , lengthWord16
@@ -54,13 +54,17 @@ import Foreign.Storable (peek, poke)
5454-- internal representations, such as UTF-8 or UTF-32, consider using
5555-- the functions in the 'Data.Text.Encoding' module.
5656
57+-- | A type representing a number of UTF-16 code units.
58+newtype I16 = I16 Int
59+ deriving (Bounded, Enum, Eq, Integral, Num, Ord, Read, Real, Show)
60+
5761-- | /O(n)/ Create a new 'Text' from a 'Ptr' 'Word16' by copying the
5862-- contents of the array.
5963fromPtr :: Ptr Word16 -- ^ source array
60− -> Int -- ^ length of source array (in 'Word16' units)
64+ -> I16 -- ^ length of source array (in 'Word16' units)
6165 -> IO Text
62−fromPtr _ 0 = return empty
63−fromPtr ptr len =
66+fromPtr _ (I16 0) = return empty
67+fromPtr ptr (I16 len) =
6468#if defined(ASSERTS)
6569 assert (len > 0) $
6670#endif
@@ -87,8 +91,8 @@ fromPtr ptr len =
8791-- If @n@ would cause the 'Text' to end inside a surrogate pair, the
8892-- end of the prefix will be advanced by one additional 'Word16' unit
8993-- to maintain its validity.
90−takeWord16 :: Int -> Text -> Text
91−takeWord16 n t@(Text arr off len)
94+takeWord16 :: I16 -> Text -> Text
95+takeWord16 (I16 n) t@(Text arr off len)
9296 | n <= 0 = empty
9397 | n >= len || m >= len = t
9498 | otherwise = Text arr off m
@@ -103,8 +107,8 @@ takeWord16 n t@(Text arr off len)
103107-- If @n@ would cause the 'Text' to begin inside a surrogate pair, the
104108-- beginning of the suffix will be advanced by one additional 'Word16'
105109-- unit to maintain its validity.
106−dropWord16 :: Int -> Text -> Text
107−dropWord16 n t@(Text arr off len)
110+dropWord16 :: I16 -> Text -> Text
111+dropWord16 (I16 n) t@(Text arr off len)
108112 | n <= 0 = t
109113 | n >= len || m >= len = empty
110114 | otherwise = Text arr (off+m) (len-m)
@@ -126,8 +130,8 @@ unsafeCopyToPtr (Text arr off len) ptr = loop ptr off
126130
127131-- | /O(n)/ Perform an action on a temporary, mutable copy of a
128132-- 'Text'. The copy is freed as soon as the action returns.
129−useAsPtr :: Text -> (Ptr Word16 -> Int -> IO a) -> IO a
133+useAsPtr :: Text -> (Ptr Word16 -> I16 -> IO a) -> IO a
130134useAsPtr t@(Text _arr _off len) action =
131135 allocaBytes (len * 2) $ \buf -> do
132136 unsafeCopyToPtr t buf
133− action (castPtr buf) len
137+ action (castPtr buf) (fromIntegral len)