@@ -32,6 +32,7 @@ module Data.Bytes.Parser.Ascii
3232 , opt
3333
3434 -- * Match Many
35+ , take
3536 , shortTrailedBy
3637 , takeShortWhile
3738
@@ -52,7 +53,7 @@ module Data.Bytes.Parser.Ascii
5253 , Latin. decWord32
5354 ) where
5455
55- import Prelude hiding (any , fail , length , takeWhile )
56+ import Prelude hiding (any , fail , length , takeWhile , take )
5657
5758import Control.Monad.ST (runST )
5859import Data.Bits (clearBit )
@@ -62,9 +63,11 @@ import Data.Char (ord)
6263import Data.Text.Short (ShortText )
6364import Data.Word (Word8 )
6465import GHC.Exts (Char (C #), Char #, Int (I #), Int #, chr #, gtChar #, indexCharArray #, ord #, (+#) , (-#) , (<#) )
66+ import Data.Text.Internal (Text (Text ))
6567
6668import qualified Data.ByteString.Short.Internal as BSS
6769import qualified Data.Bytes as Bytes
70+ import qualified Data.Bytes.Parser as Parser
6871import qualified Data.Bytes.Parser.Latin as Latin
6972import qualified Data.Bytes.Parser.Unsafe as Unsafe
7073import qualified Data.Primitive as PM
@@ -103,6 +106,15 @@ skipTrailedBy e !c = do
103106 else go
104107 go
105108
109+ -- | Consume a fixed number of ASCII characters (all less than codepoint 128).
110+ take :: e -> Int -> Parser e s Text
111+ {-# INLINE take #-}
112+ take e ! n = do
113+ bs@ (Bytes arr off len) <- Parser. take e n
114+ if Bytes. all (\ w -> w < 128 ) bs
115+ then pure (Text arr off len)
116+ else Parser. fail e
117+
106118{- | Consume characters matching the predicate. The stops when it
107119encounters a non-matching character or when it encounters a byte
108120above @0x7F@. This never fails.
0 commit comments