packages feed

netlines 0.1.0 → 0.2.0

raw patch · 2 files changed

+54/−11 lines, 2 files

Files

Network/NetLines.hs view
@@ -14,6 +14,10 @@       netLine,       netLines, +      -- * Conversion to sparse lines+      netLineEmpty,+      netLinesEmpty,+       -- * Enumerators       enumHandleTimeout     )@@ -54,6 +58,19 @@     loop step = returnI step  +-- | Predicate whether the given byte is a line terminator (includes CR+-- and LF).++isEol :: Word8 -> Bool+isEol c = c == 10 || c == 13+++-- | Predicate whether the given byte is an LF line terminator.++isNotEol :: Word8 -> Bool+isNotEol = not . isEol++ -- | Savely read a line with the given maximum length.  If a longer line -- is enumerated, the excess data is dropped in constant space.  Returns -- 'Nothing' on EOF.@@ -67,14 +84,6 @@     lift (EB.dropWhile isEol) >> netLine' n      where-    isEol :: Word8 -> Bool-    isEol 10 = True-    isEol 13 = True-    isEol _  = False--    isNotEol :: Word8 -> Bool-    isNotEol = not . isEol-     netLine' :: Int -> MaybeT r (Iteratee ByteString m) ByteString     netLine' 0 = B.empty <$ lift (EB.dropWhile isNotEol)     netLine' n = do@@ -84,6 +93,25 @@           else return B.empty  +-- | Variant of 'netLine', which supports empty lines, useful for+-- protocols like HTTP, in which empty lines have a special meaning.+-- This function splits the input stream by LF characters while simply+-- ignoring CR characters.++netLineEmpty :: forall m. Monad m => Int -> Iteratee ByteString m (Maybe ByteString)+netLineEmpty maxLine =+    joinI $ E.map (B.filter (/= 13)) $$ evalMaybeT (netLineEmpty' maxLine)++    where+    netLineEmpty' :: forall r. Int -> MaybeT r (Iteratee ByteString m) ByteString+    netLineEmpty' 0 = B.empty <$ lift (EB.dropWhile (/= 10) >> EB.drop 1)+    netLineEmpty' n = do+        c <- liftF EB.head+        if c /= 10+          then B.cons c <$> netLineEmpty' (n-1)+          else return B.empty++ -- | Convert a stream of bytes to a stream of lines with the given -- maximum length.  Longer lines are silently truncated in constant -- space.@@ -94,6 +122,21 @@     loop :: Enumeratee ByteString ByteString m b     loop (Continue k) = do         mLine <- evalMaybeT $ netLine maxLen+        case mLine of+          Just line -> k (Chunks [line]) >>== loop+          Nothing   -> k EOF >>== loop+    loop step = return step+++-- | This is the same like 'netLines', but is based on 'netLinesEmpty'+-- to support empty lines.++netLinesEmpty :: forall b m. Monad m => Int -> Enumeratee ByteString ByteString m b+netLinesEmpty maxLen = loop+    where+    loop :: Enumeratee ByteString ByteString m b+    loop (Continue k) = do+        mLine <- netLineEmpty maxLen         case mLine of           Just line -> k (Chunks [line]) >>== loop           Nothing   -> k EOF >>== loop
netlines.cabal view
@@ -1,5 +1,5 @@ Name:          netlines-Version:       0.1.0+Version:       0.2.0 Category:      Network Synopsis:      Enumerator tools for text-based network protocols Maintainer:    Ertugrul Söylemez <es@ertes.de>@@ -14,8 +14,8 @@      Enumerator tools for text-based network protocols.  This includes,     among other things, an enumeratee to split an incoming ByteString-    stream to a length-limited line stream in a safe manner-    (i.e. constant space).+    stream to a length-limited line stream in a safe manner (i.e. in+    constant space).  Library   Build-depends: