binary-varint (empty) → 0.1.0.0
raw patch · 5 files changed
+111/−0 lines, 5 filesdep +basedep +binary
Dependencies added: base, binary
Files
- CHANGELOG.md +0/−0
- LICENSE +30/−0
- README.md +1/−0
- binary-varint.cabal +44/−0
- src/Data/Binary/VarInt.hs +36/−0
+ CHANGELOG.md view
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, Monadic GmbH++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 Monadic GmbH 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.
+ README.md view
@@ -0,0 +1,1 @@+Haskell base-128 variable-width integers ([VarInts](https://developers.google.com/protocol-buffers/docs/encoding#varints)) via [Data.Binary](https://hackage.haskell.org/package/binary).
+ binary-varint.cabal view
@@ -0,0 +1,44 @@+cabal-version: 2.2+build-type: Simple++name: binary-varint+version: 0.1.0.0+synopsis: VarInt encoding/decoding via Data.Binary+homepage: https://github.com/oscoin/ipfs+bug-reports: https://github.com/oscoin/ipfs/issues+license: BSD-3-Clause+license-file: LICENSE+author: Kim Altintop+maintainer: Kim Altintop <kim@monadic.xyz>, Monadic Team <team@monadic.xyz>+copyright: 2018 Monadic GmbH++category: Data++extra-source-files:+ CHANGELOG.md+ , README.md++source-repository head+ type: git+ location: https://github.com/oscoin/ipfs++common common+ default-language: Haskell2010+ ghc-options:+ -Wall+ -Wcompat+ -Wincomplete-uni-patterns+ -Wincomplete-record-updates+ -Wredundant-constraints+ -fprint-expanded-synonyms+ -funbox-small-strict-fields++library+ import: common+ hs-source-dirs: src+ exposed-modules:+ Data.Binary.VarInt++ build-depends:+ base >= 4.9 && < 5+ , binary >= 0.8.3
+ src/Data/Binary/VarInt.hs view
@@ -0,0 +1,36 @@+-- |+-- Copyright : 2018 Monadic GmbH+-- License : BSD3+-- Maintainer : kim@monadic.xyz, team@monadic.xyz+-- Stability : stable+-- Portability : portable+--+module Data.Binary.VarInt+ ( getVarInt+ , putVarInt+ , buildVarInt+ )+where++import qualified Data.Binary as Binary+import Data.Binary.Builder (Builder)+import qualified Data.Binary.Builder as Builder+import qualified Data.Binary.Put as Binary (putLazyByteString)+import Data.Bits+import Data.Word (Word8)++getVarInt :: (Num a, Bits a) => Word8 -> Binary.Get a+getVarInt n+ | testBit n 7 = do+ v <- Binary.getWord8 >>= getVarInt+ pure $ shiftL v 7 .|. clearBit (fromIntegral n) 7+ | otherwise = pure $ fromIntegral n++putVarInt :: (Integral a, Bits a) => a -> Binary.Put+putVarInt = Binary.putLazyByteString . Builder.toLazyByteString . buildVarInt++buildVarInt :: (Integral a, Bits a) => a -> Builder+buildVarInt n+ | n < 0x80 = Builder.singleton $ fromIntegral n+ | otherwise = Builder.singleton (setBit (fromIntegral n) 7)+ <> buildVarInt (shiftR n 7)