PageRenderTime 105ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mochihex.erl

http://github.com/basho/mochiweb
Erlang | 106 lines | 52 code | 19 blank | 35 comment | 0 complexity | 2eb1299a4c911114fe3dd41ac4b1c736 MD5 | raw file
Possible License(s): MIT
  1. %% @author Bob Ippolito <bob@mochimedia.com>
  2. %% @copyright 2006 Mochi Media, Inc.
  3. %%
  4. %% Permission is hereby granted, free of charge, to any person obtaining a
  5. %% copy of this software and associated documentation files (the "Software"),
  6. %% to deal in the Software without restriction, including without limitation
  7. %% the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. %% and/or sell copies of the Software, and to permit persons to whom the
  9. %% Software is furnished to do so, subject to the following conditions:
  10. %%
  11. %% The above copyright notice and this permission notice shall be included in
  12. %% all copies or substantial portions of the Software.
  13. %%
  14. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. %% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. %% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. %% DEALINGS IN THE SOFTWARE.
  21. %% @doc Utilities for working with hexadecimal strings.
  22. -module(mochihex).
  23. -author('bob@mochimedia.com').
  24. -export([to_hex/1, to_bin/1, to_int/1, dehex/1, hexdigit/1]).
  25. %% @spec to_hex(integer | iolist()) -> string()
  26. %% @doc Convert an iolist to a hexadecimal string.
  27. to_hex(0) ->
  28. "0";
  29. to_hex(I) when is_integer(I), I > 0 ->
  30. to_hex_int(I, []);
  31. to_hex(B) ->
  32. to_hex(iolist_to_binary(B), []).
  33. %% @spec to_bin(string()) -> binary()
  34. %% @doc Convert a hexadecimal string to a binary.
  35. to_bin(L) ->
  36. to_bin(L, []).
  37. %% @spec to_int(string()) -> integer()
  38. %% @doc Convert a hexadecimal string to an integer.
  39. to_int(L) ->
  40. erlang:list_to_integer(L, 16).
  41. %% @spec dehex(char()) -> integer()
  42. %% @doc Convert a hex digit to its integer value.
  43. dehex(C) when C >= $0, C =< $9 ->
  44. C - $0;
  45. dehex(C) when C >= $a, C =< $f ->
  46. C - $a + 10;
  47. dehex(C) when C >= $A, C =< $F ->
  48. C - $A + 10.
  49. %% @spec hexdigit(integer()) -> char()
  50. %% @doc Convert an integer less than 16 to a hex digit.
  51. hexdigit(C) when C >= 0, C =< 9 ->
  52. C + $0;
  53. hexdigit(C) when C =< 15 ->
  54. C + $a - 10.
  55. %% Internal API
  56. to_hex(<<>>, Acc) ->
  57. lists:reverse(Acc);
  58. to_hex(<<C1:4, C2:4, Rest/binary>>, Acc) ->
  59. to_hex(Rest, [hexdigit(C2), hexdigit(C1) | Acc]).
  60. to_hex_int(0, Acc) ->
  61. Acc;
  62. to_hex_int(I, Acc) ->
  63. to_hex_int(I bsr 4, [hexdigit(I band 15) | Acc]).
  64. to_bin([], Acc) ->
  65. iolist_to_binary(lists:reverse(Acc));
  66. to_bin([C1, C2 | Rest], Acc) ->
  67. to_bin(Rest, [(dehex(C1) bsl 4) bor dehex(C2) | Acc]).
  68. %%
  69. %% Tests
  70. %%
  71. -ifdef(TEST).
  72. -include_lib("eunit/include/eunit.hrl").
  73. to_hex_test() ->
  74. "ff000ff1" = to_hex([255, 0, 15, 241]),
  75. "ff000ff1" = to_hex(16#ff000ff1),
  76. "0" = to_hex(16#0),
  77. ok.
  78. to_bin_test() ->
  79. <<255, 0, 15, 241>> = to_bin("ff000ff1"),
  80. <<255, 0, 10, 161>> = to_bin("Ff000aA1"),
  81. ok.
  82. to_int_test() ->
  83. 16#ff000ff1 = to_int("ff000ff1"),
  84. 16#ff000aa1 = to_int("FF000Aa1"),
  85. 16#0 = to_int("0"),
  86. ok.
  87. -endif.