bits-show (empty) → 0.0.0.0
raw patch · 6 files changed
+111/−0 lines, 6 filesdep +basedep +bits-showdep +hspec
Dependencies added: base, bits-show, hspec
Files
- bits-show.cabal +39/−0
- changelog.md +3/−0
- library/Bits/Show.hs +33/−0
- license.txt +13/−0
- readme.md +3/−0
- test/Main.hs +20/−0
+ bits-show.cabal view
@@ -0,0 +1,39 @@+cabal-version: 3.0++name: bits-show+version: 0.0.0.0+synopsis: Showing data as strings of 0 and 1+category: Bit, Text++description:+ The @showFiniteBits@ function, for a type belonging to+ the @FiniteBits@ class, displays the bits as a string.++license: Apache-2.0+license-file: license.txt++author: Chris Martin+maintainer: Chris Martin & Julie Moronuki <hello@typeclasses.com>++homepage: https://github.com/typeclasses/bits-show+bug-reports: https://github.com/typeclasses/bits-show/issues++extra-source-files: *.md++common base+ default-language: GHC2021+ build-depends: base ^>= 4.16 || ^>= 4.17 || ^>= 4.18++library+ import: base+ exposed-modules: Bits.Show+ hs-source-dirs: library++test-suite test+ import: base+ type: exitcode-stdio-1.0+ main-is: Main.hs+ hs-source-dirs: test+ build-depends:+ , bits-show+ , hspec ^>= 2.8.5 || ^>= 2.9 || ^>= 2.10
+ changelog.md view
@@ -0,0 +1,3 @@+## 0.0.0.0 -- 2023-03-29++Initial version
+ library/Bits/Show.hs view
@@ -0,0 +1,33 @@+module Bits.Show where++import Data.Bits+import GHC.Exts++class FixedWidthIntegral++{- |+Examples:++@showFiniteBits (5 :: Int8) = "00000101"@++@showFiniteBits ((-5) :: Int8) = "11111011"@+-}+showFiniteBits :: (FiniteBits bits, IsString string) => bits -> string+showFiniteBits x =+ fromString+ (+ fmap (showBit . testBit x)+ (takeWhile (>= 0)+ (+ iterate+ (\i -> i - 1)+ (finiteBitSize x - 1)+ )+ )+ )++{- | @{ True -> '1'; False -> '0' }@ -}+showBit :: Bool -> Char+showBit x = case x of+ True -> '1'+ False -> '0'
+ license.txt view
@@ -0,0 +1,13 @@+Copyright 2023 Mission Valley Software LLC++Licensed under the Apache License, Version 2.0 (the "License");+you may not use this file except in compliance with the License.+You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++Unless required by applicable law or agreed to in writing, software+distributed under the License is distributed on an "AS IS" BASIS,+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+See the License for the specific language governing permissions and+limitations under the License.
+ readme.md view
@@ -0,0 +1,3 @@+The `showFiniteBits` function, for a type belonging to the `FiniteBits` class,+displays the bits as a string. All the bits are shown; e.g. for an `Int32` you+will always get a string of length 32.
+ test/Main.hs view
@@ -0,0 +1,20 @@+import Test.Hspec++import Bits.Show+import Data.Int+import Data.Word++main = hspec $ do+ describe "showFiniteBits" $ do+ it "Word8" $ showFiniteBits @Word8 2 `shouldBe` "00000010"+ it "Int8+" $ showFiniteBits @Int8 2 `shouldBe` "00000010"+ it "Int8-" $ showFiniteBits @Int8 (-2) `shouldBe` "11111110"+ it "Word16" $ showFiniteBits @Word16 2 `shouldBe` "0000000000000010"+ it "Int16+" $ showFiniteBits @Int16 2 `shouldBe` "0000000000000010"+ it "Int16-" $ showFiniteBits @Int16 (-2) `shouldBe` "1111111111111110"+ it "Word32" $ showFiniteBits @Word32 2 `shouldBe` "00000000000000000000000000000010"+ it "Int32+" $ showFiniteBits @Int32 2 `shouldBe` "00000000000000000000000000000010"+ it "Int32-" $ showFiniteBits @Int32 (-2) `shouldBe` "11111111111111111111111111111110"+ it "Word64" $ showFiniteBits @Word64 2 `shouldBe` "0000000000000000000000000000000000000000000000000000000000000010"+ it "Int64+" $ showFiniteBits @Int64 2 `shouldBe` "0000000000000000000000000000000000000000000000000000000000000010"+ it "Int64-" $ showFiniteBits @Int64 (-2) `shouldBe` "1111111111111111111111111111111111111111111111111111111111111110"