Skip to content

Adds a function to decode arraybuffers as their string representation. #15

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 8 commits into from
Jun 30, 2018
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
33 changes: 21 additions & 12 deletions src/Data/ArrayBuffer/ArrayBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,40 @@ exports.create = function(s) {
return function () {
return new ArrayBuffer(s);
};
}
};

exports.byteLength = function(a) {
return a.byteLength;
}
};

exports.sliceImpl = function(s, e, a) {
return function () {
return a.slice(s, e);
};
}
};

exports.fromArray = function(s) {
return (new Uint8Array(s)).buffer;
}
};

exports.fromIntArray = function(s) {
return (new Uint8Array(s)).buffer;
}
};

exports.fromString = function(s) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there any issue in the previous implementation besides the missing semicolon?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I was just playing cleanup while I was in there.

var l = s.length;
var ab = new ArrayBuffer(l * 2);
var a = new Uint16Array(ab);
for (var i = 0; i < l; i++)
a[i] = s.charCodeAt(i);
return a.buffer;
}
var buf = new ArrayBuffer(s.length*2);
var bufView = new Uint16Array(buf);
for (var i=0, strLen=s.length; i<strLen; i++) {
bufView[i] = s.charCodeAt(i);
}
return buf;
};

exports.decodeToStringImpl = function(just, nothing, buffer) {
try {
return just(String.fromCharCode.apply(null, new Uint16Array(buffer)));
}
catch (e) {
return nothing;
}
};
10 changes: 10 additions & 0 deletions src/Data/ArrayBuffer/ArrayBuffer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ module Data.ArrayBuffer.ArrayBuffer ( create
, fromArray
, fromIntArray
, fromString
, decodeToString
) where

import Effect (Effect)
import Data.Maybe (Maybe(..))
import Data.Function.Uncurried (Fn3, runFn3)
import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength)

Expand All @@ -30,3 +32,11 @@ foreign import fromIntArray :: Array Int -> ArrayBuffer

-- | Convert a string into an `ArrayBuffer` representation.
foreign import fromString :: String -> ArrayBuffer

foreign import decodeToStringImpl :: Fn3 (String -> Maybe String) (Maybe String) ArrayBuffer (Maybe String)

-- | Convert an ArrayBuffer into a string. Uses fromCharCode and thus does not support full utf-16
-- | Is currently only defined for ArrayBuffers with even numbers of bytes, as it assumes the ArrayBuffer encodes string data.
-- | For more general string-encoding forms of data, use a base64 or other encoding scheme.
decodeToString :: ArrayBuffer -> Maybe String
decodeToString = runFn3 decodeToStringImpl Just Nothing
11 changes: 10 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Data.ArrayBuffer.DataView as DV
import Data.ArrayBuffer.Typed as TA
import Data.Maybe (Maybe(..), isNothing)
import Data.UInt (fromInt, pow)
import Test.QuickCheck (quickCheck', (<?>))
import Test.QuickCheck (quickCheck', (<?>), quickCheck)

assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit
assertEffEquals expectedValue computation = do
Expand Down Expand Up @@ -36,6 +36,8 @@ main = do
assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0]
assertEquals 4 $ AB.byteLength $ AB.fromIntArray [1, 2, 3, 4]
assertEquals 8 $ AB.byteLength $ AB.fromString "hola"
assertEquals 8 $ AB.byteLength $ AB.fromString "hóla"
assertEquals 10 $ AB.byteLength $ AB.fromString "hóla¡"
assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8
assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8

Expand All @@ -47,6 +49,12 @@ main = do
assertEffEquals Nothing $ TA.at fourElementInt8Array 4
assertEffEquals Nothing $ TA.at fourElementInt8Array (-1)

quickCheck
\(s) ->
Just s == (AB.decodeToString $ AB.fromString s)
<?> "Isormorphic arraybuffer conversion with string failed for input "
<> s

assertEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0]

twoElementDataView <- do
Expand All @@ -66,3 +74,4 @@ main = do
DV.setUint8 dv t 2
DV.setUint8 dv t 3
DV.getUint32be dv 0