1# purescript-parsing-dataview
3[](https://github.com/rowtype-yoga/purescript-parsing-dataview/actions)
4[](http://pursuit.purescript.org/packages/purescript-parsing-dataview/)
5[](https://github.com/jamesdbrock)
8`DataView`s on JavaScript `ArrayBuffer`s with the package
9[__parsing__](https://pursuit.purescript.org/packages/purescript-parsing/).
11With this package, the input stream support of __parsing__
12is similar to the built-in stream support of [__Megaparsec__](https://hackage.haskell.org/package/megaparsec):
14| Stream type | parsing | Megaparsec |
16| UTF-16 strings | String | Text < v2.0|
17| UTF-8 strings | __DataView__ | Text ≥ v2.0 |
18| Listy strings | Token | String |
19| Binary blobs | __DataView__ | ByteString |
23Parse values out of a `dataview :: Data.ArrayBuffer.Types.DataView`. All
24`DataView` parsing must be done in an `Effect` context. The `result` will be
25`Either` a parse error or the parsed value.
29Parse two big-endian IEEE 754 double-precision `Number`s.
32import Parsing (runParserT)
33import Parsing.DataView (anyFloat64be)
36 result <- runParserT dataview do
37 float1 <- anyFloat64be
38 float2 <- anyFloat64be
39 pure $ Tuple float1 float2
44Parse an array of `n` 32-bit big-endian signed `Int`s.
47import Parsing (runParserT)
48import Parsing.DataView (anyUint32be)
49import Data.Unfoldable (replicateA)
52 result <- runParserT dataview $ replicateA n anyInt32be
57Parse a UTF-8 `String` with a length prefix.
59We give this as an example, rather than supporting it in the library, because
61[__web-encoding__](https://pursuit.purescript.org/packages/purescript-web-encoding) for UTF-8.
64import Control.Monad.Except (ExceptT)
65import Data.ArrayBuffer.Cast (toUint8Array)
66import Effect.Exception (catchException, message)
67import Parsing (runParserT, liftExceptT)
68import Parsing.DataView (anyInt32be, takeN)
69import Web.Encoding.TextDecoder as TextDecoder
70import Web.Encoding.UtfLabel as UtfLabel
73 textDecoder <- TextDecoder.new UtfLabel.utf8
75 result <- runParserT dataview do
76 -- First parse a 32-bit big-endian length prefix for the length
77 -- of the UTF-8 string in bytes.
79 stringview <- takeN length
80 stringarray <- lift $ liftEffect $ toUint8Array stringview
81 liftExceptT $ ExceptT $ catchException (pure <<< Left <<< message) do
82 Right <$> TextDecoder.decode stringarray textDecoder
87This package is for reading (`DataView`s on) `ArrayBuffer`s, not writing
89[__arraybuffer-builder__](https://pursuit.purescript.org/packages/purescript-arraybuffer-builder/)
91serialize and build `ArrayBuffer`s.
96* [MDN `ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
97* [MDN `DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView)
101Run the tests with the development `spago` file:
104spago -x spago-dev.dhall test
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list