From 7d7129222a3f15786ecebeb557eb4874304a6c7d Mon Sep 17 00:00:00 2001 From: Gavin Panella Date: Sat, 3 Nov 2018 16:00:38 +0100 Subject: [PATCH] Fix unicode escape decode bug in `DoubleQuoteString` example. Previously \u Unicode escape codes of between 4 and 6 characters were being rejected, whereas the error message indicates that the inverse should be the behavior. The example has also been extended to use all recognized escape sequences (\n, \r, \t, \u) and give more information about what's happening. --- examples/DoubleQuoteString.elm | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/examples/DoubleQuoteString.elm b/examples/DoubleQuoteString.elm index 31ac621..0c26a59 100644 --- a/examples/DoubleQuoteString.elm +++ b/examples/DoubleQuoteString.elm @@ -9,9 +9,25 @@ import Parser exposing (..) main = - Html.text <| Debug.toString <| - run string "\"hello\"" + let + input = + "\"Hell\\u{006f}! What's up?\\r\\nThis >\\t< is a tab, that's what.\\r\\n\\u{1f600}\"" + + result = + run string input + display text = + [ Html.dt [] [ Html.text "Rendered:" ] + , Html.dd [] [ Html.pre [] [ Html.text text ] ] + ] + in + Html.dl [] <| + [ Html.dt [] [ Html.text "Input:" ] + , Html.dd [] [ Html.pre [] [ Html.text input ] ] + , Html.dt [] [ Html.text "Parse result:" ] + , Html.dd [] [ Html.pre [] [ Html.text <| Debug.toString result ] ] + ] + ++ (Result.map display result |> Result.withDefault []) -- STRINGS @@ -67,7 +83,7 @@ codeToChar str = length = String.length str code = String.foldl addHex 0 str in - if 4 <= length && length <= 6 then + if length < 4 || length > 6 then problem "code point must have between 4 and 6 digits" else if 0 <= code && code <= 0x10FFFF then succeed (Char.fromCode code)