Skip to content

Commit

Permalink
first post
Browse files Browse the repository at this point in the history
git-svn-id: http://mochiweb.googlecode.com/svn/trunk@2 583d1f64-3c3e-0410-8459-0f15fcd77d59
  • Loading branch information
matthew.dempsky committed Nov 6, 2007
1 parent d423348 commit bf85f11
Show file tree
Hide file tree
Showing 20 changed files with 2,839 additions and 0 deletions.
9 changes: 9 additions & 0 deletions LICENSE
@@ -0,0 +1,9 @@
This is the MIT license.

Copyright (c) 2007 Mochi Media, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 changes: 11 additions & 0 deletions Makefile
@@ -0,0 +1,11 @@
all:
(cd src;$(MAKE))

mmall:
(cd src;$(MAKE) mmbuild)

local:
(cd src;$(MAKE) local)

clean:
(cd src;$(MAKE) clean)
1 change: 1 addition & 0 deletions README
@@ -0,0 +1 @@
MochiWeb is an Erlang library for building lightweight HTTP servers.
15 changes: 15 additions & 0 deletions src/Makefile
@@ -0,0 +1,15 @@
include ../support/include.mk

all: $(EBIN_FILES)

mmbuild: $(EBIN_FILES_NO_DOCS)

mmdocs: $(EBIN_DOCUMENTS)

local: $(ERL_OBJECTS_LOCAL)

debug:
$(MAKE) DEBUG=-DDEBUG

clean:
rm -rf $(EBIN_FILES)
75 changes: 75 additions & 0 deletions src/mochihex.erl
@@ -0,0 +1,75 @@
%% @author Bob Ippolito <bob@mochimedia.com>
%% @copyright 2006 Mochi Media, Inc.

%% @doc Utilities for working with hexadecimal strings.

-module(mochihex).
-author('bob@mochimedia.com').

-export([test/0, to_hex/1, to_bin/1, to_int/1, dehex/1, hexdigit/1]).

%% @type iolist() = [char() | binary() | iolist()]
%% @type iodata() = iolist() | binary()

%% @spec to_hex(integer | iolist()) -> string()
%% @doc Convert an iolist to a hexadecimal string.
to_hex(0) ->
"0";
to_hex(I) when is_integer(I), I > 0 ->
to_hex_int(I, []);
to_hex(B) ->
to_hex(iolist_to_binary(B), []).

%% @spec to_bin(string()) -> binary()
%% @doc Convert a hexadecimal string to a binary.
to_bin(L) ->
to_bin(L, []).

%% @spec to_int(string()) -> integer()
%% @doc Convert a hexadecimal string to an integer.
to_int(L) ->
erlang:list_to_integer(L, 16).

%% @spec dehex(char()) -> integer()
%% @doc Convert a hex digit to its integer value.
dehex(C) when C >= $0, C =< $9 ->
C - $0;
dehex(C) when C >= $a, C =< $f ->
C - $a + 10;
dehex(C) when C >= $A, C =< $F ->
C - $A + 10.

%% @spec hexdigit(integer()) -> char()
%% @doc Convert an integer less than 16 to a hex digit.
hexdigit(C) when C >= 0, C =< 9 ->
C + $0;
hexdigit(C) when C =< 15 ->
C + $a - 10.

%% @spec test() -> ok
%% @doc Test this module.
test() ->
"ff000ff1" = to_hex([255, 0, 15, 241]),
<<255, 0, 15, 241>> = to_bin("ff000ff1"),
16#ff000ff1 = to_int("ff000ff1"),
"ff000ff1" = to_hex(16#ff000ff1),
ok.


%% Internal API

to_hex(<<>>, Acc) ->
lists:reverse(Acc);
to_hex(<<C1:4, C2:4, Rest/binary>>, Acc) ->
to_hex(Rest, [hexdigit(C2), hexdigit(C1) | Acc]).

to_hex_int(0, Acc) ->
Acc;
to_hex_int(I, Acc) ->
to_hex_int(I bsr 4, [hexdigit(I band 15) | Acc]).

to_bin([], Acc) ->
iolist_to_binary(lists:reverse(Acc));
to_bin([C1, C2 | Rest], Acc) ->
to_bin(Rest, [(dehex(C1) bsl 4) bor dehex(C2) | Acc]).

0 comments on commit bf85f11

Please sign in to comment.