PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms 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
  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
 22%% @doc Utilities for working with hexadecimal strings.
 23
 24-module(mochihex).
 25-author('bob@mochimedia.com').
 26
 27-export([to_hex/1, to_bin/1, to_int/1, dehex/1, hexdigit/1]).
 28
 29%% @spec to_hex(integer | iolist()) -> string()
 30%% @doc Convert an iolist to a hexadecimal string.
 31to_hex(0) ->
 32    "0";
 33to_hex(I) when is_integer(I), I > 0 ->
 34    to_hex_int(I, []);
 35to_hex(B) ->
 36    to_hex(iolist_to_binary(B), []).
 37
 38%% @spec to_bin(string()) -> binary()
 39%% @doc Convert a hexadecimal string to a binary.
 40to_bin(L) ->
 41    to_bin(L, []).
 42
 43%% @spec to_int(string()) -> integer()
 44%% @doc Convert a hexadecimal string to an integer.
 45to_int(L) ->
 46    erlang:list_to_integer(L, 16).
 47
 48%% @spec dehex(char()) -> integer()
 49%% @doc Convert a hex digit to its integer value.
 50dehex(C) when C >= $0, C =< $9 ->
 51    C - $0;
 52dehex(C) when C >= $a, C =< $f ->
 53    C - $a + 10;
 54dehex(C) when C >= $A, C =< $F ->
 55    C - $A + 10.
 56
 57%% @spec hexdigit(integer()) -> char()
 58%% @doc Convert an integer less than 16 to a hex digit.
 59hexdigit(C) when C >= 0, C =< 9 ->
 60    C + $0;
 61hexdigit(C) when C =< 15 ->
 62    C + $a - 10.
 63
 64%% Internal API
 65
 66to_hex(<<>>, Acc) ->
 67    lists:reverse(Acc);
 68to_hex(<<C1:4, C2:4, Rest/binary>>, Acc) ->
 69    to_hex(Rest, [hexdigit(C2), hexdigit(C1) | Acc]).
 70
 71to_hex_int(0, Acc) ->
 72    Acc;
 73to_hex_int(I, Acc) ->
 74    to_hex_int(I bsr 4, [hexdigit(I band 15) | Acc]).
 75
 76to_bin([], Acc) ->
 77    iolist_to_binary(lists:reverse(Acc));
 78to_bin([C1, C2 | Rest], Acc) ->
 79    to_bin(Rest, [(dehex(C1) bsl 4) bor dehex(C2) | Acc]).
 80
 81
 82
 83%%
 84%% Tests
 85%%
 86-ifdef(TEST).
 87-include_lib("eunit/include/eunit.hrl").
 88
 89to_hex_test() ->
 90    "ff000ff1" = to_hex([255, 0, 15, 241]),
 91    "ff000ff1" = to_hex(16#ff000ff1),
 92    "0" = to_hex(16#0),
 93    ok.
 94
 95to_bin_test() ->
 96    <<255, 0, 15, 241>> = to_bin("ff000ff1"),
 97    <<255, 0, 10, 161>> = to_bin("Ff000aA1"),
 98    ok.
 99
100to_int_test() ->
101    16#ff000ff1 = to_int("ff000ff1"),
102    16#ff000aa1 = to_int("FF000Aa1"),
103    16#0 = to_int("0"),
104    ok.
105
106-endif.