byte-count-reader 0.10.0.1 → 0.10.1.1
raw patch · 5 files changed
+39/−10 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.ByteCountReader: sizeInBytesAssumingBase2 :: Text -> Maybe Integer
Files
- ChangeLog.md +3/−0
- README.md +0/−3
- byte-count-reader.cabal +1/−1
- src/Data/ByteCountReader.hs +22/−2
- test/Spec.hs +13/−4
ChangeLog.md view
@@ -2,6 +2,9 @@ ## Unreleased changes +## 0.10.1+- Added a function that always assumes base 2 where ambiguities occur+ ## 0.10.0.1 - Relaxed upper bound on extra <1.7 -> <1.8 - Fixed incorrect link in README
README.md view
@@ -6,9 +6,6 @@ This library is for reading strings describing a number of bytes like 2Kb and 0.5 MiB. -The units KB, MB, GB and TB imply base 10 (e.g. 2KB = 2 x 1000).-The units KiB, MiB, GiB and TiB imply base 2 (e.g. 2KiB = 2 * 1024).- The following are examples of strings that are accepted: - 1b - 2 KiB
byte-count-reader.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: byte-count-reader-version: 0.10.0.1+version: 0.10.1.1 license: GPL-3.0-only license-file: LICENSE copyright: 2020 Daniel Rolls
src/Data/ByteCountReader.hs view
@@ -6,9 +6,8 @@ Maintainer : daniel.rolls.27@googlemail.com This library is for reading strings describing a number of bytes like 2Kb and 0.5 MiB.-The units KB, MB, GB and TB imply base 10 (e.g. 2KB = 2 x 1000). The units KiB, MiB, GiB and TiB imply base 2 (e.g. 2KiB = 2 * 1024). -}-module Data.ByteCountReader (sizeInBytes) where+module Data.ByteCountReader (sizeInBytes, sizeInBytesAssumingBase2) where import Data.Char (toLower) import Data.Either.Extra (eitherToMaybe)@@ -24,6 +23,13 @@ sizeInBytes inStr = do (number, units) <- eitherToMaybe $ parse bytesParser "<>" (unpack inStr) roundDoubleInteger . (number *) . fromInteger <$> toMultiplier units +-- |Read strings describing a number of bytes like 2KB and 0.5 MiB assuming 1Kb is 1024 bytes, not 1000.+-- The units KB, MB, GB and TB are assumed to be base 2 (e.g. 2KB = 2 x 1024).+-- The units KiB, MiB, GiB and TiB are assumed to be base 2 (e.g. 2KiB = 2 * 1024).+sizeInBytesAssumingBase2 :: Text -> Maybe Integer+sizeInBytesAssumingBase2 inStr = do (number, units) <- eitherToMaybe $ parse bytesParser "<>" (unpack inStr)+ roundDoubleInteger . (number *) . fromInteger <$> toBase2Multiplier units+ bytesParser :: GenParser Char st (Double, String) bytesParser = do num <- parseNumber parseSpaces@@ -47,3 +53,17 @@ mapUnits _ = Nothing _1024RaisedTo = Just . (1024 ^) _1000RaisedTo = Just . (1000 ^)++toBase2Multiplier :: String -> Maybe Integer+toBase2Multiplier = mapUnits . map toLower+ where mapUnits "b" = _1024RaisedTo 0+ mapUnits "kb" = _1024RaisedTo 1+ mapUnits "kib" = _1024RaisedTo 1+ mapUnits "mb" = _1024RaisedTo 2+ mapUnits "mib" = _1024RaisedTo 2+ mapUnits "gb" = _1024RaisedTo 3+ mapUnits "gib" = _1024RaisedTo 3+ mapUnits "tb" = _1024RaisedTo 4+ mapUnits "tib" = _1024RaisedTo 4+ mapUnits _ = Nothing+ _1024RaisedTo = Just . (1024 ^)
test/Spec.hs view
@@ -7,7 +7,7 @@ main = hspec $ do it "Unparsable strings should return Nothing" $ sizeInBytes "unparseable" `shouldBe` Nothing- testHappyPathScenarios+ testHappyPathScenarios sizeInBytes [ ("1 kb", 1000) , ("0.5 kib", 512) , ("1 kib", 1024)@@ -17,9 +17,18 @@ , ("4 GiB", 4294967296) , ("1 tIb", 1099511627776) ]- where testHappyPathScenarios = foldl (>>) (return ()) . map parseTest- parseTest (string, expectedValue) = it ("Parse " <> unpack string) $- sizeInBytes string `shouldBe` Just expectedValue+ testHappyPathScenarios sizeInBytesAssumingBase2+ [ ("1 kb", 1024)+ , ("0.5 kib", 512)+ , ("1 kib", 1024)+ , ("2 kb", 2048)+ , ("3 kb", 3072)+ , (".5 MiB", 524288)+ , ("4 GiB", 4294967296)+ ]+ where testHappyPathScenarios functionUnderTest = foldl (>>) (return ()) . map (parseTest functionUnderTest)+ parseTest functionUnderTest (string, expectedValue) = it ("Parse " <> unpack string) $+ functionUnderTest string `shouldBe` Just expectedValue