mirror of
https://github.com/rowtype-yoga/purescript-parsing-dataview.git
synced 2024-11-22 04:05:39 +03:00
PureScript 15.4
And README, docs
This commit is contained in:
parent
37603e5845
commit
0c2843f776
3
.github/workflows/push-build.yml
vendored
3
.github/workflows/push-build.yml
vendored
@ -2,8 +2,9 @@ name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
branches: [master]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
36
README.md
36
README.md
@ -57,39 +57,31 @@ Parse a UTF-8 `String` with a length prefix.
|
||||
|
||||
We give this as an example, rather than supporting it in the library, because
|
||||
it depends on
|
||||
[`Data.TextDecoding.decodeUtf8`](https://pursuit.purescript.org/packages/purescript-text-encoding/docs/Data.TextDecoding#v:decodeUtf8).
|
||||
[__web-encoding__](https://pursuit.purescript.org/packages/purescript-web-encoding) for UTF-8.
|
||||
|
||||
```purescript
|
||||
import Control.Monad.Trans.Class (lift)
|
||||
import Data.ArrayBuffer.Types (DataView, Uint8Array)
|
||||
import Data.ArrayBuffer.DataView (buffer, byteOffset, byteLength)
|
||||
import Data.ArrayBuffer.Typed (part)
|
||||
import Effect (Effect, liftEffect)
|
||||
import Text.Parsing.Parser (runParserT, fail)
|
||||
import Text.Parsing.Parser.DataView (anyUint32be, takeN)
|
||||
import Control.Monad.Except (ExceptT)
|
||||
import Data.ArrayBuffer.Cast (toUint8Array)
|
||||
import Effect.Exception (catchException, message)
|
||||
import Parsing (runParserT, liftExceptT)
|
||||
import Parsing.DataView (anyUint32be, takeN)
|
||||
import Data.UInt (toInt)
|
||||
import Data.Text.Decoding (decodeUtf8)
|
||||
|
||||
|
||||
-- Make a `Uint8Array` Typed Array from a `DataView`. We can do this
|
||||
-- for the case of `Uint8` because byte arrays are always aligned.
|
||||
mkUint8Array :: DataView -> Effect Uint8Array
|
||||
mkUint8Array dv = part (buffer dv) (byteOffset dv) (byteLength dv)
|
||||
import Web.Encoding.TextDecoder as TextDecoder
|
||||
import Web.Encoding.UtfLabel as UtfLabel
|
||||
|
||||
do
|
||||
result <- runParserT dataview do
|
||||
textDecoder <- TextDecoder.new UtfLabel.utf8
|
||||
|
||||
result :: <- runParserT dataview do
|
||||
-- Parse a 32-bit big-endian length prefix for the length
|
||||
-- of the UTF-8 string in bytes.
|
||||
length <- anyUint32be
|
||||
stringview <- takeN $ toInt length
|
||||
stringarray <- lift $ liftEffect $ mkUint8Array stringview
|
||||
case decodeUtf8 stringarray of
|
||||
Left err -> fail $ show err
|
||||
Right s -> pure s
|
||||
stringarray <- lift $ liftEffect $ toUint8Array stringview
|
||||
liftExceptT $ ExceptT $ catchException (Left <<< message) do
|
||||
Right <$> TextDecoder.decode stringarray
|
||||
```
|
||||
|
||||
You might also consider using [__web-encoding__](https://pursuit.purescript.org/packages/purescript-web-encoding) for UTF-8. It will work in *Node.js ≥ v11*.
|
||||
|
||||
## Serialization
|
||||
|
||||
This package is for reading (`DataView`s on) `ArrayBuffer`s, not writing
|
||||
|
@ -19,8 +19,8 @@
|
||||
"purescript-effect": "^v4.0.0",
|
||||
"purescript-float32": "^v2.0.0",
|
||||
"purescript-maybe": "^v6.0.0",
|
||||
"purescript-parsing": "^v9.0.0",
|
||||
"purescript-prelude": "^v6.0.0",
|
||||
"purescript-parsing": "^v10.0.0",
|
||||
"purescript-prelude": "^v6.0.1",
|
||||
"purescript-transformers": "^v6.0.0",
|
||||
"purescript-tuples": "^v7.0.0",
|
||||
"purescript-uint": "^v7.0.0"
|
||||
|
@ -117,8 +117,8 @@ let additions =
|
||||
-------------------------------
|
||||
-}
|
||||
let upstream =
|
||||
https://github.com/purescript/package-sets/releases/download/psc-0.15.0-20220505/packages.dhall
|
||||
sha256:ba57c25c86fd54c2b672cda3a6836bbbdff4b1bbf946bceaabb64e5a10285638
|
||||
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221110/packages.dhall
|
||||
sha256:55be93ee309eeb1b3a1d30c7b9fa5d18ffefa67f5fbeec1566b7b6a70b0ac218
|
||||
|
||||
let overrides = {=}
|
||||
|
||||
|
@ -1,5 +1,10 @@
|
||||
-- | Primitive parsers for input `DataView`s with the
|
||||
-- | `ParserT` type and combinators in package __parsing__.
|
||||
-- | Primitive parsers for input type `DataView`.
|
||||
-- |
|
||||
-- | All of these primitive parsers will consume when they succeed.
|
||||
-- |
|
||||
-- | All of these primitive parsers will not consume and will automatically
|
||||
-- | backtrack when they fail.
|
||||
-- |
|
||||
-- | See the package README for usage examples.
|
||||
-- |
|
||||
-- | ### Mutable ArrayBuffer
|
||||
|
Loading…
Reference in New Issue
Block a user