Skip to content

Commit 1efb030

Browse files
Juraj Hlistalastres
Juraj Hlista
authored andcommitted
Replace string:to_lower with a faster version
1 parent fad2c7b commit 1efb030

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

src/lhttpc_client.erl

+5-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
-define(HTTP_LINE_END, "\r\n").
5959
-define(CONNECTION_HDR(HDRS, DEFAULT),
60-
string:to_lower(lhttpc_lib:header_value("connection", HDRS, DEFAULT))).
60+
lhttpc_lib:to_lower(lhttpc_lib:header_value("connection", HDRS, DEFAULT))).
6161

6262
-record(client_state, {
6363
host :: string(),
@@ -184,7 +184,7 @@ handle_call({request, PathOrUrl, Method, Hdrs, Body, Options}, From,
184184
ProxyUrl when is_list(ProxyUrl) ->
185185
lhttpc_lib:parse_url(ProxyUrl)
186186
end,
187-
FinalPath, FinalHeaders, Host, Port} =
187+
{FinalPath, FinalHeaders, Host, Port} =
188188
url_extract(PathOrUrl, Hdrs, ClientHost, ClientPort),
189189
case {Host, Port} =:= {ClientHost, ClientPort} of
190190
true ->
@@ -654,7 +654,7 @@ has_body(_, _, _) ->
654654
body_type(Hdrs) ->
655655
case lhttpc_lib:header_value("content-length", Hdrs) of
656656
undefined ->
657-
TransferEncoding = string:to_lower(
657+
TransferEncoding = lhttpc_lib:to_lower(
658658
lhttpc_lib:header_value("transfer-encoding", Hdrs, "undefined")
659659
),
660660
case TransferEncoding of
@@ -922,13 +922,13 @@ read_infinite_body_part(#client_state{socket = Socket, ssl = Ssl}) ->
922922
%%------------------------------------------------------------------------------
923923
check_infinite_response({1, Minor}, Hdrs) when Minor >= 1 ->
924924
HdrValue = lhttpc_lib:header_value("connection", Hdrs, "keep-alive"),
925-
case string:to_lower(HdrValue) of
925+
case lhttpc_lib:to_lower(HdrValue) of
926926
"close" -> ok;
927927
_ -> erlang:error(no_content_length)
928928
end;
929929
check_infinite_response(_, Hdrs) ->
930930
HdrValue = lhttpc_lib:header_value("connection", Hdrs, "close"),
931-
case string:to_lower(HdrValue) of
931+
case lhttpc_lib:to_lower(HdrValue) of
932932
"keep-alive" -> erlang:error(no_content_length);
933933
_ -> ok
934934
end.

src/lhttpc_lib.erl

+48-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
format_hdrs/1,
4242
dec/1,
4343
get_cookies/1,
44-
update_cookies/2]).
44+
update_cookies/2,
45+
to_lower/1]).
4546

4647
-include("lhttpc_types.hrl").
4748
-include("lhttpc.hrl").
@@ -90,7 +91,7 @@ header_value(Hdr, [{ThisHdr, Value}| Hdrs], Default) when is_atom(ThisHdr) ->
9091
header_value(Hdr, [{ThisHdr, Value}| Hdrs], Default) when is_binary(ThisHdr) ->
9192
header_value(Hdr, [{binary_to_list(ThisHdr), Value}| Hdrs], Default);
9293
header_value(Hdr, [{ThisHdr, Value}| Hdrs], Default) ->
93-
case string:equal(string:to_lower(ThisHdr), Hdr) of
94+
case string:equal(lhttpc_lib:to_lower(ThisHdr), Hdr) of
9495
true -> case is_list(Value) of
9596
true -> string:strip(Value);
9697
false -> Value
@@ -129,7 +130,7 @@ parse_url(URL) ->
129130
{User, Passwd, HostPortPath} = split_credentials(CredsHostPortPath),
130131
{Host, PortPath} = split_host(HostPortPath, []),
131132
{Port, Path} = split_port(Scheme, PortPath, []),
132-
#lhttpc_url{host = string:to_lower(Host), port = Port, path = Path,
133+
#lhttpc_url{host = lhttpc_lib:to_lower(Host), port = Port, path = Path,
133134
user = User, password = Passwd, is_ssl = (Scheme =:= https)}.
134135

135136
%%------------------------------------------------------------------------------
@@ -216,6 +217,15 @@ update_cookies(RespHeaders, StateCookies) ->
216217
%% Delete the cookies that are expired (check max-age and expire fields).
217218
delete_expired_cookies(NewCookies).
218219

220+
221+
%%------------------------------------------------------------------------------
222+
%% @doc Converts characters in a string ro lower case.
223+
%% @end
224+
%%------------------------------------------------------------------------------
225+
-spec to_lower(string()) -> string().
226+
to_lower(String) ->
227+
[char_to_lower(X) || X <- String].
228+
219229
%%==============================================================================
220230
%% Internal functions
221231
%%==============================================================================
@@ -555,7 +565,7 @@ add_content_headers(Hdrs, _Body, true) ->
555565
{undefined, undefined} ->
556566
[{"Transfer-Encoding", "chunked"} | Hdrs];
557567
{undefined, TransferEncoding} ->
558-
case string:to_lower(TransferEncoding) of
568+
case lhttpc_lib:to_lower(TransferEncoding) of
559569
"chunked" -> Hdrs;
560570
_ -> erlang:error({error, unsupported_transfer_encoding})
561571
end;
@@ -586,7 +596,7 @@ add_host(Hdrs, Host, Port) ->
586596
%%------------------------------------------------------------------------------
587597
-spec is_chunked(headers()) -> boolean().
588598
is_chunked(Hdrs) ->
589-
TransferEncoding = string:to_lower(
599+
TransferEncoding = lhttpc_lib:to_lower(
590600
header_value("transfer-encoding", Hdrs, "undefined")),
591601
case TransferEncoding of
592602
"chunked" -> true;
@@ -619,3 +629,36 @@ maybe_ipv6_enclose(Host) ->
619629
_ ->
620630
Host
621631
end.
632+
633+
%%------------------------------------------------------------------------------
634+
%% @private
635+
%% @doc
636+
%% @end
637+
%%------------------------------------------------------------------------------
638+
char_to_lower($A) -> $a;
639+
char_to_lower($B) -> $b;
640+
char_to_lower($C) -> $c;
641+
char_to_lower($D) -> $d;
642+
char_to_lower($E) -> $e;
643+
char_to_lower($F) -> $f;
644+
char_to_lower($G) -> $g;
645+
char_to_lower($H) -> $h;
646+
char_to_lower($I) -> $i;
647+
char_to_lower($J) -> $j;
648+
char_to_lower($K) -> $k;
649+
char_to_lower($L) -> $l;
650+
char_to_lower($M) -> $m;
651+
char_to_lower($N) -> $n;
652+
char_to_lower($O) -> $o;
653+
char_to_lower($P) -> $p;
654+
char_to_lower($Q) -> $q;
655+
char_to_lower($R) -> $r;
656+
char_to_lower($S) -> $s;
657+
char_to_lower($T) -> $t;
658+
char_to_lower($U) -> $u;
659+
char_to_lower($V) -> $v;
660+
char_to_lower($W) -> $w;
661+
char_to_lower($X) -> $x;
662+
char_to_lower($Y) -> $y;
663+
char_to_lower($Z) -> $z;
664+
char_to_lower(Ch) -> Ch.

0 commit comments

Comments
 (0)