on master help sign in
Add the I16 type
e32437eBryan O'Sullivan
Data/Text/Foreign.hs+16−12
@@ -1,4 +1,4 @@
1−{-# LANGUAGE BangPatterns, CPP #-}
1+{-# LANGUAGE BangPatterns, CPP, GeneralizedNewtypeDeriving #-}
2-- |
2-- |
3-- Module : Data.Text.Foreign
3-- Module : Data.Text.Foreign
4-- Copyright : (c) 2009, 2010 Bryan O'Sullivan
4-- Copyright : (c) 2009, 2010 Bryan O'Sullivan
@@ -16,9 +16,9 @@ module Data.Text.Foreign
16 (
16 (
17 -- * Interoperability with native code
17 -- * Interoperability with native code
18 -- $interop
18 -- $interop
19−
19+ I16
20 -- * Safe conversion functions
20 -- * Safe conversion functions
21− fromPtr
21+ , fromPtr
22 , useAsPtr
22 , useAsPtr
23 -- * Unsafe conversion code
23 -- * Unsafe conversion code
24 , lengthWord16
24 , lengthWord16
@@ -54,13 +54,17 @@ import Foreign.Storable (peek, poke)
54-- internal representations, such as UTF-8 or UTF-32, consider using
54-- internal representations, such as UTF-8 or UTF-32, consider using
55-- the functions in the 'Data.Text.Encoding' module.
55-- the functions in the 'Data.Text.Encoding' module.
56
56
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+
57-- | /O(n)/ Create a new 'Text' from a 'Ptr' 'Word16' by copying the
61-- | /O(n)/ Create a new 'Text' from a 'Ptr' 'Word16' by copying the
58-- contents of the array.
62-- contents of the array.
59fromPtr :: Ptr Word16 -- ^ source array
63fromPtr :: Ptr Word16 -- ^ source array
60− -> Int -- ^ length of source array (in 'Word16' units)
64+ -> I16 -- ^ length of source array (in 'Word16' units)
61 -> IO Text
65 -> IO Text
62−fromPtr _ 0 = return empty
66+fromPtr _ (I16 0) = return empty
63−fromPtr ptr len =
67+fromPtr ptr (I16 len) =
64#if defined(ASSERTS)
68#if defined(ASSERTS)
65 assert (len > 0) $
69 assert (len > 0) $
66#endif
70#endif
@@ -87,8 +91,8 @@ fromPtr ptr len =
87-- If @n@ would cause the 'Text' to end inside a surrogate pair, the
91-- If @n@ would cause the 'Text' to end inside a surrogate pair, the
88-- end of the prefix will be advanced by one additional 'Word16' unit
92-- end of the prefix will be advanced by one additional 'Word16' unit
89-- to maintain its validity.
93-- to maintain its validity.
90−takeWord16 :: Int -> Text -> Text
94+takeWord16 :: I16 -> Text -> Text
91−takeWord16 n t@(Text arr off len)
95+takeWord16 (I16 n) t@(Text arr off len)
92 | n <= 0 = empty
96 | n <= 0 = empty
93 | n >= len || m >= len = t
97 | n >= len || m >= len = t
94 | otherwise = Text arr off m
98 | otherwise = Text arr off m
@@ -103,8 +107,8 @@ takeWord16 n t@(Text arr off len)
103-- If @n@ would cause the 'Text' to begin inside a surrogate pair, the
107-- If @n@ would cause the 'Text' to begin inside a surrogate pair, the
104-- beginning of the suffix will be advanced by one additional 'Word16'
108-- beginning of the suffix will be advanced by one additional 'Word16'
105-- unit to maintain its validity.
109-- unit to maintain its validity.
106−dropWord16 :: Int -> Text -> Text
110+dropWord16 :: I16 -> Text -> Text
107−dropWord16 n t@(Text arr off len)
111+dropWord16 (I16 n) t@(Text arr off len)
108 | n <= 0 = t
112 | n <= 0 = t
109 | n >= len || m >= len = empty
113 | n >= len || m >= len = empty
110 | otherwise = Text arr (off+m) (len-m)
114 | otherwise = Text arr (off+m) (len-m)
@@ -126,8 +130,8 @@ unsafeCopyToPtr (Text arr off len) ptr = loop ptr off
126
130
127-- | /O(n)/ Perform an action on a temporary, mutable copy of a
131-- | /O(n)/ Perform an action on a temporary, mutable copy of a
128-- 'Text'. The copy is freed as soon as the action returns.
132-- '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
130useAsPtr t@(Text _arr _off len) action =
134useAsPtr t@(Text _arr _off len) action =
131 allocaBytes (len * 2) $ \buf -> do
135 allocaBytes (len * 2) $ \buf -> do
132 unsafeCopyToPtr t buf
136 unsafeCopyToPtr t buf
133− action (castPtr buf) len
137+ action (castPtr buf) (fromIntegral len)