Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.22.0
+ Added `map :: (ByteArrayAccess ba, ByteArray ba) => (Word8 -> Word8) -> ba -> ba`
to `Data.ByteArray.Methods` (re-exported via `Data.ByteArray`).
Applies a function to each byte of a byte array. (closes #5)

## 0.21.1
+ Reverted 0.21.0 changes: restored custom Base16/Base32/Base64 encode/decode,
the GHC.Prim-based `Bytes` implementation, and `readWord8OffAddr#`-based FNV
Expand Down
18 changes: 16 additions & 2 deletions Data/ByteArray/Methods.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Data.ByteArray.Methods
, all
, append
, concat
, map
) where

import Data.ByteArray.Types
Expand All @@ -49,7 +50,7 @@ import Data.Monoid
import Foreign.Storable
import Foreign.Ptr

import Prelude hiding (length, take, drop, span, reverse, concat, replicate, splitAt, null, pred, last, any, all)
import Prelude hiding (length, take, drop, span, reverse, concat, replicate, splitAt, null, pred, last, any, all, map)
import qualified Prelude


Expand Down Expand Up @@ -200,7 +201,7 @@ reverse bs = unsafeCreate n $ \d -> withByteArray bs $ \s -> memReverse d s n
concat :: (ByteArrayAccess bin, ByteArray bout) => [bin] -> bout
concat l = unsafeCreate retLen (loopCopy l)
where
retLen = sum $ map length l
retLen = sum $ Prelude.map length l

loopCopy [] _ = return ()
loopCopy (x:xs) dst = do
Expand Down Expand Up @@ -296,6 +297,19 @@ any f b
all :: (ByteArrayAccess ba) => (Word8 -> Bool) -> ba -> Bool
all f b = not (any (not . f) b)

-- | Map a function over each byte of a bytearray
map :: (ByteArrayAccess ba, ByteArray ba) => (Word8 -> Word8) -> ba -> ba
map f ba = copyAndFreeze ba $ loop 0
where
len = length ba
loop i ptr
| i == len = return ()
| otherwise = do
let ptr' = ptr `plusPtr` i
x <- peek ptr'
poke ptr' $ f x
loop (i + 1) ptr

-- | Convert a bytearray to another type of bytearray
convert :: (ByteArrayAccess bin, ByteArray bout) => bin -> bout
convert bs = inlineUnsafeCreate (length bs) (copyByteArrayToPtr bs)
2 changes: 1 addition & 1 deletion ram.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: ram
version: 0.21.1
version: 0.22.0
synopsis: memory and related abstraction stuff
description:
This is a fork of memory. It's open to accept changes from anyone,
Expand Down
4 changes: 4 additions & 0 deletions tests/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,8 @@ main = defaultMain $ testGroup "memory"
, testProperty "span (const False)" $ \(Words8 l) ->
let b = witnessID (B.pack l)
in B.span (const False) b == (B.empty, b)
, testProperty "map f == pack . Prelude.map f . unpack" $ \(Words8 l) (Positive w) ->
let b = witnessID (B.pack l)
f x = x + fromIntegral w :: Word8
in B.map f b == (witnessID . B.pack . Prelude.map f $ l)
]
Loading