Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SHA: add readDigestBS

A simple function to parse a hex string into a ByteString representing
a SHA digest.
  • Loading branch information...
commit 1d21df4dc243d5097fa9de5dd17a3a623e943777 1 parent 28c20b8
@kfish authored
Showing with 10 additions and 1 deletion.
  1. +10 −1 Git/SHA.hs
View
11 Git/SHA.hs
@@ -1,13 +1,16 @@
{-# OPTIONS -Wall #-}
module Git.SHA (
- showDigestBS
+ showDigestBS,
+ readDigestBS
) where
import Data.Bits
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Char
+import Data.List (unfoldr)
+import Numeric
------------------------------------------------------------
-- From Data.Digest.Pure.SHA
@@ -20,3 +23,9 @@ showDigestBS bs = foldr paddedShowHex [] (BS.unpack bs)
: intToDigit (fromIntegral (x .&. 0xf))
: xs
+
+------------------------------------------------------------
+-- Read a string as a hex bytestring
+
+readDigestBS :: String -> ByteString
+readDigestBS = BS.pack . map (fst . head . readHex) . takeWhile (not . null) . unfoldr (Just . splitAt 2)
Please sign in to comment.
Something went wrong with that request. Please try again.