From 103b76f91d4e6c4ef82a51c39895f0d025234e22 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Wed, 15 Sep 2021 21:21:55 +0100 Subject: Use Erlang uri parser --- src/gleam_stdlib.erl | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src/gleam_stdlib.erl') diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index f607497..9a27c18 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -6,7 +6,7 @@ decode_float/1, decode_thunk/1, decode_list/1, decode_option/2, decode_field/2, parse_int/1, parse_float/1, less_than/2, string_pop_grapheme/1, string_starts_with/2, wrap_list/1, - string_ends_with/2, string_pad/4, decode_map/1, + string_ends_with/2, string_pad/4, decode_map/1, uri_parse/1, bit_string_int_to_u32/1, bit_string_int_from_u32/1, decode_result/1, bit_string_slice/3, decode_bit_string/1, compile_regex/2, regex_scan/2, percent_encode/1, percent_decode/1, regex_check/2, regex_split/2, @@ -298,3 +298,39 @@ check_utf8(Cs) -> {error, _, _} -> {error, nil}; _ -> {ok, Cs} end. + +uri_parse(String) -> + case uri_string:parse(String) of + {error, _, _} -> {error, nil}; + Uri -> + % #{ + % host := Host, path := Path, port := Port, query := Query, + % scheme := Scheme, userinfo := Userinfo + % } -> + % scheme: Option(String), + % userinfo: Option(String), + % host: Option(String), + % port: Option(Int), + % path: String, + % query: Option(String), + % fragment: Option(String), + {ok, {uri, + maps_get_optional(Uri, scheme), + maps_get_optional(Uri, userinfo), + maps_get_optional(Uri, host), + maps_get_optional(Uri, port), + maps_get_or(Uri, path, <<>>), + maps_get_optional(Uri, query), + maps_get_optional(Uri, fragment) + }} + end. + +maps_get_optional(Map, Key) -> + try {some, maps:get(Key, Map)} + catch _:_ -> none + end. + +maps_get_or(Map, Key, Default) -> + try maps:get(Key, Map) + catch _:_ -> Default + end. -- cgit v1.2.3