on master help sign in
Add tests and documentation
749c5c8Bryan O'Sullivan
Data/Text/Foreign.hs+8
@@ -24,6 +24,7 @@ module Data.Text.Foreign
24 , lengthWord16
24 , lengthWord16
25 , unsafeCopyToPtr
25 , unsafeCopyToPtr
26 -- * Low-level manipulation
26 -- * Low-level manipulation
27+ -- $lowlevel
27 , dropWord16
28 , dropWord16
28 , takeWord16
29 , takeWord16
29 ) where
30 ) where
@@ -79,6 +80,13 @@ lengthWord16 :: Text -> Int
79lengthWord16 (Text _arr _off len) = len
80lengthWord16 (Text _arr _off len) = len
80{-# INLINE lengthWord16 #-}
81{-# INLINE lengthWord16 #-}
81
82
83+-- $lowlevel
84+--
85+-- Foreign functions that use UTF-16 internally may return indices in
86+-- units of 'Word16' instead of characters. These functions may
87+-- safely be used with such indices, as they will adjust offsets if
88+-- necessary to preserve the validity of a Unicode string.
89+
82-- | /O(1)/ Return the prefix of the 'Text' of @n@ 'Word16' units in
90-- | /O(1)/ Return the prefix of the 'Text' of @n@ 'Word16' units in
83-- length.
91-- length.
84--
92--
tests/Properties.hs+14
@@ -17,6 +17,7 @@ import qualified Data.Text.Lazy as TL
17import qualified Data.Text.Lazy.Builder as TB
17import qualified Data.Text.Lazy.Builder as TB
18import qualified Data.Text.Encoding as E
18import qualified Data.Text.Encoding as E
19import Control.Exception (SomeException, evaluate, try)
19import Control.Exception (SomeException, evaluate, try)
20+import Data.Text.Foreign
20import qualified Data.Text.Fusion as S
21import qualified Data.Text.Fusion as S
21import qualified Data.Text.Fusion.Common as S
22import qualified Data.Text.Fusion.Common as S
22import Data.Text.Fusion.Size
23import Data.Text.Fusion.Size
@@ -573,6 +574,13 @@ t_builderAssociative s1 s2 s3 = TB.toLazyText (b1 `mappend` (b2 `mappend` b3)) =
573 b2 = TB.fromText (packS s2)
574 b2 = TB.fromText (packS s2)
574 b3 = TB.fromText (packS s3)
575 b3 = TB.fromText (packS s3)
575
576
577+-- Low-level.
578+
579+t_dropWord16 m t = dropWord16 m t `T.isSuffixOf` t
580+t_takeWord16 m t = takeWord16 m t `T.isPrefixOf` t
581+t_take_drop_16 m t = T.append (takeWord16 n t) (dropWord16 n t) == t
582+ where n = small m
583+
576-- Regression tests.
584-- Regression tests.
577s_filter_eq s = S.filter p t == S.streamList (filter p s)
585s_filter_eq s = S.filter p t == S.streamList (filter p s)
578 where p = (/= S.last t)
586 where p = (/= S.last t)
@@ -955,5 +963,11 @@ tests = [
955 testProperty "t_builderSingleton" t_builderSingleton,
963 testProperty "t_builderSingleton" t_builderSingleton,
956 testProperty "t_builderFromText" t_builderFromText,
964 testProperty "t_builderFromText" t_builderFromText,
957 testProperty "t_builderAssociative" t_builderAssociative
965 testProperty "t_builderAssociative" t_builderAssociative
966+ ],
967+
968+ testGroup "lowlevel" [
969+ testProperty "t_dropWord16" t_dropWord16,
970+ testProperty "t_takeWord16" t_takeWord16,
971+ testProperty "t_take_drop_16" t_take_drop_16
958 ]
972 ]
959 ]
973 ]