Skip to content
This repository was archived by the owner on Oct 5, 2024. It is now read-only.

Commit 09fa46c

Browse files
committed
use Hex.fromString
1 parent 3163b05 commit 09fa46c

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

src/compiler/Stage/Parse/Parser.elm

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -468,34 +468,20 @@ literalChar =
468468
unicode : Parser_ Char
469469
unicode =
470470
P.getChompedString (P.chompWhile Char.isHexDigit)
471-
|> P.andThen codeToChar
472-
473-
474-
codeToChar : String -> Parser_ Char
475-
codeToChar str =
476-
let
477-
length = String.length str
478-
code = String.foldl addHex 0 str
479-
in
480-
if length < 4 || length > 6 then
481-
P.problem InvalidUnicodeCodePoint
482-
else if 0 <= code && code <= 0x10FFFF then
483-
P.succeed (Char.fromCode code)
484-
else
485-
P.problem InvalidUnicodeCodePoint
486-
487-
488-
addHex : Char -> Int -> Int
489-
addHex char total =
490-
let
491-
code = Char.toCode char
492-
in
493-
if 0x30 <= code && code <= 0x39 then
494-
16 * total + (code - 0x30)
495-
else if 0x41 <= code && code <= 0x46 then
496-
16 * total + (10 + code - 0x41)
497-
else
498-
16 * total + (10 + code - 0x61)
471+
|> P.andThen (\str ->
472+
let
473+
len = String.length str
474+
in
475+
if len < 4 || len > 6 then
476+
P.problem InvalidUnicodeCodePoint
477+
else
478+
str
479+
|> String.toLower
480+
|> Hex.fromString
481+
|> Result.map Char.fromCode
482+
|> Result.map P.succeed
483+
|> Result.withDefault (P.problem InvalidUnicodeCodePoint)
484+
)
499485

500486

501487
{-| TODO escapes

0 commit comments

Comments
 (0)