From 3cd6d5d4af754e10e67539502e96d33d4c989aef Mon Sep 17 00:00:00 2001 From: Gareth Pendleton Date: Mon, 22 Sep 2025 12:30:09 +0100 Subject: [PATCH] perf: Fix minor perf issue for JS --- src/gluri/internal/parser.gleam | 30 +++++++++++++++++------------- src/gluri/internal/utils.gleam | 9 +++++---- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/gluri/internal/parser.gleam b/src/gluri/internal/parser.gleam index 2fe8559..db655ab 100644 --- a/src/gluri/internal/parser.gleam +++ b/src/gluri/internal/parser.gleam @@ -733,12 +733,13 @@ fn parse_sub_delim(str: String) -> Result(#(String, String), Nil) { case string.pop_grapheme(str) { Ok(#(char, tail)) -> { let assert [codepoint] = string.to_utf_codepoints(char) - case string.utf_codepoint_to_int(codepoint) { - i if i >= 0x26 && i <= 0x2C -> Ok(#(char, tail)) - i if i == 0x21 -> Ok(#(char, tail)) - i if i == 0x24 -> Ok(#(char, tail)) - i if i == 0x3B -> Ok(#(char, tail)) - i if i == 0x3D -> Ok(#(char, tail)) + let i = string.utf_codepoint_to_int(codepoint) + case i { + _ if i >= 0x26 && i <= 0x2C -> Ok(#(char, tail)) + _ if i == 0x21 -> Ok(#(char, tail)) + _ if i == 0x24 -> Ok(#(char, tail)) + _ if i == 0x3B -> Ok(#(char, tail)) + _ if i == 0x3D -> Ok(#(char, tail)) _ -> Error(Nil) } } @@ -751,8 +752,9 @@ fn parse_digit(str: String) -> Result(#(String, String), Nil) { case string.pop_grapheme(str) { Ok(#(char, tail)) -> { let assert [codepoint] = string.to_utf_codepoints(char) - case string.utf_codepoint_to_int(codepoint) { - i if i >= 0x30 && i <= 0x39 -> Ok(#(char, tail)) + let i = string.utf_codepoint_to_int(codepoint) + case i { + _ if i >= 0x30 && i <= 0x39 -> Ok(#(char, tail)) _ -> Error(Nil) } } @@ -765,8 +767,9 @@ fn parse_digit_nz(str: String) -> Result(#(String, String), Nil) { case string.pop_grapheme(str) { Ok(#(char, tail)) -> { let assert [codepoint] = string.to_utf_codepoints(char) - case string.utf_codepoint_to_int(codepoint) { - i if i >= 0x31 && i <= 0x39 -> Ok(#(char, tail)) + let i = string.utf_codepoint_to_int(codepoint) + case i { + _ if i >= 0x31 && i <= 0x39 -> Ok(#(char, tail)) _ -> Error(Nil) } } @@ -788,9 +791,10 @@ fn parse_alpha(str: String) -> Result(#(String, String), Nil) { case string.pop_grapheme(str) { Ok(#(char, tail)) -> { let assert [codepoint] = string.to_utf_codepoints(char) - case string.utf_codepoint_to_int(codepoint) { - i if i >= 0x41 && i <= 0x5A -> Ok(#(char, tail)) - i if i >= 0x61 && i <= 0x7A -> Ok(#(char, tail)) + let i = string.utf_codepoint_to_int(codepoint) + case i { + _ if i >= 0x41 && i <= 0x5A -> Ok(#(char, tail)) + _ if i >= 0x61 && i <= 0x7A -> Ok(#(char, tail)) _ -> Error(Nil) } } diff --git a/src/gluri/internal/utils.gleam b/src/gluri/internal/utils.gleam index 0a648ef..4b900f6 100644 --- a/src/gluri/internal/utils.gleam +++ b/src/gluri/internal/utils.gleam @@ -374,10 +374,11 @@ pub fn parse_hex_digit(str: String) -> Result(#(String, String), Nil) { case string.pop_grapheme(str) { Ok(#(char, tail)) -> { let assert [codepoint] = string.to_utf_codepoints(char) - case string.utf_codepoint_to_int(codepoint) { - i if i >= 0x30 && i <= 0x39 -> Ok(#(char, tail)) - i if i >= 0x41 && i <= 0x46 -> Ok(#(char, tail)) - i if i >= 0x61 && i <= 0x66 -> Ok(#(char, tail)) + let i = string.utf_codepoint_to_int(codepoint) + case i { + _ if i >= 0x30 && i <= 0x39 -> Ok(#(char, tail)) + _ if i >= 0x41 && i <= 0x46 -> Ok(#(char, tail)) + _ if i >= 0x61 && i <= 0x66 -> Ok(#(char, tail)) _ -> Error(Nil) } }