on master help sign in
Add tests and documentation

--HG-- extra : convert_revision : 554cd4cc7306e50f4fbd4058cad5d4b6420c78af

commit749c5c868e689ff5dc4f513f1d472cd685d65b68 authorBryan O'Sullivan <bos@serpentine.com> dateWed 25 Aug 2010, 05:11 (16 years ago) parent881b846
2 files changed+22
Data/Text/Foreign.hs+8 tests/Properties.hs+14
Data/Text/Foreign.hs+8
@@ -24,6 +24,7 @@ module Data.Text.Foreign
2424 , lengthWord16
2525 , unsafeCopyToPtr
2626 -- * Low-level manipulation
27+ -- $lowlevel
2728 , dropWord16
2829 , takeWord16
2930 ) where
@@ -79,6 +80,13 @@ lengthWord16 :: Text -> Int
7980lengthWord16 (Text _arr _off len) = len
8081{-# INLINE lengthWord16 #-}
8182
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+
8290-- | /O(1)/ Return the prefix of the 'Text' of @n@ 'Word16' units in
8391-- length.
8492--
tests/Properties.hs+14
@@ -17,6 +17,7 @@ import qualified Data.Text.Lazy as TL
1717import qualified Data.Text.Lazy.Builder as TB
1818import qualified Data.Text.Encoding as E
1919import Control.Exception (SomeException, evaluate, try)
20+import Data.Text.Foreign
2021import qualified Data.Text.Fusion as S
2122import qualified Data.Text.Fusion.Common as S
2223import Data.Text.Fusion.Size
@@ -573,6 +574,13 @@ t_builderAssociative s1 s2 s3 = TB.toLazyText (b1 `mappend` (b2 `mappend` b3)) =
573574 b2 = TB.fromText (packS s2)
574575 b3 = TB.fromText (packS s3)
575576
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+
576584-- Regression tests.
577585s_filter_eq s = S.filter p t == S.streamList (filter p s)
578586 where p = (/= S.last t)
@@ -955,5 +963,11 @@ tests = [
955963 testProperty "t_builderSingleton" t_builderSingleton,
956964 testProperty "t_builderFromText" t_builderFromText,
957965 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
958972 ]
959973 ]