git.iko.soy/typeable/attoparsec
on master help sign in
Document FastSet.
commit7c412f805d7b0460aa56cdc7d114c3cb26a32d33 authorBryan O'Sullivan <bos@serpentine.com> dateSun 27 Jan 2008, 08:39 (19 years ago) parent1a18bb6
1 file changed+11−7
src/Data/ParserCombinators/Attoparsec/FastSet.hs+11−7
src/Data/ParserCombinators/Attoparsec/FastSet.hs+11−7
@@ -10,7 +10,10 @@
1010-- Stability : experimental
1111-- Portability : unknown
1212--
13−-- Fast 8-bit character set membership.
13+-- Fast set membership tests for 'Word8' and 8-bit 'Char' values. The
14+-- set representation is unboxed for efficiency. For sets of fewer
15+-- than 32 elements, we test for membership using a binary search.
16+-- For larger sets, we use a lookup table.
1417--
1518-----------------------------------------------------------------------------
1619module Data.ParserCombinators.Attoparsec.FastSet
@@ -46,16 +49,12 @@ instance Show FastSet where
4649tableCutoff :: Int
4750tableCutoff = 32
4851
49−-- | Create a character set.
52+-- | Create a set.
5053set :: B.ByteString -> FastSet
5154set s | B.length s < tableCutoff = Sorted . B.sort $ s
5255 | otherwise = Table . mkTable $ s
5356
54−-- | Check the table for membership.
55−memberChar :: Char -> FastSet -> Bool
56−memberChar c = memberWord8 (I.c2w c)
57−
58−-- | Check the table for membership.
57+-- | Check the set for membership.
5958memberWord8 :: Word8 -> FastSet -> Bool
6059memberWord8 w (Table t) = U.unsafeIndex t (fromIntegral w) == entry
6160memberWord8 w (Sorted s) = search 0 (B.length s - 1)
@@ -68,6 +67,11 @@ memberWord8 w (Sorted s) = search 0 (B.length s - 1)
6867 LT -> search (mid + 1) hi
6968 _ -> True
7069
70+-- | Check the set for membership. Only works with 8-bit characters:
71+-- characters above code point 255 will give wrong answers.
72+memberChar :: Char -> FastSet -> Bool
73+memberChar c = memberWord8 (I.c2w c)
74+
7175-- | The value in a table that indicates that a character is not
7276-- present. We avoid NUL to make the table representation printable.
7377noEntry :: Word8