From 8b8d3e577ec06df76ec8b2e3635f17e42c1e40a7 Mon Sep 17 00:00:00 2001 From: Gareth Pendleton Date: Sun, 14 Sep 2025 17:11:04 +0100 Subject: [PATCH] perf: Add tweak to parse known schema more quickly http, https, and a few other urls can be assumed to be possible schemes. If we check for these then we cut down on the character by character parsing that would otherwise happen --- src/gluri/internal/parser.gleam | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/gluri/internal/parser.gleam b/src/gluri/internal/parser.gleam index 7c9406d..783630c 100644 --- a/src/gluri/internal/parser.gleam +++ b/src/gluri/internal/parser.gleam @@ -603,15 +603,31 @@ fn parse_userinfo( } fn parse_scheme(str: String) -> Result(#(Uri, String), Nil) { - case parse_alpha(str) { - Ok(#(first, rest)) -> { - case do_parse_scheme(rest, first) { - Error(_) -> Error(Nil) - Ok(#(scheme, rest)) -> - Ok(#(Uri(Some(scheme), None, None, None, "", None, None), rest)) + case str { + "http:" <> rest -> + Ok(#(Uri(Some("http"), None, None, None, "", None, None), rest)) + "https:" <> rest -> + Ok(#(Uri(Some("https"), None, None, None, "", None, None), rest)) + "ftp:" <> rest -> + Ok(#(Uri(Some("ftp"), None, None, None, "", None, None), rest)) + "file:" <> rest -> + Ok(#(Uri(Some("file"), None, None, None, "", None, None), rest)) + "ws:" <> rest -> + Ok(#(Uri(Some("ws"), None, None, None, "", None, None), rest)) + "wss:" <> rest -> + Ok(#(Uri(Some("wss"), None, None, None, "", None, None), rest)) + _ -> { + case parse_alpha(str) { + Ok(#(first, rest)) -> { + case do_parse_scheme(rest, first) { + Error(_) -> Error(Nil) + Ok(#(scheme, rest)) -> + Ok(#(Uri(Some(scheme), None, None, None, "", None, None), rest)) + } + } + _ -> Error(Nil) } } - _ -> Error(Nil) } }