aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.erl
diff options
context:
space:
mode:
authorrubytree <rt@rubytree.me>2023-04-30 13:09:26 +0200
committerLouis Pilfold <louis@lpil.uk>2023-05-13 16:32:38 +0100
commit037e2c575c367666a952f99ea4d9cc42268cedec (patch)
treefe0857f1ac1593696bfbad333c14e1ee3881cc1c /src/gleam_stdlib.erl
parent37b4a44fe8f63b6314f3611f8ec3dab820f34e3a (diff)
downloadgleam_stdlib-037e2c575c367666a952f99ea4d9cc42268cedec.tar.gz
gleam_stdlib-037e2c575c367666a952f99ea4d9cc42268cedec.zip
`dynamic.tupleN/2` optimization
Diffstat (limited to 'src/gleam_stdlib.erl')
-rw-r--r--src/gleam_stdlib.erl31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 2ede96a..79ded3f 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -8,10 +8,11 @@
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,
- base_decode64/1, parse_query/1, bit_string_concat/1, size_of_tuple/1,
- decode_tuple/1, tuple_get/2, classify_dynamic/1, print/1, println/1,
- print_error/1, println_error/1, inspect/1, float_to_string/1,
- int_from_base_string/2, list_to_tuple/1]).
+ base_decode64/1, parse_query/1, bit_string_concat/1, size_of_tuple/1,
+ decode_tuple/1, decode_tuple2/1, decode_tuple3/1, decode_tuple4/1,
+ decode_tuple5/1, decode_tuple6/1, tuple_get/2, classify_dynamic/1,
+ print/1, println/1, print_error/1, println_error/1, inspect/1,
+ float_to_string/1, int_from_base_string/2]).
%% Taken from OTP's uri_string module
-define(DEC2HEX(X),
@@ -96,6 +97,26 @@ tuple_get(Data, Index) -> {ok, element(Index + 1, Data)}.
decode_tuple(Data) when is_tuple(Data) -> {ok, Data};
decode_tuple(Data) -> decode_error_msg(<<"Tuple">>, Data).
+decode_tuple2({_,_} = A) -> {ok, A};
+decode_tuple2([A,B]) -> {ok, {A,B}};
+decode_tuple2(Data) -> decode_error_msg(<<"Tuple or List of 2 elements">>, Data).
+
+decode_tuple3({_,_,_} = A) -> {ok, A};
+decode_tuple3([A,B,C]) -> {ok, {A,B,C}};
+decode_tuple3(Data) -> decode_error_msg(<<"Tuple or List of 3 elements">>, Data).
+
+decode_tuple4({_,_,_,_} = A) -> {ok, A};
+decode_tuple4([A,B,C,D]) -> {ok, {A,B,C,D}};
+decode_tuple4(Data) -> decode_error_msg(<<"Tuple or List of 4 elements">>, Data).
+
+decode_tuple5({_,_,_,_,_} = A) -> {ok, A};
+decode_tuple5([A,B,C,D,E]) -> {ok, {A,B,C,D,E}};
+decode_tuple5(Data) -> decode_error_msg(<<"Tuple or List of 5 elements">>, Data).
+
+decode_tuple6({_,_,_,_,_,_} = A) -> {ok, A};
+decode_tuple6([A,B,C,D,E,F]) -> {ok, {A,B,C,D,E,F}};
+decode_tuple6(Data) -> decode_error_msg(<<"Tuple or List of 6 elements">>, Data).
+
decode_option(Term, F) ->
Decode = fun(Inner) ->
case F(Inner) of
@@ -419,5 +440,3 @@ inspect_maybe_utf8_string(Binary, Acc) ->
float_to_string(Float) when is_float(Float) ->
erlang:iolist_to_binary(io_lib_format:fwrite_g(Float)).
-list_to_tuple(Data) when is_list(Data) -> {ok, erlang:list_to_tuple(Data)};
-list_to_tuple(Data) -> decode_error_msg(<<"List">>, Data). \ No newline at end of file