From bcc245b2a9b3baf0ae9c988a6ce12198d1cf915c Mon Sep 17 00:00:00 2001 From: "Gavin M. Roy" Date: Thu, 27 Oct 2016 12:48:42 -0400 Subject: [PATCH] Shrink up the hex_to_upper method --- src/urilib.erl | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/urilib.erl b/src/urilib.erl index bc9e8a3..b579ad9 100644 --- a/src/urilib.erl +++ b/src/urilib.erl @@ -321,16 +321,20 @@ url_maybe_add_fragment(Value, URL) -> -spec hex_to_upper(string()) -> string(). %% @private hex_to_upper(Value) -> - hex_to_upper(Value, [], []). -hex_to_upper([], [], Value) -> - Value; -hex_to_upper([], Hex, Value) -> - lists:append(Value, string:to_upper(Hex)); -hex_to_upper([37|T], [], Value) -> - hex_to_upper(T, [37], Value); -hex_to_upper([H|T], [], Value) -> - hex_to_upper(T, [], lists:append(Value, [H])); -hex_to_upper(Remaining, Hex, Value) when length(Hex) == 3 -> - hex_to_upper(Remaining, [], lists:append(Value, string:to_upper(Hex))); -hex_to_upper([H|T], Hex, Value) -> - hex_to_upper(T, lists:append(Hex, [H]), Value). + hex_to_upper(Value, []). +hex_to_upper([], Accum) -> + Accum; +hex_to_upper([$%, B1, B2|T], Accum) -> + hex_to_upper(T, lists:append(Accum, [$%, to_upper(B1), to_upper(B2)])); +hex_to_upper([H|T], Accum) -> + hex_to_upper(T, lists:append(Accum, [H])). + + +-spec to_upper(char()) -> char(). +to_upper($a) -> $A; +to_upper($b) -> $B; +to_upper($c) -> $C; +to_upper($d) -> $D; +to_upper($e) -> $E; +to_upper($f) -> $F; +to_upper(C) -> C.