bytestring-to-vector-0.3.0.1/src/0000755000000000000000000000000013427201237014741 5ustar0000000000000000bytestring-to-vector-0.3.0.1/src/Data/0000755000000000000000000000000013427201237015612 5ustar0000000000000000bytestring-to-vector-0.3.0.1/src/Data/Vector/0000755000000000000000000000000013427201237017054 5ustar0000000000000000bytestring-to-vector-0.3.0.1/src/Data/Vector/Storable/0000755000000000000000000000000013427201237020627 5ustar0000000000000000bytestring-to-vector-0.3.0.1/test/0000755000000000000000000000000013427201646015135 5ustar0000000000000000bytestring-to-vector-0.3.0.1/src/Data/Vector/Storable/ByteString.hs0000644000000000000000000000245113427201237023257 0ustar0000000000000000-- | Convert between @ByteString@ and @Vector.Storable@ -- without copying. module Data.Vector.Storable.ByteString ( -- | See also the caveats mentioned in the package's -- top-level documentation. byteStringToVector , vectorToByteString ) where import qualified Data.ByteString as BS import qualified Data.ByteString.Internal as BS import qualified Data.Vector.Storable as V import Foreign.Storable import Foreign.ForeignPtr sizeOfElem :: (Storable a) => V.Vector a -> Int sizeOfElem vec = sizeOf (undefined `asTypeOf` V.head vec) -- | Convert a @'BS.ByteString'@ to a @'V.Vector'@. -- -- This function can produce @'Vector'@s which do not obey -- architectural alignment requirements. On @x86@ this should -- not be an issue. byteStringToVector :: (Storable a) => BS.ByteString -> V.Vector a byteStringToVector bs = vec where vec = V.unsafeFromForeignPtr (castForeignPtr fptr) (scale off) (scale len) (fptr, off, len) = BS.toForeignPtr bs scale = (`div` sizeOfElem vec) -- | Convert a @'V.Vector'@ to a @'BS.ByteString'@. vectorToByteString :: (Storable a) => V.Vector a -> BS.ByteString vectorToByteString vec = BS.fromForeignPtr (castForeignPtr fptr) (scale off) (scale len) where (fptr, off, len) = V.unsafeToForeignPtr vec scale = (* sizeOfElem vec) bytestring-to-vector-0.3.0.1/test/Main.hs0000644000000000000000000000520413427201237016352 0ustar0000000000000000{-# LANGUAGE ScopedTypeVariables #-} module Main ( main ) where import qualified Data.ByteString as B import qualified Data.Vector.Storable as V import Data.Word import System.ByteOrder -- package 'byteorder' import Test.QuickCheck import Foreign.Storable import Data.Vector.Storable.ByteString arbList :: (Bounded a, Integral a) => Gen [a] arbList = choose (0, 16384) >>= flip vectorOf arbitraryBoundedIntegral instance Arbitrary B.ByteString where arbitrary = fmap B.pack arbList instance (Bounded a, Integral a, Storable a) => Arbitrary (V.Vector a) where arbitrary = fmap V.fromList arbList -- Reference implementations fromBE, fromHost :: (Integral a) => [Word8] -> a fromBE = foldl (\n x -> n * 256 + fromIntegral x) 0 fromHost = case byteOrder of LittleEndian -> fromBE . reverse BigEndian -> fromBE toLE, toHost :: (Integral a) => Int -> a -> [Word8] toLE sz = take sz . map (fromIntegral . (`mod` 256)) . iterate (`div` 256) toHost = case byteOrder of LittleEndian -> toLE BigEndian -> \sz -> reverse . toLE sz data Rep a = Rep Int ([Word8] -> a) (a -> [Word8]) rep8 :: Rep Word8 rep8 = Rep 1 (\[x] -> x) return repN :: forall a . (Storable a, Integral a) => Rep a repN = let n = sizeOf (undefined :: a) in Rep n fromHost (toHost n) ref_byteStringToVector :: (Storable a) => Rep a -> B.ByteString -> V.Vector a ref_byteStringToVector (Rep n f _) = V.fromList . go where go bs = case B.splitAt n bs of (x, xs) | B.length x == n -> f (B.unpack x) : go xs _ -> [] ref_vectorToByteString :: (Storable a) => Rep a -> V.Vector a -> B.ByteString ref_vectorToByteString (Rep _ _ f) = B.pack . concatMap f . V.toList -- Properties mkProp_inv _ x = byteStringToVector (vectorToByteString x) == x mkProp_inv_ref r x = ref_byteStringToVector r (ref_vectorToByteString r x) == x mkProp_inv_ref1 r x = ref_byteStringToVector r (vectorToByteString x) == x mkProp_inv_ref2 r x = byteStringToVector (ref_vectorToByteString r x) == x mkProp_eq_BV r x = ref_byteStringToVector r x == byteStringToVector x mkProp_eq_VB r x = ref_vectorToByteString r x == vectorToByteString x -- Test runner runFor :: (Integral a, Bounded a, Storable a, Show a) => Rep a -> IO () runFor r = do mapM_ (quickCheck . ($ r)) [ mkProp_inv , mkProp_inv_ref , mkProp_inv_ref1 , mkProp_inv_ref2 , mkProp_eq_VB ] quickCheck (mkProp_eq_BV r) main :: IO () main = do runFor rep8 runFor (repN :: Rep Word16) runFor (repN :: Rep Word32) runFor (repN :: Rep Word64) bytestring-to-vector-0.3.0.1/LICENSE0000644000000000000000000000271413427201237015163 0ustar0000000000000000Copyright (c) Keegan McAllister 2011 All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. 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. 3. Neither the name of the author nor the names of his 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 AUTHORS 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. bytestring-to-vector-0.3.0.1/Setup.hs0000755000000000000000000000010613427201237015606 0ustar0000000000000000#! /usr/bin/runhaskell import Distribution.Simple main = defaultMain bytestring-to-vector-0.3.0.1/bytestring-to-vector.cabal0000644000000000000000000000323113427205406021251 0ustar0000000000000000name: bytestring-to-vector version: 0.3.0.1 license: BSD3 license-file: LICENSE synopsis: Convert between ByteString and Vector.Storable without copying homepage: https://github.com/sheyll/bytestring-to-vector category: Data Structures author: Keegan McAllister maintainer: Sven Heyll build-type: Simple cabal-version: >=1.10 description: This library allows conversion between the types from @Data.ByteString@ (package @bytestring@) and @Data.Vector.Storable@ (package @vector@) without copying the underlying data. This is useful, for example, when @ByteString@ IO produces or consumes vectors of numbers in native byte order. . The conversion relies on the fact that @ByteString@ and @Vector@ use their respective @ForeignPtr@s in compatible ways. . This library is a fork of the @spool@ package written by Keegan McAllister. extra-source-files: README.md, stack.yaml, LICENSE, .travis.yml library exposed-modules: Data.Vector.Storable.ByteString hs-source-dirs: src ghc-options: -Wall default-language: Haskell2010 build-depends: base >= 3 && < 5 , bytestring , vector test-suite tests type: exitcode-stdio-1.0 hs-source-dirs: test main-is: Main.hs default-language: Haskell2010 build-depends: base >= 3 && < 5 , byteorder , bytestring-to-vector , bytestring , vector , QuickCheck source-repository head type: git location: git://github.com/sheyll/bytestring-to-vector.git bytestring-to-vector-0.3.0.1/README.md0000644000000000000000000000077413427205351015442 0ustar0000000000000000[![Build Status](https://travis-ci.org/sheyll/bytestring-to-vetcor.svg?branch=master)](https://travis-ci.org/sheyll/bytestring-to-vetcor) [![Hackage](https://img.shields.io/hackage/v/bytestring-to-vetcor.svg?style=flat)](http://hackage.haskell.org/packages/bytestring-to-vetcor) # Platform dependent no-copy vector - bytestring conversion It provides conversion between ByteString and Vector.Storable without copying the underlying data. This is a fork of [spool](https://github.com/kmcallister/spool). bytestring-to-vector-0.3.0.1/stack.yaml0000644000000000000000000000012713427201237016143 0ustar0000000000000000resolver: lts-13.1 packages: - '.' extra-deps: [] flags: {} extra-package-dbs: [] bytestring-to-vector-0.3.0.1/.travis.yml0000644000000000000000000000220213427202604016256 0ustar0000000000000000# This is the simple Travis configuration, which is intended for use # on applications which do not require cross-platform and # multiple-GHC-version support. For more information and other # options, see: # # https://docs.haskellstack.org/en/stable/travis_ci/ # # Copy these contents into the root directory of your Github project in a file # named .travis.yml # Choose a build environment dist: xenial # Do not choose a language; we provide our own build tools. language: generic # Caching so the next build will be fast too. cache: directories: - $HOME/.stack # Ensure necessary system libraries are present addons: apt: packages: - libgmp-dev before_install: # Download and unpack the stack executable - mkdir -p ~/.local/bin - export PATH=$HOME/.local/bin:$PATH - travis_retry curl -L https://get.haskellstack.org/stable/linux-x86_64.tar.gz | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack' install: # Build dependencies - stack --no-terminal --install-ghc test --only-dependencies script: # Build the package, its tests, and its docs and run the tests - stack --no-terminal test --haddock --no-haddock-deps