git.iko.soy/rowtype-yoga/purescript-parsing-dataview
on master help sign in
Test UTF-8
commitb6c6a8feb793e7713b6c203720e93a7746c90bbc authorJames Brock <jamesbrock@gmail.com> dateFri 2 Dec 2022, 19:13 (4 years ago) parent0c2843f
4 files changed+44−13
README.md+7−8 packages.dhall+2−2 spago-dev.dhall+2−1 test/Main.purs+33−2
README.md+7−8
@@ -64,22 +64,21 @@ import Control.Monad.Except (ExceptT)
6464import Data.ArrayBuffer.Cast (toUint8Array)
6565import Effect.Exception (catchException, message)
6666import Parsing (runParserT, liftExceptT)
67−import Parsing.DataView (anyUint32be, takeN)
68−import Data.UInt (toInt)
67+import Parsing.DataView (anyInt32be, takeN)
6968import Web.Encoding.TextDecoder as TextDecoder
7069import Web.Encoding.UtfLabel as UtfLabel
7170
7271do
7372 textDecoder <- TextDecoder.new UtfLabel.utf8
7473
75− result :: <- runParserT dataview do
76− -- Parse a 32-bit big-endian length prefix for the length
74+ result <- runParserT dataview do
75+ -- First parse a 32-bit big-endian length prefix for the length
7776 -- of the UTF-8 string in bytes.
78− length <- anyUint32be
79− stringview <- takeN $ toInt length
77+ length <- anyInt32be
78+ stringview <- takeN length
8079 stringarray <- lift $ liftEffect $ toUint8Array stringview
81− liftExceptT $ ExceptT $ catchException (Left <<< message) do
82− Right <$> TextDecoder.decode stringarray
80+ liftExceptT $ ExceptT $ catchException (pure <<< Left <<< message) do
81+ Right <$> TextDecoder.decode stringarray textDecoder
8382```
8483
8584## Serialization
packages.dhall+2−2
@@ -117,8 +117,8 @@ let additions =
117117-------------------------------
118118-}
119119let upstream =
120− https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221110/packages.dhall
121− sha256:55be93ee309eeb1b3a1d30c7b9fa5d18ffefa67f5fbeec1566b7b6a70b0ac218
120+ https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221202/packages.dhall
121+ sha256:4e2f2e060ab73b119eeb55fe9bc75168b380fc5e0696d2cc2941ac674b32cb3c
122122
123123let overrides = {=}
124124
spago-dev.dhall+2−1
@@ -18,7 +18,8 @@ in conf //
1818 , "console"
1919 , "foldable-traversable"
2020 , "lists"
21− , "psci-support"
2221 , "either"
22+ , "web-encoding"
23+ , "exceptions"
2324 ]
2425 }
test/Main.purs+33−2
@@ -2,8 +2,10 @@ module Test.Main where
22
33import Prelude
44
5+import Control.Monad.Except (ExceptT(..))
56import Control.Monad.Trans.Class (lift)
6−import Data.ArrayBuffer.ArrayBuffer (empty) as AB
7+import Data.ArrayBuffer.ArrayBuffer as AB
8+import Data.ArrayBuffer.Cast (fromUint8Array, toUint8Array)
79import Data.ArrayBuffer.DataView as DataView
810import Data.Either (Either(..))
911import Data.List (fromFoldable)
@@ -13,12 +15,17 @@ import Data.Traversable (for_)
1315import Data.Tuple (Tuple(..))
1416import Data.UInt (fromInt)
1517import Effect (Effect)
18+import Effect.Class (liftEffect)
1619import Effect.Console (logShow)
17−import Parsing (ParserT, Position(..), fail, parseErrorPosition, runParserT)
20+import Effect.Exception (catchException, message)
21+import Parsing (ParserT, Position(..), fail, liftExceptT, parseErrorPosition, runParserT)
1822import Parsing.Combinators (many, manyTill)
1923import Parsing.DataView (anyInt8)
2024import Parsing.DataView as DV
2125import Test.Assert (assert', assertEqual')
26+import Web.Encoding.TextDecoder as TextDecoder
27+import Web.Encoding.TextEncoder as TextEncoder
28+import Web.Encoding.UtfLabel as UtfLabel
2229
2330parseTestT :: forall s a. Show a => Eq a => s -> a -> ParserT s Effect a -> Effect Unit
2431parseTestT input expected p = do
@@ -138,3 +145,27 @@ main = do
138145 { actual: x
139146 , expected: Right 200000
140147 }
148+
149+ do
150+ let
151+ teststring = "test string 👓"
152+
153+ textDecoder <- TextDecoder.new UtfLabel.utf8
154+
155+ textEncoder <- TextEncoder.new
156+
157+ let testarray = TextEncoder.encode teststring textEncoder
158+
159+ testview <- fromUint8Array testarray
160+
161+ result <- runParserT testview do
162+ let length = DataView.byteLength testview
163+ stringview <- DV.takeN length
164+ stringarray <- lift $ liftEffect $ toUint8Array stringview
165+ liftExceptT $ ExceptT $ catchException (pure <<< Left <<< message) do
166+ Right <$> TextDecoder.decode stringarray textDecoder
167+
168+ assertEqual' "UTF-8 decoding example"
169+ { expected: Right teststring
170+ , actual: result
171+ }