PureScript 15.4

And README, docs
This commit is contained in:
James Brock 2022-12-01 10:40:14 +09:00
parent 37603e5845
commit 0c2843f776
5 changed files with 27 additions and 29 deletions

View File

@ -2,8 +2,9 @@ name: CI
on:
push:
branches: [master]
pull_request:
branches: [main]
branches: [master]
jobs:
build:

View File

@ -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

View File

@ -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"

View File

@ -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 = {=}

View File

@ -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