attosplit (empty) → 0.0.1
raw patch · 4 files changed
+190/−0 lines, 4 filesdep +attoparsecdep +basedep +bytestringsetup-changed
Dependencies added: attoparsec, base, bytestring
Files
- Data/Attoparsec/Split.hs +83/−0
- LICENSE +29/−0
- Setup.hs +2/−0
- attosplit.cabal +76/−0
+ Data/Attoparsec/Split.hs view
@@ -0,0 +1,83 @@+-----------------------------------------------------------------------------+-- |+-- Module : Data.Attoparsec.Split+-- Copyright : 2010 Suite Solutions Ltd., Israel+--+-- Maintainer : Yitz Gale <yitzg@suite-sol.com>+-- Portability : portable+--+-- Split a lazy bytestring into a lazy list of lazy bytestrings at+-- boundaries defined by an attoparsec parser. The result of+-- a matching parse is included at the beginning of the+-- lazy bytestring which begins at that point.++{- Copyright (c) 2010 Suite Solutions Ltd., Israel. All rights reserved.+For licensing information, see the BSD3-style license in the file+LICENSE that was originally distributed by the author together with+this file. -}++module Data.Attoparsec.Split (split) where++import Data.Attoparsec (Parser, Result(..), parse)+import Data.ByteString.Lazy.Internal (ByteString(..), chunk)+import qualified Data.ByteString.Lazy as Lazy+import qualified Data.ByteString as B+import qualified Data.ByteString as Strict+import Data.Maybe (fromMaybe, isJust)++-- The result of examining the next group of bytes in the input stream+data SplitResult =+ -- A match of the parser, ending the current lazy bytestring+ Match !B.ByteString -- The result of the match, to be used as+ -- the prefix for the next component+ !ByteString -- The rest of the input after the match+ !Bool -- Did the match consume any input?+ | SplitChunk !B.ByteString -- A chunk of the current lazy bytestring+ deriving Show++isChunk :: SplitResult -> Bool+isChunk (SplitChunk _) = True+isChunk _ = False++asChunk :: SplitResult -> B.ByteString+asChunk (SplitChunk c) = c+asChunk _ = B.empty++-- | Split a lazy bytestring at boundaries defined by an attoparsec parser.+split :: Parser Strict.ByteString -> Lazy.ByteString -> [Lazy.ByteString]+split bdry = firstSplit . splitOne True bdry+ where+ firstSplit [] = []+ firstSplit result@(Match _ _ _ : _) = continue result+ firstSplit result = nextSplit B.empty result++ nextSplit pfx result =+ chunk pfx (foldr (chunk . asChunk) Empty result) :+ continue (dropWhile isChunk result)++ continue (Match pfx xs bump : _) = nextSplit pfx (splitOne bump bdry xs)+ continue _ = []++-- Split off the chunks of the first lazy bytestring from the input.+-- If the previous parser match did not consume any input, pass the+-- first byte through and only start looking for further parser matches+-- at the second byte.+-- A Match element will only occur as the last element of the result list.+-- If the last element is not a Match, end of input was reached.+splitOne :: Bool -> (Parser B.ByteString) -> ByteString -> [SplitResult]+splitOne _ _ Empty = []+splitOne bump bdry (Chunk x xs)+ | bump = go 0 Nothing . parse bdry $ x+ | otherwise = go 1 Nothing . parse bdry $ B.tail x+ where+ go n _ (Fail _ _ _)+ | n < B.length x = let n' = n + 1+ in go n' Nothing . parse bdry $ B.drop n' x+ | otherwise = SplitChunk x : splitOne True bdry xs+ go n ys (Done x' pfx) = [SplitChunk $ B.take n x,+ Match pfx (chunk x' $ fromMaybe xs ys) $+ isJust ys || B.length x' < B.length x - n]+ go n ys (Partial k) = goPartial n (fromMaybe xs ys) k++ goPartial n (Chunk y ys) k = go n (Just ys) $ k y+ goPartial n _ _ = go n Nothing $ Fail B.empty [] ""
+ LICENSE view
@@ -0,0 +1,29 @@+Copyright (c) 2010 Suite Solutions Ltd., Israel. All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Yitzchak Gale nor the name Suite Solutions+ nor the names of other contributors may be used to endorse or+ promote products derived from this software without specific+ prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ attosplit.cabal view
@@ -0,0 +1,76 @@+-- attosplit.cabal auto-generated by cabal init. For additional+-- options, see+-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.+-- The name of the package.+Name: attosplit++-- The package version. See the Haskell package versioning policy+-- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for+-- standards guiding when and how versions should be incremented.+Version: 0.0.1++-- A short (one-line) description of the package.+Synopsis:+ Split a lazy bytestring at boundaries defined by an attoparsec parser++-- A longer description of the package.+Description:+ Split a lazy bytestring into a lazy list of lazy bytestrings at+ boundaries defined by an attoparsec parser. The result of+ a matching parse is included at the beginning of the+ lazy bytestring which begins at that point.++-- URL for the project homepage or repository.+Homepage: http://projects.haskell.org/attosplit++-- The license under which the package is released.+License: BSD3++-- The file containing the license text.+License-file: LICENSE++-- The package author(s).+Author: Yitzchak Gale++-- An email address to which users can send suggestions, bug reports,+-- and patches.+Maintainer: yitz@community.haskell.org++-- A copyright notice.+Copyright:+ Copyright (c) 2010 Suite Solutions Ltd., Israel. All rights reserved.++Category: Data++Build-type: Simple++-- Extra files to be distributed with the package, such as examples or+-- a README.+-- Extra-source-files:++-- Constraint on the version of Cabal needed to build this package.+Cabal-version: >=1.6+++Library+ -- Modules exported by the library.+ Exposed-modules: Data.Attoparsec.Split++ -- Packages needed in order to build this package.+ Build-depends:+ base >= 3 && < 5,+ attoparsec == 0.8.*,+ bytestring == 0.9.*++ -- Modules not exported by this package.+ -- Other-modules:++ -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.+ -- Build-tools:++ GHC-options: -O2 -Wall+ GHC-prof-options: -auto-all++Source-repository HEAD+ Type: darcs+ Location: http://code.haskell.org/attosplit