network-attoparsec 0.11.2 → 0.12.1
raw patch · 3 files changed
+15/−11 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
network-attoparsec.cabal view
@@ -1,6 +1,6 @@ name: network-attoparsec category: Network, Parsing -version: 0.11.2 +version: 0.12.1 license: MIT license-file: LICENSE copyright: (c) 2015 Leon Mergen
src/Network/Attoparsec.hs view
@@ -54,7 +54,7 @@ -> ParseC a -- ^ Continuation parser state -> m (ParseC a, [a]) -- ^ Next parser state with parsed values parseMany s p0 pCur = do - buf <- readAvailable s + buf <- readAvailable s Nothing (p1, xs) <- parseBuffer p0 Many buf pCur return (p1, xs) @@ -62,10 +62,11 @@ -- single succesful parse on the socket, and guarantees that exactly one item -- will be parsed. -- --- __Warning:__ this function will /not/ work correctly when input data is --- pipelined. The parser might consume more data than required from the socket, --- or a partial second object is parsed, and the parser state and buffer will --- be discarded. +-- __Warning:__ In order to make this function work stable with pipelined data, +-- we read in data one byte at a time, which causes many context +-- switches and kernel syscalls, and furthermore causes a lot of +-- separate calls to attoparsec. So only use if performance is not +-- a consideration. -- -- The is typically used as follows: -- @@ -77,7 +78,7 @@ -> ParseC a -- ^ Initial parser state -> m a -- ^ Parsed value parseOne s p0 = do - buf <- readAvailable s + buf <- readAvailable s (Just 1) (p1, value) <- parseBuffer p0 Single buf p0 case value of @@ -99,6 +100,7 @@ -> m (ParseC a, [a]) -- ^ Next parser state with parsed values parseBuffer p0 mode = + let next bCur pCur = case pCur bCur of -- On error, throw error through MonadError @@ -129,13 +131,15 @@ readAvailable :: ( MonadIO m , MonadMask m) => NS.Socket + -> Maybe Int -> m BS.ByteString -readAvailable s = +readAvailable s Nothing = readAvailable s (Just 2048) +readAvailable s (Just bytes) = let buf :: IO (Maybe BS.ByteString) buf = do -- For some reason, Windows seems to be generating an exception sometimes -- when the remote has closed the connection - result <- tryAny $ NSB.recv s 2048 + result <- tryAny $ NSB.recv s bytes case result of Left _ -> return Nothing
test/Network/AttoparsecSpec.hs view
@@ -103,11 +103,11 @@ endOfLine return num - it "should return error when using single object parser" $ + it "should return not error when using single object parser" $ let writeSocket s = NS.send s "1234\n5678\n" readSocket s = Atto.parseOne s (Atto.parse numberParser) - in (pairSockets writeSocket readSocket) `shouldThrow` isUserError + in (pairSockets writeSocket readSocket) `shouldReturn` 1234 it "should return multiple objects when using multi object parser" $ let writeSocket s = NS.send s "1234\n5678\n"