byteable-0.1.1/0000755000000000000000000000000012163225073011467 5ustar0000000000000000byteable-0.1.1/LICENSE0000644000000000000000000000272212163225073012477 0ustar0000000000000000Copyright (c) 2013 Vincent Hanquez 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 REGENTS 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. byteable-0.1.1/byteable.cabal0000644000000000000000000000207012163225073014241 0ustar0000000000000000Name: byteable Version: 0.1.1 Synopsis: Type class for sequence of bytes Description: Abstract class to manipulate sequence of bytes . The use case of this class is abstracting manipulation of types that are just wrapping a bytestring with stronger and more meaniful name. . Usual definition of those types are of the form: newtype MyType = MyType ByteString License: BSD3 License-file: LICENSE Copyright: Vincent Hanquez Author: Vincent Hanquez Maintainer: vincent@snarc.org Category: Data Stability: experimental Build-Type: Simple Homepage: http://github.com/vincenthz/hs-byteable Cabal-Version: >=1.8 data-files: README.md Library Exposed-modules: Data.Byteable Build-depends: base >= 4 && < 5 , bytestring ghc-options: -Wall -fwarn-tabs source-repository head type: git location: git://github.com/vincenthz/hs-byteable byteable-0.1.1/README.md0000644000000000000000000000014412163225073012745 0ustar0000000000000000byteable ======= Documentation: [byteable on hackage](http://hackage.haskell.org/package/byteable) byteable-0.1.1/Setup.hs0000644000000000000000000000005612163225073013124 0ustar0000000000000000import Distribution.Simple main = defaultMain byteable-0.1.1/Data/0000755000000000000000000000000012163225073012340 5ustar0000000000000000byteable-0.1.1/Data/Byteable.hs0000644000000000000000000000375412163225073014434 0ustar0000000000000000-- | -- Module : Data.Byteable -- License : BSD-style -- Maintainer : Vincent Hanquez -- Stability : experimental -- Portability : good -- module Data.Byteable ( Byteable(..) , constEqBytes ) where import Foreign.Ptr (Ptr, plusPtr) import Foreign.ForeignPtr (withForeignPtr) import Data.ByteString (ByteString) import Data.List (foldl') import Data.Word (Word8) import qualified Data.ByteString as B (length, zipWith) import qualified Data.ByteString.Internal as B (toForeignPtr) -- | Class of things that can generate sequence of bytes class Byteable a where -- | Convert a byteable type to a bytestring toBytes :: a -> ByteString -- | Return the size of the byteable . byteableLength :: a -> Int byteableLength = B.length . toBytes -- | Provide a way to look at the data of a byteable type with a ptr. withBytePtr :: a -> (Ptr Word8 -> IO b) -> IO b withBytePtr a f = withForeignPtr fptr $ \ptr -> f (ptr `plusPtr` off) where (fptr, off, _) = B.toForeignPtr $ toBytes a instance Byteable ByteString where toBytes bs = bs -- | A constant time equality test for 2 byteable objects. -- -- If objects are of 2 different sizes, the function will abort early -- without comparing any bytes. -- -- compared to == , this function will go over all the bytes -- present before yielding a result even when knowing the -- overall result early in the processing. constEqBytes :: Byteable a => a -> a -> Bool constEqBytes a b = constEqByteString (toBytes a) (toBytes b) {-# RULES "constEqBytes/ByteString" constEqBytes = constEqByteString #-} {-# INLINE constEqByteString #-} constEqByteString :: ByteString -> ByteString -> Bool constEqByteString a b | len /= B.length b = False | otherwise = foldl' (&&!) True $ B.zipWith (==) a b where len = B.length a (&&!) :: Bool -> Bool -> Bool True &&! True = True True &&! False = False False &&! True = False False &&! False = False