Skip to content

Use Number for reading and writing, fixes #25 #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2019
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
4 changes: 2 additions & 2 deletions src/Node/Buffer/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Monad m <= MutableBuffer buf m | buf -> m where
toArrayBuffer :: buf -> m ArrayBuffer

-- | Reads a numeric value from a buffer at the specified offset.
read :: BufferValueType -> Offset -> buf -> m Int
read :: BufferValueType -> Offset -> buf -> m Number

-- | Reads a section of a buffer as a string with the specified encoding.
readString :: Encoding -> Offset -> Offset -> buf -> m String
Expand All @@ -77,7 +77,7 @@ class Monad m <= MutableBuffer buf m | buf -> m where
toString :: Encoding -> buf -> m String

-- | Writes a numeric value to a buffer at the specified offset.
write :: BufferValueType -> Int -> Offset -> buf -> m Unit
write :: BufferValueType -> Number -> Offset -> buf -> m Unit

-- | Writes octets from a string to a buffer at the specified offset. Multi-byte
-- | characters will not be written to the buffer if there is not enough capacity
Expand Down
4 changes: 2 additions & 2 deletions src/Node/Buffer/Immutable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ fromString str = fromStringImpl str <<< encodingToNode
foreign import fromStringImpl :: String -> String -> ImmutableBuffer

-- | Reads a numeric value from a buffer at the specified offset.
read :: BufferValueType -> Offset -> ImmutableBuffer -> Int
read :: BufferValueType -> Offset -> ImmutableBuffer -> Number
read = readImpl <<< show

foreign import readImpl :: String -> Offset -> ImmutableBuffer -> Int
foreign import readImpl :: String -> Offset -> ImmutableBuffer -> Number

-- | Reads a section of a buffer as a string with the specified encoding.
readString :: Encoding -> Offset -> Offset -> ImmutableBuffer -> String
Expand Down
6 changes: 3 additions & 3 deletions src/Node/Buffer/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fromArrayBuffer = usingToImmutable Immutable.fromArrayBuffer
toArrayBuffer :: forall buf m. Monad m => buf -> m ArrayBuffer
toArrayBuffer = usingFromImmutable Immutable.toArrayBuffer

read :: forall buf m. Monad m => BufferValueType -> Offset -> buf -> m Int
read :: forall buf m. Monad m => BufferValueType -> Offset -> buf -> m Number
read t o = usingFromImmutable $ Immutable.read t o

readString :: forall buf m. Monad m => Encoding -> Offset -> Offset -> buf -> m String
Expand All @@ -73,10 +73,10 @@ readString m o o' = usingFromImmutable $ Immutable.readString m o o'
toString :: forall buf m. Monad m => Encoding -> buf -> m String
toString m = usingFromImmutable $ Immutable.toString m

write :: forall buf m. Monad m => BufferValueType -> Int -> Offset -> buf -> m Unit
write :: forall buf m. Monad m => BufferValueType -> Number -> Offset -> buf -> m Unit
write = writeInternal <<< show

foreign import writeInternal :: forall buf m. String -> Int -> Offset -> buf -> m Unit
foreign import writeInternal :: forall buf m. String -> Number -> Offset -> buf -> m Unit

writeString :: forall buf m. Monad m => Encoding -> Offset -> Int -> String -> buf -> m Int
writeString = writeStringInternal <<< encodingToNode
Expand Down
6 changes: 3 additions & 3 deletions test/Test/Node/Buffer/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ testMutableBuffer _ run = do

testReadWrite :: Effect Unit
testReadWrite = do
let val = 42
let val = 42.0
readVal <- run do
buf <- create 1 :: m buf
write UInt8 val 0 buf
Expand All @@ -94,7 +94,7 @@ testMutableBuffer _ run = do
buf <- fromArray [1,2,3,4,5] :: m buf
read UInt8 2 buf

assertEqual {expected: 3, actual: readVal}
assertEqual {expected: 3.0, actual: readVal}

testToArray :: Effect Unit
testToArray = do
Expand All @@ -112,7 +112,7 @@ testMutableBuffer _ run = do
buf <- fromString str ASCII :: m buf
read UInt8 6 buf

assertEqual {expected: 32, actual: val} -- ASCII space
assertEqual {expected: 32.0, actual: val} -- ASCII space

testToFromArrayBuffer :: Effect Unit
testToFromArrayBuffer = do
Expand Down
2 changes: 1 addition & 1 deletion test/Test/Node/Buffer/Immutable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ testCreate = do
testFromString :: Effect Unit
testFromString = do
let buf = Immutable.fromString "hello, world" ASCII
assertEqual {expected: 32, actual: Immutable.read UInt8 6 buf}
assertEqual {expected: 32.0, actual: Immutable.read UInt8 6 buf}

testToString :: Effect Unit
testToString = do
Expand Down