couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cml...@apache.org
Subject svn commit: r642953 [1/3] - in /incubator/couchdb/vendor/mochiweb: ./ current/ current/deps/ current/doc/ current/ebin/ current/include/ current/priv/ current/priv/skel/ current/priv/skel/deps/ current/priv/skel/doc/ current/priv/skel/ebin/ current/pri...
Date Mon, 31 Mar 2008 10:30:34 GMT
Author: cmlenz
Date: Mon Mar 31 03:30:27 2008
New Revision: 642953

URL: http://svn.apache.org/viewvc?rev=642953&view=rev
Log:
Create mochiweb vendor branch based on r64 from <http://mochiweb.googlecode.com/svn/trunk/>.

Added:
    incubator/couchdb/vendor/mochiweb/
    incubator/couchdb/vendor/mochiweb/current/
    incubator/couchdb/vendor/mochiweb/current/LICENSE
    incubator/couchdb/vendor/mochiweb/current/Makefile   (with props)
    incubator/couchdb/vendor/mochiweb/current/README
    incubator/couchdb/vendor/mochiweb/current/deps/
    incubator/couchdb/vendor/mochiweb/current/doc/   (with props)
    incubator/couchdb/vendor/mochiweb/current/ebin/   (with props)
    incubator/couchdb/vendor/mochiweb/current/include/
    incubator/couchdb/vendor/mochiweb/current/priv/
    incubator/couchdb/vendor/mochiweb/current/priv/skel/
    incubator/couchdb/vendor/mochiweb/current/priv/skel/Makefile   (with props)
    incubator/couchdb/vendor/mochiweb/current/priv/skel/deps/
    incubator/couchdb/vendor/mochiweb/current/priv/skel/doc/
    incubator/couchdb/vendor/mochiweb/current/priv/skel/ebin/
    incubator/couchdb/vendor/mochiweb/current/priv/skel/include/
    incubator/couchdb/vendor/mochiweb/current/priv/skel/priv/
    incubator/couchdb/vendor/mochiweb/current/priv/skel/priv/www/
    incubator/couchdb/vendor/mochiweb/current/priv/skel/priv/www/index.html   (with props)
    incubator/couchdb/vendor/mochiweb/current/priv/skel/src/
    incubator/couchdb/vendor/mochiweb/current/priv/skel/src/Makefile   (with props)
    incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.app
    incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.erl
    incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.hrl
    incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_app.erl
    incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_deps.erl
    incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_sup.erl
    incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_web.erl
    incubator/couchdb/vendor/mochiweb/current/priv/skel/start-dev.sh   (with props)
    incubator/couchdb/vendor/mochiweb/current/priv/skel/start.sh   (with props)
    incubator/couchdb/vendor/mochiweb/current/priv/skel/support/
    incubator/couchdb/vendor/mochiweb/current/priv/skel/support/include.mk
    incubator/couchdb/vendor/mochiweb/current/scripts/
    incubator/couchdb/vendor/mochiweb/current/scripts/new_mochiweb.erl
    incubator/couchdb/vendor/mochiweb/current/src/
    incubator/couchdb/vendor/mochiweb/current/src/Makefile   (with props)
    incubator/couchdb/vendor/mochiweb/current/src/mochihex.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochijson.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochijson2.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochinum.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb.app
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_app.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_charref.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_cookies.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_echo.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_headers.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_html.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_http.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_multipart.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_request.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_response.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_skel.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_socket_server.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_sup.erl
    incubator/couchdb/vendor/mochiweb/current/src/mochiweb_util.erl
    incubator/couchdb/vendor/mochiweb/current/src/reloader.erl
    incubator/couchdb/vendor/mochiweb/current/support/
    incubator/couchdb/vendor/mochiweb/current/support/include.mk

Added: incubator/couchdb/vendor/mochiweb/current/LICENSE
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/LICENSE?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/LICENSE (added)
+++ incubator/couchdb/vendor/mochiweb/current/LICENSE Mon Mar 31 03:30:27 2008
@@ -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.

Added: incubator/couchdb/vendor/mochiweb/current/Makefile
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/Makefile?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/Makefile (added)
+++ incubator/couchdb/vendor/mochiweb/current/Makefile Mon Mar 31 03:30:27 2008
@@ -0,0 +1,8 @@
+all:
+	(cd src;$(MAKE))
+
+test:
+	(cd src;$(MAKE) test)
+
+clean:
+	(cd src;$(MAKE) clean)

Propchange: incubator/couchdb/vendor/mochiweb/current/Makefile
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/couchdb/vendor/mochiweb/current/README
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/README?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/README (added)
+++ incubator/couchdb/vendor/mochiweb/current/README Mon Mar 31 03:30:27 2008
@@ -0,0 +1 @@
+MochiWeb is an Erlang library for building lightweight HTTP servers.

Propchange: incubator/couchdb/vendor/mochiweb/current/doc/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Mar 31 03:30:27 2008
@@ -0,0 +1 @@
+*.html

Propchange: incubator/couchdb/vendor/mochiweb/current/ebin/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Mar 31 03:30:27 2008
@@ -0,0 +1,2 @@
+*.app
+*.beam

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/Makefile
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/Makefile?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/Makefile (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/Makefile Mon Mar 31 03:30:27 2008
@@ -0,0 +1,5 @@
+all:
+	(cd src;$(MAKE))
+
+clean:
+	(cd src;$(MAKE) clean)

Propchange: incubator/couchdb/vendor/mochiweb/current/priv/skel/Makefile
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/priv/www/index.html
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/priv/www/index.html?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/priv/www/index.html (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/priv/www/index.html Mon Mar 31 03:30:27 2008
@@ -0,0 +1,8 @@
+<html>
+<head>
+<title>It Worked</title>
+</head>
+<body>
+MochiWeb running.
+</body>
+</html>

Propchange: incubator/couchdb/vendor/mochiweb/current/priv/skel/priv/www/index.html
------------------------------------------------------------------------------
    svn:mime-type = text/html

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/src/Makefile
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/src/Makefile?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/src/Makefile (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/src/Makefile Mon Mar 31 03:30:27 2008
@@ -0,0 +1,9 @@
+include ../support/include.mk
+
+all: $(EBIN_FILES)
+
+debug:
+	$(MAKE) DEBUG=-DDEBUG
+
+clean:
+	rm -rf $(EBIN_FILES)

Propchange: incubator/couchdb/vendor/mochiweb/current/priv/skel/src/Makefile
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.app
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.app?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.app (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.app Mon Mar 31 03:30:27 2008
@@ -0,0 +1,14 @@
+{application, skel,
+ [{description, "skel"},
+  {vsn, "0.01"},
+  {modules, [
+    skel,
+    skel_app,
+    skel_sup,
+    skel_web,
+    skel_deps
+  ]},
+  {registered, []},
+  {mod, {skel_app, []}},
+  {env, []},
+  {applications, [kernel, stdlib, crypto]}]}.

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,30 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc TEMPLATE.
+
+-module(skel).
+-author('author <author@example.com>').
+-export([start/0, stop/0]).
+
+ensure_started(App) ->
+    case application:start(App) of
+	ok ->
+	    ok;
+	{error, {already_started, App}} ->
+	    ok
+    end.
+	
+%% @spec start() -> ok
+%% @doc Start the skel server.
+start() ->
+    skel_deps:ensure(),
+    ensure_started(crypto),
+    application:start(skel).
+
+%% @spec stop() -> ok
+%% @doc Stop the skel server.
+stop() ->
+    Res = application:stop(skel),
+    application:stop(crypto),
+    Res.

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.hrl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.hrl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.hrl (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel.hrl Mon Mar 31 03:30:27 2008
@@ -0,0 +1 @@
+

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_app.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_app.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_app.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_app.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,22 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Callbacks for the skel application.
+
+-module(skel_app).
+-author('author <author@example.com>').
+
+-behaviour(application).
+-export([start/2,stop/1]).
+
+
+%% @spec start(_Type, _StartArgs) -> ServerRet
+%% @doc application start callback for skel.
+start(_Type, _StartArgs) ->
+    skel_deps:ensure(),
+    skel_sup:start_link().
+
+%% @spec stop(_State) -> ServerRet
+%% @doc application stop callback for skel.
+stop(_State) ->
+    ok.

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_deps.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_deps.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_deps.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_deps.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,84 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Ensure that the relatively-installed dependencies are on the code
+%%      loading path, and locate resources relative
+%%      to this application's path.
+
+-module(skel_deps).
+-author('author <author@example.com>').
+
+-export([ensure/0, ensure/1]).
+-export([get_base_dir/0, get_base_dir/1]).
+-export([local_path/1, local_path/2]).
+-export([deps_on_path/0, new_siblings/1]).
+
+%% @spec deps_on_path() -> [ProjNameAndVers]
+%% @doc List of project dependencies on the path.
+deps_on_path() ->
+    F = fun (X, Acc) ->
+                ProjDir = filename:dirname(X),
+                case {filename:basename(X),
+                      filename:basename(filename:dirname(ProjDir))} of
+                    {"ebin", "deps"} ->
+                        [filename:basename(ProjDir) | Acc];
+                    _ ->
+                        Acc
+                end
+        end,
+    ordsets:from_list(lists:foldl(F, [], code:get_path())).
+    
+%% @spec new_siblings(Module) -> [Dir]
+%% @doc Find new siblings paths relative to Module that aren't already on the
+%%      code path.
+new_siblings(Module) ->
+    Existing = deps_on_path(),
+    SiblingEbin = filelib:wildcard(local_path(["deps", "*", "ebin"], Module)),
+    Siblings = [filename:dirname(X) || X <- SiblingEbin,
+                           ordsets:is_element(
+                             filename:basename(filename:dirname(X)),
+                             Existing) =:= false],
+    lists:filter(fun filelib:is_dir/1, 
+                 lists:append([[filename:join([X, "ebin"]),
+                                filename:join([X, "include"])] ||
+                                  X <- Siblings])).
+        
+
+%% @spec ensure(Module) -> ok
+%% @doc Ensure that all ebin and include paths for dependencies
+%%      of the application for Module are on the code path.
+ensure(Module) ->
+    code:add_paths(new_siblings(Module)),
+    code:clash(),
+    ok.
+
+%% @spec ensure() -> ok
+%% @doc Ensure that the ebin and include paths for dependencies of
+%%      this application are on the code path. Equivalent to
+%%      ensure(?Module).
+ensure() ->
+    ensure(?MODULE).
+
+%% @spec get_base_dir(Module) -> string()
+%% @doc Return the application directory for Module. It assumes Module is in
+%%      a standard OTP layout application in the ebin or src directory.
+get_base_dir(Module) ->
+    {file, Here} = code:is_loaded(Module),
+    filename:dirname(filename:dirname(Here)).
+
+%% @spec get_base_dir() -> string()
+%% @doc Return the application directory for this application. Equivalent to
+%%      get_base_dir(?MODULE).
+get_base_dir() ->
+    get_base_dir(?MODULE).
+
+%% @spec local_path([string()], Module) -> string()
+%% @doc Return an application-relative directory from Module's application.
+local_path(Components, Module) ->
+    filename:join([get_base_dir(Module) | Components]).
+
+%% @spec local_path(Components) -> string()
+%% @doc Return an application-relative directory for this application.
+%%      Equivalent to local_path(Components, ?MODULE).
+local_path(Components) ->
+    local_path(Components, ?MODULE).

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_sup.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_sup.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_sup.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_sup.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,54 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Supervisor for the skel application.
+
+-module(skel_sup).
+-author('author <author@example.com>').
+
+-behaviour(supervisor).
+
+%% External exports
+-export([start_link/0, upgrade/0]).
+
+%% supervisor callbacks
+-export([init/1]).
+
+%% @spec start_link() -> ServerRet
+%% @doc API for starting the supervisor.
+start_link() ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% @spec upgrade() -> ok
+%% @doc Add processes if necessary.
+upgrade() ->
+    {ok, {_, Specs}} = init([]),
+
+    Old = sets:from_list(
+	    [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
+    New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
+    Kill = sets:subtract(Old, New),
+
+    sets:fold(fun (Id, ok) ->
+		      supervisor:terminate_child(?MODULE, Id),
+		      supervisor:delete_child(?MODULE, Id),
+		      ok
+	      end, ok, Kill),
+
+    [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
+    ok.
+
+%% @spec init([]) -> SupervisorTree
+%% @doc supervisor callback.
+init([]) ->
+    Ip = case os:getenv("MOCHIWEB_IP") of false -> "0.0.0.0"; Any -> Any end,   
+    WebConfig = [
+         {ip, Ip},
+		 {port, 8000},
+		 {docroot, skel_deps:local_path(["priv", "www"])}],
+    Web = {skel_web,
+	   {skel_web, start, [WebConfig]},
+	   permanent, 5000, worker, dynamic},
+
+    Processes = [Web],
+    {ok, {{one_for_one, 10, 10}, Processes}}.

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_web.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_web.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_web.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/src/skel_web.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,43 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Web server for skel.
+
+-module(skel_web).
+-author('author <author@example.com>').
+
+-export([start/1, stop/0, loop/2]).
+
+%% External API
+
+start(Options) ->
+    {DocRoot, Options1} = get_option(docroot, Options),
+    Loop = fun (Req) ->
+		   ?MODULE:loop(Req, DocRoot)
+	   end,
+    mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
+
+stop() ->
+    mochiweb_http:stop(?MODULE).
+
+loop(Req, DocRoot) ->
+    "/" ++ Path = Req:get(path),
+    case Req:get(method) of
+	Method when Method =:= 'GET'; Method =:= 'HEAD' ->
+	    case Path of
+		_ ->
+		    Req:serve_file(Path, DocRoot)
+	    end;
+	'POST' ->
+	    case Path of
+		_ ->
+		    Req:not_found()
+	    end;
+	_ ->
+	    Req:respond({501, [], []})
+    end.
+
+%% Internal API
+
+get_option(Option, Options) ->
+    {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/start-dev.sh
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/start-dev.sh?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/start-dev.sh (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/start-dev.sh Mon Mar 31 03:30:27 2008
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd `dirname $0`
+exec erl -pa $PWD/ebin $PWD/deps/*/ebin -boot start_sasl -s reloader -s skel

Propchange: incubator/couchdb/vendor/mochiweb/current/priv/skel/start-dev.sh
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/couchdb/vendor/mochiweb/current/priv/skel/start-dev.sh
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/start.sh
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/start.sh?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/start.sh (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/start.sh Mon Mar 31 03:30:27 2008
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd `dirname $0`
+exec erl -pa $PWD/ebin $PWD/deps/*/ebin -boot start_sasl -s skel

Propchange: incubator/couchdb/vendor/mochiweb/current/priv/skel/start.sh
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/couchdb/vendor/mochiweb/current/priv/skel/start.sh
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/couchdb/vendor/mochiweb/current/priv/skel/support/include.mk
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/priv/skel/support/include.mk?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/priv/skel/support/include.mk (added)
+++ incubator/couchdb/vendor/mochiweb/current/priv/skel/support/include.mk Mon Mar 31 03:30:27 2008
@@ -0,0 +1,46 @@
+## -*- makefile -*-
+
+######################################################################
+## Erlang
+
+ERL := erl
+ERLC := $(ERL)c
+
+INCLUDE_DIRS := ../include $(wildcard ../deps/*/include)
+EBIN_DIRS := $(wildcard ../deps/*/ebin)
+ERLC_FLAGS := -W $(INCLUDE_DIRS:../%=-I ../%) $(EBIN_DIRS:%=-pa %)
+
+ifndef no_debug_info
+  ERLC_FLAGS += +debug_info
+endif
+
+ifdef debug
+  ERLC_FLAGS += -Ddebug
+endif
+
+EBIN_DIR := ../ebin
+DOC_DIR  := ../doc
+EMULATOR := beam
+
+ERL_SOURCES := $(wildcard *.erl)
+ERL_HEADERS := $(wildcard *.hrl) $(wildcard ../include/*.hrl)
+ERL_OBJECTS := $(ERL_SOURCES:%.erl=$(EBIN_DIR)/%.$(EMULATOR))
+ERL_DOCUMENTS := $(ERL_SOURCES:%.erl=$(DOC_DIR)/%.html)
+ERL_OBJECTS_LOCAL := $(ERL_SOURCES:%.erl=./%.$(EMULATOR))
+APP_FILES := $(wildcard *.app)
+EBIN_FILES = $(ERL_OBJECTS) $(ERL_DOCUMENTS) $(APP_FILES:%.app=../ebin/%.app)
+EBIN_FILES_NO_DOCS = $(ERL_OBJECTS) $(APP_FILES:%.app=../ebin/%.app)
+MODULES = $(ERL_SOURCES:%.erl=%)
+
+../ebin/%.app: %.app
+	cp $< $@
+
+$(EBIN_DIR)/%.$(EMULATOR): %.erl
+	$(ERLC) $(ERLC_FLAGS) -o $(EBIN_DIR) $<
+
+./%.$(EMULATOR): %.erl
+	$(ERLC) $(ERLC_FLAGS) -o . $<
+
+$(DOC_DIR)/%.html: %.erl
+	$(ERL) -noshell -run edoc file $< -run init stop
+	mv *.html $(DOC_DIR)

Added: incubator/couchdb/vendor/mochiweb/current/scripts/new_mochiweb.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/scripts/new_mochiweb.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/scripts/new_mochiweb.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/scripts/new_mochiweb.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,27 @@
+#!/usr/bin/env escript
+%% -*- mode: erlang -*-
+-export([main/1]).
+
+%% External API
+
+main([Name]) ->
+    main([Name, "."]);
+main([Name, Dest]) ->
+    ensure(),
+    DestDir = filename:absname(Dest),
+    ok = mochiweb_skel:skelcopy(DestDir, Name);
+main(_) ->
+    usage().
+
+%% Internal API
+
+ensure() ->
+    code:add_patha(filename:join(filename:dirname(escript:script_name()),
+                                 "../ebin")).
+
+usage() ->
+    io:format("usage: ~s name [destdir]~n",
+              [filename:basename(escript:script_name())]),
+    halt(1).
+
+

Added: incubator/couchdb/vendor/mochiweb/current/src/Makefile
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/src/Makefile?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/src/Makefile (added)
+++ incubator/couchdb/vendor/mochiweb/current/src/Makefile Mon Mar 31 03:30:27 2008
@@ -0,0 +1,12 @@
+include ../support/include.mk
+
+all: $(EBIN_FILES)
+
+debug:
+	$(MAKE) DEBUG=-DDEBUG
+
+clean:
+	rm -rf $(EBIN_FILES)
+
+test: all
+	$(ERL) -noshell -pa ../ebin -s mochiweb test -s init stop

Propchange: incubator/couchdb/vendor/mochiweb/current/src/Makefile
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/couchdb/vendor/mochiweb/current/src/mochihex.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/src/mochihex.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/src/mochihex.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/src/mochihex.erl Mon Mar 31 03:30:27 2008
@@ -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]).
+

Added: incubator/couchdb/vendor/mochiweb/current/src/mochijson.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/src/mochijson.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/src/mochijson.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/src/mochijson.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,529 @@
+%% @author Bob Ippolito <bob@mochimedia.com>
+%% @copyright 2006 Mochi Media, Inc.
+
+%% @doc Yet another JSON (RFC 4627) library for Erlang.
+-module(mochijson).
+-author('bob@mochimedia.com').
+-export([encoder/1, encode/1]).
+-export([decoder/1, decode/1]).
+-export([binary_encoder/1, binary_encode/1]).
+-export([binary_decoder/1, binary_decode/1]).
+-export([test/0]).
+
+% This is a macro to placate syntax highlighters..
+-define(Q, $\").
+-define(ADV_COL(S, N), S#decoder{column=N+S#decoder.column}).
+-define(INC_COL(S), S#decoder{column=1+S#decoder.column}).
+-define(INC_LINE(S), S#decoder{column=1, line=1+S#decoder.line}).
+
+%% @type iolist() = [char() | binary() | iolist()]
+%% @type iodata() = iolist() | binary()
+%% @type json_string() = atom | string() | binary()
+%% @type json_number() = integer() | float()
+%% @type json_array() = {array, [json_term()]}
+%% @type json_object() = {struct, [{json_string(), json_term()}]}
+%% @type json_term() = json_string() | json_number() | json_array() |
+%%                     json_object()
+%% @type encoding() = utf8 | unicode
+%% @type encoder_option() = {input_encoding, encoding()} |
+%%                          {handler, function()}
+%% @type decoder_option() = {input_encoding, encoding()} |
+%%                          {object_hook, function()}
+%% @type bjson_string() = binary()
+%% @type bjson_number() = integer() | float()
+%% @type bjson_array() = [bjson_term()]
+%% @type bjson_object() = {struct, [{bjson_string(), bjson_term()}]}
+%% @type bjson_term() = bjson_string() | bjson_number() | bjson_array() |
+%%                      bjson_object()
+%% @type binary_encoder_option() = {handler, function()}
+%% @type binary_decoder_option() = {object_hook, function()}
+
+-record(encoder, {input_encoding=unicode,
+		  handler=null}).
+
+-record(decoder, {input_encoding=utf8,
+		  object_hook=null,
+		  line=1,
+		  column=1,
+		  state=null}).
+
+%% @spec encoder([encoder_option()]) -> function()
+%% @doc Create an encoder/1 with the given options.
+encoder(Options) ->
+    State = parse_encoder_options(Options, #encoder{}),
+    fun (O) -> json_encode(O, State) end.
+
+%% @spec encode(json_term()) -> iolist()
+%% @doc Encode the given as JSON to an iolist.
+encode(Any) ->
+    json_encode(Any, #encoder{}).
+
+%% @spec decoder([decoder_option()]) -> function()
+%% @doc Create a decoder/1 with the given options.
+decoder(Options) ->
+    State = parse_decoder_options(Options, #decoder{}),
+    fun (O) -> json_decode(O, State) end.
+
+%% @spec decode(iolist()) -> json_term()
+%% @doc Decode the given iolist to Erlang terms.
+decode(S) ->
+    json_decode(S, #decoder{}).
+
+%% @spec binary_decoder([binary_decoder_option()]) -> function()
+%% @doc Create a binary_decoder/1 with the given options.
+binary_decoder(Options) ->
+    mochijson2:decoder(Options).
+
+%% @spec binary_encoder([binary_encoder_option()]) -> function()
+%% @doc Create a binary_encoder/1 with the given options.
+binary_encoder(Options) ->
+    mochijson2:encoder(Options).
+
+%% @spec binary_encode(bjson_term()) -> iolist()
+%% @doc Encode the given as JSON to an iolist, using lists for arrays and
+%%      binaries for strings.
+binary_encode(Any) ->
+    mochijson2:encode(Any).
+
+%% @spec binary_decode(iolist()) -> bjson_term()
+%% @doc Decode the given iolist to Erlang terms, using lists for arrays and
+%%      binaries for strings.
+binary_decode(S) ->
+    mochijson2:decode(S).
+
+test() ->
+    test_all(),
+    mochijson2:test().
+
+%% Internal API
+
+parse_encoder_options([], State) ->
+    State;
+parse_encoder_options([{input_encoding, Encoding} | Rest], State) ->
+    parse_encoder_options(Rest, State#encoder{input_encoding=Encoding});
+parse_encoder_options([{handler, Handler} | Rest], State) ->
+    parse_encoder_options(Rest, State#encoder{handler=Handler}).
+
+parse_decoder_options([], State) ->
+    State;
+parse_decoder_options([{input_encoding, Encoding} | Rest], State) ->
+    parse_decoder_options(Rest, State#decoder{input_encoding=Encoding});
+parse_decoder_options([{object_hook, Hook} | Rest], State) ->
+    parse_decoder_options(Rest, State#decoder{object_hook=Hook}).
+
+json_encode(true, _State) ->
+    "true";
+json_encode(false, _State) ->
+    "false";
+json_encode(null, _State) ->
+    "null";
+json_encode(I, _State) when is_integer(I) ->
+    integer_to_list(I);
+json_encode(F, _State) when is_float(F) ->
+    mochinum:digits(F);
+json_encode(L, State) when is_list(L); is_binary(L); is_atom(L) ->
+    json_encode_string(L, State);
+json_encode({array, Props}, State) when is_list(Props) ->
+    json_encode_array(Props, State);
+json_encode({struct, Props}, State) when is_list(Props) ->
+    json_encode_proplist(Props, State);
+json_encode(Bad, #encoder{handler=null}) ->
+    exit({json_encode, {bad_term, Bad}});
+json_encode(Bad, State=#encoder{handler=Handler}) ->
+    json_encode(Handler(Bad), State).
+
+json_encode_array([], _State) ->
+    "[]";
+json_encode_array(L, State) ->
+    F = fun (O, Acc) ->
+		[$,, json_encode(O, State) | Acc]
+	end,
+    [$, | Acc1] = lists:foldl(F, "[", L),
+    lists:reverse([$\] | Acc1]).
+
+json_encode_proplist([], _State) ->
+    "{}";
+json_encode_proplist(Props, State) ->
+    F = fun ({K, V}, Acc) ->
+		KS = case K of 
+			 K when is_atom(K) ->
+			     json_encode_string_utf8(atom_to_list(K));
+			 K when is_integer(K) ->
+			     json_encode_string(integer_to_list(K), State);
+			 K when is_list(K); is_binary(K) ->
+			     json_encode_string(K, State)
+		     end,
+		VS = json_encode(V, State),
+		[$,, VS, $:, KS | Acc]
+	end,
+    [$, | Acc1] = lists:foldl(F, "{", Props),
+    lists:reverse([$\} | Acc1]).
+
+json_encode_string(A, _State) when is_atom(A) ->
+    json_encode_string_unicode(xmerl_ucs:from_utf8(atom_to_list(A)));
+json_encode_string(B, _State) when is_binary(B) ->
+    json_encode_string_unicode(xmerl_ucs:from_utf8(B));
+json_encode_string(S, #encoder{input_encoding=utf8}) ->
+    json_encode_string_utf8(S);
+json_encode_string(S, #encoder{input_encoding=unicode}) ->
+    json_encode_string_unicode(S).
+
+json_encode_string_utf8(S) ->
+    [?Q | json_encode_string_utf8_1(S)].
+
+json_encode_string_utf8_1([C | Cs]) when C >= 0, C =< 16#7f ->
+    NewC = case C of
+               $\\ -> "\\\\";
+               ?Q -> "\\\"";
+               _ when C >= $\s, C < 16#7f -> C;
+               $\t -> "\\t";
+               $\n -> "\\n";
+               $\r -> "\\r";
+               $\f -> "\\f";
+               $\b -> "\\b";
+               _ when C >= 0, C =< 16#7f -> unihex(C);
+               _ -> exit({json_encode, {bad_char, C}})
+           end,
+    [NewC | json_encode_string_utf8_1(Cs)];
+json_encode_string_utf8_1(All=[C | _]) when C >= 16#80, C =< 16#10FFFF ->
+    json_encode_string_unicode(xmerl_ucs:from_utf8(All));
+json_encode_string_utf8_1([]) ->
+    "\"".
+
+json_encode_string_unicode(S) ->
+    [?Q | json_encode_string_unicode_1(S)].
+
+json_encode_string_unicode_1([C | Cs]) ->
+    NewC = case C of
+               $\\ -> "\\\\";
+               ?Q -> "\\\"";
+               _ when C >= $\s, C < 16#7f -> C;
+               $\t -> "\\t";
+               $\n -> "\\n";
+               $\r -> "\\r";
+               $\f -> "\\f";
+               $\b -> "\\b";
+               _ when C >= 0, C =< 16#10FFFF -> unihex(C);
+               _ -> exit({json_encode, {bad_char, C}})
+           end,
+    [NewC | json_encode_string_unicode_1(Cs)];
+json_encode_string_unicode_1([]) ->
+    "\"".
+
+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.
+
+hexdigit(C) when C >= 0, C =< 9 ->
+    C + $0;
+hexdigit(C) when C =< 15 ->
+    C + $a - 10.
+
+unihex(C) when C < 16#10000 ->
+    <<D3:4, D2:4, D1:4, D0:4>> = <<C:16>>,
+    Digits = [hexdigit(D) || D <- [D3, D2, D1, D0]],
+    [$\\, $u | Digits];
+unihex(C) when C =< 16#10FFFF ->
+    N = C - 16#10000,
+    S1 = 16#d800 bor ((N bsr 10) band 16#3ff),
+    S2 = 16#dc00 bor (N band 16#3ff),
+    [unihex(S1), unihex(S2)].
+
+json_decode(B, S) when is_binary(B) ->
+    json_decode(binary_to_list(B), S);
+json_decode(L, S) ->
+    {Res, L1, S1} = decode1(L, S),
+    {eof, [], _} = tokenize(L1, S1#decoder{state=trim}),
+    Res.
+
+decode1(L, S=#decoder{state=null}) ->
+    case tokenize(L, S#decoder{state=any}) of
+	{{const, C}, L1, S1} ->
+	    {C, L1, S1};
+	{start_array, L1, S1} ->
+	    decode_array(L1, S1#decoder{state=any}, []);
+	{start_object, L1, S1} ->
+	    decode_object(L1, S1#decoder{state=key}, [])
+    end.
+
+make_object(V, #decoder{object_hook=null}) ->
+    V;
+make_object(V, #decoder{object_hook=Hook}) ->
+    Hook(V).
+
+decode_object(L, S=#decoder{state=key}, Acc) ->
+    case tokenize(L, S) of
+	{end_object, Rest, S1} ->
+	    V = make_object({struct, lists:reverse(Acc)}, S1),
+	    {V, Rest, S1#decoder{state=null}};
+	{{const, K}, Rest, S1} when is_list(K) ->
+	    {colon, L2, S2} = tokenize(Rest, S1),
+	    {V, L3, S3} = decode1(L2, S2#decoder{state=null}),
+	    decode_object(L3, S3#decoder{state=comma}, [{K, V} | Acc])
+    end;
+decode_object(L, S=#decoder{state=comma}, Acc) ->
+    case tokenize(L, S) of
+	{end_object, Rest, S1} ->
+	    V = make_object({struct, lists:reverse(Acc)}, S1),
+	    {V, Rest, S1#decoder{state=null}};
+	{comma, Rest, S1} ->
+	    decode_object(Rest, S1#decoder{state=key}, Acc)
+    end.
+
+decode_array(L, S=#decoder{state=any}, Acc) ->
+    case tokenize(L, S) of
+	{end_array, Rest, S1} ->
+	    {{array, lists:reverse(Acc)}, Rest, S1#decoder{state=null}};
+	{start_array, Rest, S1} ->
+	    {Array, Rest1, S2} = decode_array(Rest, S1#decoder{state=any}, []),
+	    decode_array(Rest1, S2#decoder{state=comma}, [Array | Acc]);
+	{start_object, Rest, S1} ->
+	    {Array, Rest1, S2} = decode_object(Rest, S1#decoder{state=key}, []),
+	    decode_array(Rest1, S2#decoder{state=comma}, [Array | Acc]);
+	{{const, Const}, Rest, S1} ->
+	    decode_array(Rest, S1#decoder{state=comma}, [Const | Acc])
+    end;
+decode_array(L, S=#decoder{state=comma}, Acc) ->
+    case tokenize(L, S) of
+	{end_array, Rest, S1} ->
+	    {{array, lists:reverse(Acc)}, Rest, S1#decoder{state=null}};
+	{comma, Rest, S1} ->
+	    decode_array(Rest, S1#decoder{state=any}, Acc)
+    end.
+
+tokenize_string(IoList=[C | _], S=#decoder{input_encoding=utf8}, Acc)
+  when is_list(C); is_binary(C); C >= 16#7f ->
+    List = xmerl_ucs:from_utf8(iolist_to_binary(IoList)),
+    tokenize_string(List, S#decoder{input_encoding=unicode}, Acc);
+tokenize_string("\"" ++ Rest, S, Acc) ->
+    {lists:reverse(Acc), Rest, ?INC_COL(S)};
+tokenize_string("\\\"" ++ Rest, S, Acc) ->
+    tokenize_string(Rest, ?ADV_COL(S, 2), [$\" | Acc]);
+tokenize_string("\\\\" ++ Rest, S, Acc) ->
+    tokenize_string(Rest, ?ADV_COL(S, 2), [$\\ | Acc]);
+tokenize_string("\\/" ++ Rest, S, Acc) ->
+    tokenize_string(Rest, ?ADV_COL(S, 2), [$/ | Acc]);
+tokenize_string("\\b" ++ Rest, S, Acc) ->
+    tokenize_string(Rest, ?ADV_COL(S, 2), [$\b | Acc]);
+tokenize_string("\\f" ++ Rest, S, Acc) ->
+    tokenize_string(Rest, ?ADV_COL(S, 2), [$\f | Acc]);
+tokenize_string("\\n" ++ Rest, S, Acc) ->
+    tokenize_string(Rest, ?ADV_COL(S, 2), [$\n | Acc]);
+tokenize_string("\\r" ++ Rest, S, Acc) ->
+    tokenize_string(Rest, ?ADV_COL(S, 2), [$\r | Acc]);
+tokenize_string("\\t" ++ Rest, S, Acc) ->
+    tokenize_string(Rest, ?ADV_COL(S, 2), [$\t | Acc]);
+tokenize_string([$\\, $u, C3, C2, C1, C0 | Rest], S, Acc) ->
+    % coalesce UTF-16 surrogate pair?
+    C = dehex(C0) bor
+	(dehex(C1) bsl 4) bor
+	(dehex(C2) bsl 8) bor 
+	(dehex(C3) bsl 12),
+    tokenize_string(Rest, ?ADV_COL(S, 6), [C | Acc]);
+tokenize_string([C | Rest], S, Acc) when C >= $\s; C < 16#10FFFF ->
+    tokenize_string(Rest, ?ADV_COL(S, 1), [C | Acc]).
+    
+tokenize_number(IoList=[C | _], Mode, S=#decoder{input_encoding=utf8}, Acc)
+  when is_list(C); is_binary(C); C >= 16#7f ->
+    List = xmerl_ucs:from_utf8(iolist_to_binary(IoList)),
+    tokenize_number(List, Mode, S#decoder{input_encoding=unicode}, Acc);
+tokenize_number([$- | Rest], sign, S, []) ->
+    tokenize_number(Rest, int, ?INC_COL(S), [$-]);
+tokenize_number(Rest, sign, S, []) ->
+    tokenize_number(Rest, int, S, []);
+tokenize_number([$0 | Rest], int, S, Acc) ->
+    tokenize_number(Rest, frac, ?INC_COL(S), [$0 | Acc]);
+tokenize_number([C | Rest], int, S, Acc) when C >= $1, C =< $9 ->
+    tokenize_number(Rest, int1, ?INC_COL(S), [C | Acc]);
+tokenize_number([C | Rest], int1, S, Acc) when C >= $0, C =< $9 ->
+    tokenize_number(Rest, int1, ?INC_COL(S), [C | Acc]);
+tokenize_number(Rest, int1, S, Acc) ->
+    tokenize_number(Rest, frac, S, Acc);
+tokenize_number([$., C | Rest], frac, S, Acc) when C >= $0, C =< $9 ->
+    tokenize_number(Rest, frac1, ?ADV_COL(S, 2), [C, $. | Acc]);
+tokenize_number([E | Rest], frac, S, Acc) when E == $e; E == $E ->
+    tokenize_number(Rest, esign, ?INC_COL(S), [$e, $0, $. | Acc]);
+tokenize_number(Rest, frac, S, Acc) ->
+    {{int, lists:reverse(Acc)}, Rest, S};
+tokenize_number([C | Rest], frac1, S, Acc) when C >= $0, C =< $9 ->
+    tokenize_number(Rest, frac1, ?INC_COL(S), [C | Acc]);
+tokenize_number([E | Rest], frac1, S, Acc) when E == $e; E == $E ->
+    tokenize_number(Rest, esign, ?INC_COL(S), [$e | Acc]);
+tokenize_number(Rest, frac1, S, Acc) ->
+    {{float, lists:reverse(Acc)}, Rest, S};
+tokenize_number([C | Rest], esign, S, Acc) when C == $-; C == $+ ->
+    tokenize_number(Rest, eint, ?INC_COL(S), [C | Acc]);
+tokenize_number(Rest, esign, S, Acc) ->
+    tokenize_number(Rest, eint, S, Acc);
+tokenize_number([C | Rest], eint, S, Acc) when C >= $0, C =< $9 ->
+    tokenize_number(Rest, eint1, ?INC_COL(S), [C | Acc]);
+tokenize_number([C | Rest], eint1, S, Acc) when C >= $0, C =< $9 ->
+    tokenize_number(Rest, eint1, ?INC_COL(S), [C | Acc]);
+tokenize_number(Rest, eint1, S, Acc) ->
+    {{float, lists:reverse(Acc)}, Rest, S}.
+
+tokenize([], S=#decoder{state=trim}) ->
+    {eof, [], S};
+tokenize([L | Rest], S) when is_list(L) ->
+    tokenize(L ++ Rest, S);
+tokenize([B | Rest], S) when is_binary(B) ->
+    tokenize(xmerl_ucs:from_utf8(B) ++ Rest, S);
+tokenize("\r\n" ++ Rest, S) ->
+    tokenize(Rest, ?INC_LINE(S));
+tokenize("\n" ++ Rest, S) ->
+    tokenize(Rest, ?INC_LINE(S));
+tokenize([C | Rest], S) when C == $\s; C == $\t ->
+    tokenize(Rest, ?INC_COL(S));
+tokenize("{" ++ Rest, S) ->
+    {start_object, Rest, ?INC_COL(S)};
+tokenize("}" ++ Rest, S) ->
+    {end_object, Rest, ?INC_COL(S)};
+tokenize("[" ++ Rest, S) ->
+    {start_array, Rest, ?INC_COL(S)};
+tokenize("]" ++ Rest, S) ->
+    {end_array, Rest, ?INC_COL(S)};
+tokenize("," ++ Rest, S) ->
+    {comma, Rest, ?INC_COL(S)};
+tokenize(":" ++ Rest, S) ->
+    {colon, Rest, ?INC_COL(S)};
+tokenize("null" ++ Rest, S) ->
+    {{const, null}, Rest, ?ADV_COL(S, 4)};
+tokenize("true" ++ Rest, S) ->
+    {{const, true}, Rest, ?ADV_COL(S, 4)};
+tokenize("false" ++ Rest, S) ->
+    {{const, false}, Rest, ?ADV_COL(S, 5)};
+tokenize("\"" ++ Rest, S) ->
+    {String, Rest1, S1} = tokenize_string(Rest, ?INC_COL(S), []),
+    {{const, String}, Rest1, S1};
+tokenize(L=[C | _], S) when C >= $0, C =< $9; C == $- ->
+    case tokenize_number(L, sign, S, []) of
+	{{int, Int}, Rest, S1} ->
+	    {{const, list_to_integer(Int)}, Rest, S1};
+	{{float, Float}, Rest, S1} ->
+	    {{const, list_to_float(Float)}, Rest, S1}
+    end.
+
+%% testing constructs borrowed from the Yaws JSON implementation.
+
+%% Create an object from a list of Key/Value pairs.
+
+obj_new() ->
+    {struct, []}.
+
+is_obj({struct, Props}) ->
+    F = fun ({K, _}) when is_list(K) ->
+		true;
+	    (_) ->
+		false
+	end,	
+    lists:all(F, Props).
+
+obj_from_list(Props) ->
+    Obj = {struct, Props},
+    case is_obj(Obj) of
+        true -> Obj;
+        false -> exit(json_bad_object)
+    end.
+
+%% Test for equivalence of Erlang terms.
+%% Due to arbitrary order of construction, equivalent objects might
+%% compare unequal as erlang terms, so we need to carefully recurse
+%% through aggregates (tuples and objects).
+
+equiv({struct, Props1}, {struct, Props2}) ->
+    equiv_object(Props1, Props2);
+equiv({array, L1}, {array, L2}) ->
+    equiv_list(L1, L2);
+equiv(N1, N2) when is_number(N1), is_number(N2)	-> N1 == N2;
+equiv(S1, S2) when is_list(S1), is_list(S2)	-> S1 == S2;
+equiv(true, true) -> true;
+equiv(false, false) -> true;
+equiv(null, null) -> true.
+
+%% Object representation and traversal order is unknown.
+%% Use the sledgehammer and sort property lists.
+
+equiv_object(Props1, Props2) ->
+    L1 = lists:keysort(1, Props1),
+    L2 = lists:keysort(1, Props2),
+    Pairs = lists:zip(L1, L2),
+    true = lists:all(fun({{K1, V1}, {K2, V2}}) ->
+	equiv(K1, K2) and equiv(V1, V2)
+    end, Pairs).
+
+%% Recursively compare tuple elements for equivalence.
+
+equiv_list([], []) ->
+    true;
+equiv_list([V1 | L1], [V2 | L2]) ->
+    case equiv(V1, V2) of
+	true ->
+	    equiv_list(L1, L2);
+	false ->
+	    false
+    end.
+
+test_all() ->
+    test_one(e2j_test_vec(utf8), 1).
+
+test_one([], _N) ->
+    %% io:format("~p tests passed~n", [N-1]),
+    ok;
+test_one([{E, J} | Rest], N) ->
+    %% io:format("[~p] ~p ~p~n", [N, E, J]),
+    true = equiv(E, decode(J)),
+    true = equiv(E, decode(encode(E))),
+    test_one(Rest, 1+N).
+
+e2j_test_vec(unicode) -> 
+    [
+     {"foo" ++ [500] ++ "bar", [$", $f, $o, $o, 500, $b, $a, $r, $"]}
+    ];
+e2j_test_vec(utf8) ->
+    [
+    {1, "1"},
+    {3.1416, "3.14160"}, % text representation may truncate, trail zeroes
+    {-1, "-1"},
+    {-3.1416, "-3.14160"},
+    {12.0e10, "1.20000e+11"},
+    {1.234E+10, "1.23400e+10"},
+    {-1.234E-10, "-1.23400e-10"},
+    {10.0, "1.0e+01"},
+    {123.456, "1.23456E+2"},
+    {10.0, "1e1"},
+    {"foo", "\"foo\""},
+    {"foo" ++ [5] ++ "bar", "\"foo\\u0005bar\""},
+    {"", "\"\""},
+    {"\"", "\"\\\"\""},
+    {"\n\n\n", "\"\\n\\n\\n\""},
+    {"\\", "\"\\\\\""},
+    {"\" \b\f\r\n\t\"", "\"\\\" \\b\\f\\r\\n\\t\\\"\""},
+    {obj_new(), "{}"},
+    {obj_from_list([{"foo", "bar"}]), "{\"foo\":\"bar\"}"},
+    {obj_from_list([{"foo", "bar"}, {"baz", 123}]),
+     "{\"foo\":\"bar\",\"baz\":123}"},
+    {{array, []}, "[]"},
+    {{array, [{array, []}]}, "[[]]"},
+    {{array, [1, "foo"]}, "[1,\"foo\"]"},
+
+    % json array in a json object
+    {obj_from_list([{"foo", {array, [123]}}]),
+     "{\"foo\":[123]}"},
+
+    % json object in a json object
+    {obj_from_list([{"foo", obj_from_list([{"bar", true}])}]),
+     "{\"foo\":{\"bar\":true}}"},
+
+    % fold evaluation order
+    {obj_from_list([{"foo", {array, []}},
+                     {"bar", obj_from_list([{"baz", true}])},
+                     {"alice", "bob"}]),
+     "{\"foo\":[],\"bar\":{\"baz\":true},\"alice\":\"bob\"}"},
+
+    % json object in a json array
+    {{array, [-123, "foo", obj_from_list([{"bar", {array, []}}]), null]},
+     "[-123,\"foo\",{\"bar\":[]},null]"}
+    ].

Added: incubator/couchdb/vendor/mochiweb/current/src/mochijson2.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/src/mochijson2.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/src/mochijson2.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/src/mochijson2.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,509 @@
+%% @author Bob Ippolito <bob@mochimedia.com>
+%% @copyright 2007 Mochi Media, Inc.
+
+%% @doc Yet another JSON (RFC 4627) library for Erlang. mochijson2 works
+%%      with binaries as strings, arrays as lists (without an {array, _})
+%%      wrapper and it only knows how to decode UTF-8 (and ASCII).
+
+-module(mochijson2).
+-author('bob@mochimedia.com').
+-export([encoder/1, encode/1]).
+-export([decoder/1, decode/1]).
+-export([test/0]).
+
+% This is a macro to placate syntax highlighters..
+-define(Q, $\").
+-define(ADV_COL(S, N), S#decoder{offset=N+S#decoder.offset,
+                                 column=N+S#decoder.column}).
+-define(INC_COL(S), S#decoder{offset=1+S#decoder.offset,
+                              column=1+S#decoder.column}).
+-define(INC_LINE(S), S#decoder{offset=1+S#decoder.offset,
+                               column=1,
+                               line=1+S#decoder.line}).
+-define(INC_CHAR(S, C),
+        case C of
+            $\n ->
+                S#decoder{column=1,
+                          line=1+S#decoder.line,
+                          offset=1+S#decoder.offset};
+            _ ->
+                S#decoder{column=1+S#decoder.column,
+                          offset=1+S#decoder.offset}
+        end).
+-define(IS_WHITESPACE(C),
+ 	(C =:= $\s orelse C =:= $\t orelse C =:= $\r orelse C =:= $\n)).
+
+%% @type iolist() = [char() | binary() | iolist()]
+%% @type iodata() = iolist() | binary()
+%% @type json_string() = atom | binary()
+%% @type json_number() = integer() | float()
+%% @type json_array() = [json_term()]
+%% @type json_object() = {struct, [{json_string(), json_term()}]}
+%% @type json_term() = json_string() | json_number() | json_array() |
+%%                     json_object()
+
+-record(encoder, {handler=null}).
+
+-record(decoder, {object_hook=null,
+		  offset=0,
+                  line=1,
+		  column=1,
+		  state=null}).
+
+%% @spec encoder([encoder_option()]) -> function()
+%% @doc Create an encoder/1 with the given options.
+encoder(Options) ->
+    State = parse_encoder_options(Options, #encoder{}),
+    fun (O) -> json_encode(O, State) end.
+
+%% @spec encode(json_term()) -> iolist()
+%% @doc Encode the given as JSON to an iolist.
+encode(Any) ->
+    json_encode(Any, #encoder{}).
+
+%% @spec decoder([decoder_option()]) -> function()
+%% @doc Create a decoder/1 with the given options.
+decoder(Options) ->
+    State = parse_decoder_options(Options, #decoder{}),
+    fun (O) -> json_decode(O, State) end.
+
+%% @spec decode(iolist()) -> json_term()
+%% @doc Decode the given iolist to Erlang terms.
+decode(S) ->
+    json_decode(S, #decoder{}).
+
+test() ->
+    test_all().
+
+%% Internal API
+
+parse_encoder_options([], State) ->
+    State;
+parse_encoder_options([{handler, Handler} | Rest], State) ->
+    parse_encoder_options(Rest, State#encoder{handler=Handler}).
+
+parse_decoder_options([], State) ->
+    State;
+parse_decoder_options([{object_hook, Hook} | Rest], State) ->
+    parse_decoder_options(Rest, State#decoder{object_hook=Hook}).
+
+json_encode(true, _State) ->
+    <<"true">>;
+json_encode(false, _State) ->
+    <<"false">>;
+json_encode(null, _State) ->
+    <<"null">>;
+json_encode(I, _State) when is_integer(I) andalso I >= -2147483648 andalso I =< 2147483647 ->
+    %% Anything outside of 32-bit integers should be encoded as a float
+    integer_to_list(I);
+json_encode(I, _State) when is_integer(I) ->
+    mochinum:digits(float(I));
+json_encode(F, _State) when is_float(F) ->
+    mochinum:digits(F);
+json_encode(S, State) when is_binary(S); is_atom(S) ->
+    json_encode_string(S, State);
+json_encode(Array, State) when is_list(Array) ->
+    json_encode_array(Array, State);
+json_encode({struct, Props}, State) when is_list(Props) ->
+    json_encode_proplist(Props, State);
+json_encode(Bad, #encoder{handler=null}) ->
+    exit({json_encode, {bad_term, Bad}});
+json_encode(Bad, State=#encoder{handler=Handler}) ->
+    json_encode(Handler(Bad), State).
+
+json_encode_array([], _State) ->
+    <<"[]">>;
+json_encode_array(L, State) ->
+    F = fun (O, Acc) ->
+		[$,, json_encode(O, State) | Acc]
+	end,
+    [$, | Acc1] = lists:foldl(F, "[", L),
+    lists:reverse([$\] | Acc1]).
+
+json_encode_proplist([], _State) ->
+    <<"{}">>;
+json_encode_proplist(Props, State) ->
+    F = fun ({K, V}, Acc) ->
+		KS = json_encode_string(K, State),
+		VS = json_encode(V, State),
+		[$,, VS, $:, KS | Acc]
+	end,
+    [$, | Acc1] = lists:foldl(F, "{", Props),
+    lists:reverse([$\} | Acc1]).
+
+json_encode_string(A, _State) when is_atom(A) ->
+    json_encode_string_unicode(xmerl_ucs:from_utf8(atom_to_list(A)), [?Q]);
+json_encode_string(B, _State) when is_binary(B) ->
+    json_encode_string_unicode(xmerl_ucs:from_utf8(B), [?Q]);
+json_encode_string(I, _State) when is_integer(I) ->
+    json_encode_string_unicode(integer_to_list(I), [?Q]);
+json_encode_string(L, _State) when is_list(L) ->
+    json_encode_string_unicode(L, [?Q]).
+
+json_encode_string_unicode([], Acc) ->
+    lists:reverse([$\" | Acc]);
+json_encode_string_unicode([C | Cs], Acc) ->
+    Acc1 = case C of
+	       ?Q ->
+		   [?Q, $\\ | Acc];
+               %% Escaping solidus is only useful when trying to protect
+               %% against "</script>" injection attacks which are only
+               %% possible when JSON is inserted into a HTML document
+               %% in-line. mochijson2 does not protect you from this, so
+               %% if you do insert directly into HTML then you need to
+               %% uncomment the following case or escape the output of encode.
+               %%
+	       %% $/ ->
+	       %%    [$/, $\\ | Acc];
+               %%
+	       $\\ ->
+		   [$\\, $\\ | Acc];
+	       $\b ->
+		   [$b, $\\ | Acc];
+	       $\f ->
+		   [$f, $\\ | Acc];
+	       $\n ->
+		   [$n, $\\ | Acc];
+	       $\r ->
+		   [$r, $\\ | Acc];
+	       $\t ->
+		   [$t, $\\ | Acc];
+	       C when C >= 0, C < $\s; C >= 16#7f, C =< 16#10FFFF ->
+		   [unihex(C) | Acc];
+	       C when C < 16#7f ->
+		   [C | Acc];
+	       _ ->
+		   exit({json_encode, {bad_char, C}})
+	   end,
+    json_encode_string_unicode(Cs, Acc1).
+
+hexdigit(C) when C >= 0, C =< 9 ->
+    C + $0;
+hexdigit(C) when C =< 15 ->
+    C + $a - 10.
+
+unihex(C) when C < 16#10000 ->
+    <<D3:4, D2:4, D1:4, D0:4>> = <<C:16>>,
+    Digits = [hexdigit(D) || D <- [D3, D2, D1, D0]],
+    [$\\, $u | Digits];
+unihex(C) when C =< 16#10FFFF ->
+    N = C - 16#10000,
+    S1 = 16#d800 bor ((N bsr 10) band 16#3ff),
+    S2 = 16#dc00 bor (N band 16#3ff),
+    [unihex(S1), unihex(S2)].
+
+json_decode(L, S) when is_list(L) ->
+    json_decode(iolist_to_binary(L), S);
+json_decode(B, S) ->
+    {Res, S1} = decode1(B, S),
+    {eof, _} = tokenize(B, S1#decoder{state=trim}),
+    Res.
+
+decode1(B, S=#decoder{state=null}) ->
+    case tokenize(B, S#decoder{state=any}) of
+	{{const, C}, S1} ->
+	    {C, S1};
+	{start_array, S1} ->
+	    decode_array(B, S1);
+	{start_object, S1} ->
+	    decode_object(B, S1)
+    end.
+
+make_object(V, #decoder{object_hook=null}) ->
+    V;
+make_object(V, #decoder{object_hook=Hook}) ->
+    Hook(V).
+
+decode_object(B, S) ->
+    decode_object(B, S#decoder{state=key}, []).
+
+decode_object(B, S=#decoder{state=key}, Acc) ->
+    case tokenize(B, S) of
+	{end_object, S1} ->
+	    V = make_object({struct, lists:reverse(Acc)}, S1),
+	    {V, S1#decoder{state=null}};
+	{{const, K}, S1} ->
+	    {colon, S2} = tokenize(B, S1),
+	    {V, S3} = decode1(B, S2#decoder{state=null}),
+	    decode_object(B, S3#decoder{state=comma}, [{K, V} | Acc])
+    end;
+decode_object(B, S=#decoder{state=comma}, Acc) ->
+    case tokenize(B, S) of
+	{end_object, S1} ->
+	    V = make_object({struct, lists:reverse(Acc)}, S1),
+	    {V, S1#decoder{state=null}};
+	{comma, S1} ->
+	    decode_object(B, S1#decoder{state=key}, Acc)
+    end.
+
+decode_array(B, S) ->
+    decode_array(B, S#decoder{state=any}, []).
+
+decode_array(B, S=#decoder{state=any}, Acc) ->
+    case tokenize(B, S) of
+	{end_array, S1} ->
+	    {lists:reverse(Acc), S1#decoder{state=null}};
+	{start_array, S1} ->
+	    {Array, S2} = decode_array(B, S1),
+	    decode_array(B, S2#decoder{state=comma}, [Array | Acc]);
+	{start_object, S1} ->
+	    {Array, S2} = decode_object(B, S1),
+	    decode_array(B, S2#decoder{state=comma}, [Array | Acc]);
+	{{const, Const}, S1} ->
+	    decode_array(B, S1#decoder{state=comma}, [Const | Acc])
+    end;
+decode_array(B, S=#decoder{state=comma}, Acc) ->
+    case tokenize(B, S) of
+	{end_array, S1} ->
+	    {lists:reverse(Acc), S1#decoder{state=null}};
+	{comma, S1} ->
+	    decode_array(B, S1#decoder{state=any}, Acc)
+    end.
+
+tokenize_string(B, S) ->
+    tokenize_string(B, S, []).
+
+tokenize_string(B, S=#decoder{offset=O}, Acc) ->
+    case B of
+        <<_:O/binary, ?Q, _/binary>> ->
+            {{const, iolist_to_binary(lists:reverse(Acc))}, ?INC_COL(S)};
+        <<_:O/binary, "\\\"", _/binary>> ->
+            tokenize_string(B, ?ADV_COL(S, 2), [$\" | Acc]);
+        <<_:O/binary, "\\\\", _/binary>> ->
+            tokenize_string(B, ?ADV_COL(S, 2), [$\\ | Acc]);
+        <<_:O/binary, "\\/", _/binary>> ->
+            tokenize_string(B, ?ADV_COL(S, 2), [$/ | Acc]);
+        <<_:O/binary, "\\b", _/binary>> ->
+            tokenize_string(B, ?ADV_COL(S, 2), [$\b | Acc]);
+        <<_:O/binary, "\\f", _/binary>> ->
+            tokenize_string(B, ?ADV_COL(S, 2), [$\f | Acc]);
+        <<_:O/binary, "\\n", _/binary>> ->
+            tokenize_string(B, ?ADV_COL(S, 2), [$\n | Acc]);
+        <<_:O/binary, "\\r", _/binary>> ->
+            tokenize_string(B, ?ADV_COL(S, 2), [$\r | Acc]);
+        <<_:O/binary, "\\t", _/binary>> ->
+            tokenize_string(B, ?ADV_COL(S, 2), [$\t | Acc]);
+        <<_:O/binary, "\\u", C3, C2, C1, C0, _/binary>> ->
+            %% coalesce UTF-16 surrogate pair?
+            C = erlang:list_to_integer([C3, C2, C1, C0], 16),
+            Acc1 = lists:reverse(xmerl_ucs:to_utf8(C), Acc),
+            tokenize_string(B, ?ADV_COL(S, 6), Acc1);
+        <<_:O/binary, C, _/binary>> ->
+            tokenize_string(B, ?INC_CHAR(S, C), [C | Acc])
+    end.
+
+tokenize_number(B, S) ->
+    case tokenize_number(B, sign, S, []) of
+        {{int, Int}, S1} ->
+            {{const, list_to_integer(Int)}, S1};
+        {{float, Float}, S1} ->
+            {{const, list_to_float(Float)}, S1}
+    end.
+
+tokenize_number(B, sign, S=#decoder{offset=O}, []) ->
+    case B of
+        <<_:O/binary, $-, _/binary>> ->
+            tokenize_number(B, int, ?INC_COL(S), [$-]);
+        _ ->
+            tokenize_number(B, int, S, [])
+    end;
+tokenize_number(B, int, S=#decoder{offset=O}, Acc) ->
+    case B of
+        <<_:O/binary, $0, _/binary>> ->
+            tokenize_number(B, frac, ?INC_COL(S), [$0 | Acc]);
+        <<_:O/binary, C, _/binary>> when C >= $1 andalso C =< $9 ->
+            tokenize_number(B, int1, ?INC_COL(S), [C | Acc])
+    end;
+tokenize_number(B, int1, S=#decoder{offset=O}, Acc) ->
+    case B of
+        <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
+            tokenize_number(B, int1, ?INC_COL(S), [C | Acc]);
+        _ ->
+            tokenize_number(B, frac, S, Acc)
+    end;
+tokenize_number(B, frac, S=#decoder{offset=O}, Acc) ->
+    case B of
+        <<_:O/binary, $., C, _/binary>> when C >= $0, C =< $9 ->
+            tokenize_number(B, frac1, ?ADV_COL(S, 2), [C, $. | Acc]);
+        <<_:O/binary, E, _/binary>> when E =:= $e orelse E =:= $E ->
+            tokenize_number(B, esign, ?INC_COL(S), [$e, $0, $. | Acc]);
+        _ ->
+            {{int, lists:reverse(Acc)}, S}
+    end;
+tokenize_number(B, frac1, S=#decoder{offset=O}, Acc) ->
+    case B of
+        <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
+            tokenize_number(B, frac1, ?INC_COL(S), [C | Acc]);
+        <<_:O/binary, E, _/binary>> when E =:= $e orelse E =:= $E ->
+            tokenize_number(B, esign, ?INC_COL(S), [$e | Acc]);
+        _ ->
+            {{float, lists:reverse(Acc)}, S}
+    end;
+tokenize_number(B, esign, S=#decoder{offset=O}, Acc) ->
+    case B of
+        <<_:O/binary, C, _/binary>> when C =:= $- orelse C=:= $+ ->
+            tokenize_number(B, eint, ?INC_COL(S), [C | Acc]);
+        _ ->
+            tokenize_number(B, eint, S, Acc)
+    end;
+tokenize_number(B, eint, S=#decoder{offset=O}, Acc) ->
+    case B of
+        <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
+            tokenize_number(B, eint1, ?INC_COL(S), [C | Acc])
+    end;
+tokenize_number(B, eint1, S=#decoder{offset=O}, Acc) ->
+    case B of
+        <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
+            tokenize_number(B, eint1, ?INC_COL(S), [C | Acc]);
+        _ ->
+            {{float, lists:reverse(Acc)}, S}
+    end.
+
+tokenize(B, S=#decoder{offset=O}) ->
+    case B of
+        <<_:O/binary, C, _/binary>> when ?IS_WHITESPACE(C) ->
+            tokenize(B, ?INC_CHAR(S, C));
+        <<_:O/binary, "{", _/binary>> ->
+            {start_object, ?INC_COL(S)};
+        <<_:O/binary, "}", _/binary>> ->
+            {end_object, ?INC_COL(S)};
+        <<_:O/binary, "[", _/binary>> ->
+            {start_array, ?INC_COL(S)};
+        <<_:O/binary, "]", _/binary>> ->
+            {end_array, ?INC_COL(S)};
+        <<_:O/binary, ",", _/binary>> ->
+            {comma, ?INC_COL(S)};
+        <<_:O/binary, ":", _/binary>> ->
+            {colon, ?INC_COL(S)};
+        <<_:O/binary, "null", _/binary>> ->
+            {{const, null}, ?ADV_COL(S, 4)};
+        <<_:O/binary, "true", _/binary>> ->
+            {{const, true}, ?ADV_COL(S, 4)};
+        <<_:O/binary, "false", _/binary>> ->
+            {{const, false}, ?ADV_COL(S, 5)};
+        <<_:O/binary, "\"", _/binary>> ->
+            tokenize_string(B, ?INC_COL(S));
+        <<_:O/binary, C, _/binary>> when (C >= $0 andalso C =< $9)
+                                         orelse C =:= $- ->
+            tokenize_number(B, S);
+        <<_:O/binary>> ->
+            trim = S#decoder.state,
+            {eof, S}
+    end.
+
+%% testing constructs borrowed from the Yaws JSON implementation.
+
+%% Create an object from a list of Key/Value pairs.
+
+obj_new() ->
+    {struct, []}.
+
+is_obj({struct, Props}) ->
+    F = fun ({K, _}) when is_binary(K) ->
+		true;
+	    (_) ->
+		false
+	end,	
+    lists:all(F, Props).
+
+obj_from_list(Props) ->
+    Obj = {struct, Props},
+    case is_obj(Obj) of
+        true -> Obj;
+        false -> exit({json_bad_object, Obj})
+    end.
+
+%% Test for equivalence of Erlang terms.
+%% Due to arbitrary order of construction, equivalent objects might
+%% compare unequal as erlang terms, so we need to carefully recurse
+%% through aggregates (tuples and objects).
+
+equiv({struct, Props1}, {struct, Props2}) ->
+    equiv_object(Props1, Props2);
+equiv(L1, L2) when is_list(L1), is_list(L2) ->
+    equiv_list(L1, L2);
+equiv(N1, N2) when is_number(N1), is_number(N2)	-> N1 == N2;
+equiv(B1, B2) when is_binary(B1), is_binary(B2)	-> B1 == B2;
+equiv(true, true) -> true;
+equiv(false, false) -> true;
+equiv(null, null) -> true.
+
+%% Object representation and traversal order is unknown.
+%% Use the sledgehammer and sort property lists.
+
+equiv_object(Props1, Props2) ->
+    L1 = lists:keysort(1, Props1),
+    L2 = lists:keysort(1, Props2),
+    Pairs = lists:zip(L1, L2),
+    true = lists:all(fun({{K1, V1}, {K2, V2}}) ->
+                             equiv(K1, K2) and equiv(V1, V2)
+                     end, Pairs).
+
+%% Recursively compare tuple elements for equivalence.
+
+equiv_list([], []) ->
+    true;
+equiv_list([V1 | L1], [V2 | L2]) ->
+    case equiv(V1, V2) of
+	true ->
+	    equiv_list(L1, L2);
+	false ->
+	    false
+    end.
+
+test_all() ->
+    [1199344435545.0, 1] = decode(<<"[1199344435545.0,1]">>),
+    test_one(e2j_test_vec(utf8), 1).
+
+test_one([], _N) ->
+    %% io:format("~p tests passed~n", [N-1]),
+    ok;
+test_one([{E, J} | Rest], N) ->
+    %% io:format("[~p] ~p ~p~n", [N, E, J]),
+    true = equiv(E, decode(J)),
+    true = equiv(E, decode(encode(E))),
+    test_one(Rest, 1+N).
+
+e2j_test_vec(utf8) ->
+    [
+     {1, "1"},
+     {3.1416, "3.14160"}, %% text representation may truncate, trail zeroes
+     {-1, "-1"},
+     {-3.1416, "-3.14160"},
+     {12.0e10, "1.20000e+11"},
+     {1.234E+10, "1.23400e+10"},
+     {-1.234E-10, "-1.23400e-10"},
+     {10.0, "1.0e+01"},
+     {123.456, "1.23456E+2"},
+     {10.0, "1e1"},
+     {<<"foo">>, "\"foo\""},
+     {<<"foo", 5, "bar">>, "\"foo\\u0005bar\""},
+     {<<"">>, "\"\""},
+     {<<"\n\n\n">>, "\"\\n\\n\\n\""},
+     {<<"\" \b\f\r\n\t\"">>, "\"\\\" \\b\\f\\r\\n\\t\\\"\""},
+     {obj_new(), "{}"},
+     {obj_from_list([{<<"foo">>, <<"bar">>}]), "{\"foo\":\"bar\"}"},
+     {obj_from_list([{<<"foo">>, <<"bar">>}, {<<"baz">>, 123}]),
+      "{\"foo\":\"bar\",\"baz\":123}"},
+     {[], "[]"},
+     {[[]], "[[]]"},
+     {[1, <<"foo">>], "[1,\"foo\"]"},
+     
+     %% json array in a json object
+     {obj_from_list([{<<"foo">>, [123]}]),
+      "{\"foo\":[123]}"},
+     
+     %% json object in a json object
+     {obj_from_list([{<<"foo">>, obj_from_list([{<<"bar">>, true}])}]),
+      "{\"foo\":{\"bar\":true}}"},
+     
+     %% fold evaluation order
+     {obj_from_list([{<<"foo">>, []},
+                     {<<"bar">>, obj_from_list([{<<"baz">>, true}])},
+                     {<<"alice">>, <<"bob">>}]),
+      "{\"foo\":[],\"bar\":{\"baz\":true},\"alice\":\"bob\"}"},
+     
+     %% json object in a json array
+     {[-123, <<"foo">>, obj_from_list([{<<"bar">>, []}]), null],
+      "[-123,\"foo\",{\"bar\":[]},null]"}
+    ].

Added: incubator/couchdb/vendor/mochiweb/current/src/mochinum.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/src/mochinum.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/src/mochinum.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/src/mochinum.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,289 @@
+%% @copyright 2007 Mochi Media, Inc.
+%% @author Bob Ippolito <bob@mochimedia.com>
+
+%% @doc Useful numeric algorithms for floats that cover some deficiencies
+%% in the math module. More interesting is digits/1, which implements
+%% the algorithm from:
+%% http://www.cs.indiana.edu/~burger/fp/index.html
+%% See also "Printing Floating-Point Numbers Quickly and Accurately"
+%% in Proceedings of the SIGPLAN '96 Conference on Programming Language
+%% Design and Implementation.
+
+-module(mochinum).
+-author("Bob Ippolito <bob@mochimedia.com>").
+-export([digits/1, frexp/1, int_pow/2, int_ceil/1, test/0]).
+
+%% IEEE 754 Float exponent bias
+-define(FLOAT_BIAS, 1022).
+-define(MIN_EXP, -1074).
+-define(BIG_POW, 4503599627370496).
+
+%% External API
+
+%% @spec digits(number()) -> string()
+%% @doc  Returns a string that accurately represents the given integer or float
+%%       using a conservative amount of digits. Great for generating
+%%       human-readable output, or compact ASCII serializations for floats.
+digits(N) when is_integer(N) ->
+    integer_to_list(N);
+digits(0.0) ->
+    "0.0";
+digits(Float) ->
+    {Frac, Exp} = frexp(Float),
+    Exp1 = Exp - 53,
+    Frac1 = trunc(abs(Frac) * (1 bsl 53)),
+    [Place | Digits] = digits1(Float, Exp1, Frac1),
+    R = insert_decimal(Place, [$0 + D || D <- Digits]),
+    case Float < 0 of
+        true ->
+            [$- | R];
+        _ ->
+            R
+    end.
+    
+%% @spec frexp(F::float()) -> {Frac::float(), Exp::float()}
+%% @doc  Return the fractional and exponent part of an IEEE 754 double,
+%%       equivalent to the libc function of the same name.
+%%       F = Frac * pow(2, Exp).
+frexp(F) ->
+    frexp1(unpack(F)).
+
+%% @spec int_pow(X::integer(), N::integer()) -> Y::integer()
+%% @doc  Moderately efficient way to exponentiate integers.
+%%       int_pow(10, 2) = 100.
+int_pow(_X, 0) ->
+    1;
+int_pow(X, N) when N > 0 ->
+    int_pow(X, N, 1).
+
+%% @spec int_ceil(F::float()) -> integer()
+%% @doc  Return the ceiling of F as an integer. The ceiling is defined as
+%%       F when F == trunc(F);
+%%       trunc(F) when F &lt; 0;
+%%       trunc(F) + 1 when F &gt; 0.
+int_ceil(X) ->
+    T = trunc(X),
+    case (X - T) of
+        Neg when Neg < 0 -> T;
+        Pos when Pos > 0 -> T + 1;
+        _ -> T
+    end.
+
+
+%% Internal API
+
+int_pow(X, N, R) when N < 2 ->
+    R * X;
+int_pow(X, N, R) ->
+    int_pow(X * X, N bsr 1, case N band 1 of 1 -> R * X; 0 -> R end).
+
+insert_decimal(0, S) ->
+    "0." ++ S;
+insert_decimal(Place, S) when Place > 0 ->
+    L = length(S),
+    case Place - L of
+         0 ->
+            S ++ ".0";
+        N when N < 0 ->
+            {S0, S1} = lists:split(L + N, S),
+            S0 ++ "." ++ S1;
+        N when N < 6 ->
+            %% More places than digits
+            S ++ lists:duplicate(N, $0) ++ ".0";
+        _ ->
+            insert_decimal_exp(Place, S)
+    end;
+insert_decimal(Place, S) when Place > -6 ->
+    "0." ++ lists:duplicate(abs(Place), $0) ++ S;
+insert_decimal(Place, S) ->
+    insert_decimal_exp(Place, S).
+
+insert_decimal_exp(Place, S) ->
+    [C | S0] = S,
+    S1 = case S0 of
+             [] ->
+                 "0";
+             _ ->
+                 S0
+         end,
+    Exp = case Place < 0 of
+              true ->
+                  "e-";
+              false ->
+                  "e+"
+          end,
+    [C] ++ "." ++ S1 ++ Exp ++ integer_to_list(abs(Place - 1)).
+
+
+digits1(Float, Exp, Frac) ->
+    Round = ((Frac band 1) =:= 0),
+    case Exp >= 0 of
+        true ->
+            BExp = 1 bsl Exp,
+            case (Frac /= ?BIG_POW) of
+                true ->
+                    scale((Frac * BExp * 2), 2, BExp, BExp,
+                          Round, Round, Float);
+                false ->
+                    scale((Frac * BExp * 4), 4, (BExp * 2), BExp,
+                          Round, Round, Float)
+            end;
+        false ->
+            case (Exp == ?MIN_EXP) orelse (Frac /= ?BIG_POW) of
+                true ->
+                    scale((Frac * 2), 1 bsl (1 - Exp), 1, 1,
+                          Round, Round, Float);
+                false ->
+                    scale((Frac * 4), 1 bsl (2 - Exp), 2, 1,
+                          Round, Round, Float)
+            end
+    end.
+
+scale(R, S, MPlus, MMinus, LowOk, HighOk, Float) ->
+    Est = int_ceil(math:log10(abs(Float)) - 1.0e-10),
+    %% Note that the scheme implementation uses a 326 element look-up table
+    %% for int_pow(10, N) where we do not.
+    case Est >= 0 of
+        true ->
+            fixup(R, S * int_pow(10, Est), MPlus, MMinus, Est,
+                  LowOk, HighOk);
+        false ->
+            Scale = int_pow(10, -Est),
+            fixup(R * Scale, S, MPlus * Scale, MMinus * Scale, Est,
+                  LowOk, HighOk)
+    end.
+
+fixup(R, S, MPlus, MMinus, K, LowOk, HighOk) ->
+    TooLow = case HighOk of
+                 true ->
+                     (R + MPlus) >= S;
+                 false ->
+                     (R + MPlus) > S
+             end,
+    case TooLow of
+        true ->
+            [(K + 1) | generate(R, S, MPlus, MMinus, LowOk, HighOk)];
+        false ->
+            [K | generate(R * 10, S, MPlus * 10, MMinus * 10, LowOk, HighOk)]
+    end.
+
+generate(R0, S, MPlus, MMinus, LowOk, HighOk) ->
+    D = R0 div S,
+    R = R0 rem S,
+    TC1 = case LowOk of
+              true ->
+                  R =< MMinus;
+              false ->
+                  R < MMinus
+          end,
+    TC2 = case HighOk of
+              true ->
+                  (R + MPlus) >= S;
+              false ->
+                  (R + MPlus) > S
+          end,
+    case TC1 of
+        false ->
+            case TC2 of
+                false ->
+                    [D | generate(R * 10, S, MPlus * 10, MMinus * 10,
+                                  LowOk, HighOk)];
+                true ->
+                    [D + 1]
+            end;
+        true ->
+            case TC2 of
+                false ->
+                    [D];
+                true ->
+                    case R * 2 < S of
+                        true ->
+                            [D];
+                        false ->
+                            [D + 1]
+                    end
+            end
+    end.
+
+unpack(Float) ->    
+    <<Sign:1, Exp:11, Frac:52>> = <<Float:64/float>>,
+    {Sign, Exp, Frac}.
+
+frexp1({_Sign, 0, 0}) ->
+    {0.0, 0};
+frexp1({Sign, 0, Frac}) ->
+    Exp = log2floor(Frac),
+    <<Frac1:64/float>> = <<Sign:1, ?FLOAT_BIAS:11, (Frac-1):52>>,
+    {Frac1, -(?FLOAT_BIAS) - 52 + Exp};
+frexp1({Sign, Exp, Frac}) ->
+    <<Frac1:64/float>> = <<Sign:1, ?FLOAT_BIAS:11, Frac:52>>,
+    {Frac1, Exp - ?FLOAT_BIAS}.
+
+log2floor(Int) ->
+    log2floor(Int, 0).
+
+log2floor(0, N) ->
+    N;
+log2floor(Int, N) ->
+    log2floor(Int bsr 1, 1 + N).
+
+
+test() ->
+    ok = test_frexp(),
+    ok = test_int_ceil(),
+    ok = test_int_pow(),
+    ok = test_digits(),
+    ok.
+
+test_int_ceil() ->
+    1 = int_ceil(0.0001),
+    0 = int_ceil(0.0),
+    1 = int_ceil(0.99),
+    1 = int_ceil(1.0),
+    -1 = int_ceil(-1.5),
+    -2 = int_ceil(-2.0),
+    ok.
+    
+test_int_pow() ->
+    1 = int_pow(1, 1),
+    1 = int_pow(1, 0),
+    1 = int_pow(10, 0),
+    10 = int_pow(10, 1),
+    100 = int_pow(10, 2),
+    1000 = int_pow(10, 3),
+    ok.
+    
+test_digits() ->
+    "0" = digits(0),
+    "0.0" = digits(0.0),
+    "1.0" = digits(1.0),
+    "-1.0" = digits(-1.0),
+    "0.1" = digits(0.1),
+    "0.01" = digits(0.01),
+    "0.001" = digits(0.001),
+    ok.
+
+test_frexp() ->
+    %% zero
+    {0.0, 0} = frexp(0.0),
+    %% one
+    {0.5, 1} = frexp(1.0),
+    %% negative one
+    {-0.5, 1} = frexp(-1.0),
+    %% small denormalized number
+    %% 4.94065645841246544177e-324
+    <<SmallDenorm/float>> = <<0,0,0,0,0,0,0,1>>,
+    {0.5, -1073} = frexp(SmallDenorm),
+    %% large denormalized number
+    %% 2.22507385850720088902e-308
+    <<BigDenorm/float>> = <<0,15,255,255,255,255,255,255>>,
+    {0.99999999999999978, -1022} = frexp(BigDenorm),
+    %% small normalized number
+    %% 2.22507385850720138309e-308
+    <<SmallNorm/float>> = <<0,16,0,0,0,0,0,0>>,
+    {0.5, -1021} = frexp(SmallNorm),
+    %% large normalized number
+    %% 1.79769313486231570815e+308
+    <<LargeNorm/float>> = <<127,239,255,255,255,255,255,255>>,
+    {0.99999999999999989, 1024} = frexp(LargeNorm),
+    ok.

Added: incubator/couchdb/vendor/mochiweb/current/src/mochiweb.app
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/src/mochiweb.app?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/src/mochiweb.app (added)
+++ incubator/couchdb/vendor/mochiweb/current/src/mochiweb.app Mon Mar 31 03:30:27 2008
@@ -0,0 +1,29 @@
+{application, mochiweb,
+ [{description, "MochiMedia Web Server"},
+  {vsn, "0.01"},
+  {modules, [
+        mochihex,
+        mochijson,
+        mochijson2,
+        mochinum,
+        mochiweb,
+        mochiweb_app,
+        mochiweb_charref,
+        mochiweb_cookies,
+        mochiweb_echo,
+        mochiweb_headers,
+        mochiweb_html,
+        mochiweb_http,
+        mochiweb_multipart,
+        mochiweb_request,
+        mochiweb_response,
+        mochiweb_skel,
+        mochiweb_socket_server,
+        mochiweb_sup,
+        mochiweb_util,
+        reloader
+	    ]},
+  {registered, []},
+  {mod, {mochiweb_app, []}},
+  {env, []},
+  {applications, [kernel, stdlib]}]}.

Added: incubator/couchdb/vendor/mochiweb/current/src/mochiweb.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/src/mochiweb.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/src/mochiweb.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/src/mochiweb.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,101 @@
+%% @author Bob Ippolito <bob@mochimedia.com>
+%% @copyright 2007 Mochi Media, Inc.
+
+%% @doc Start and stop the MochiWeb server.
+
+-module(mochiweb).
+-author('bob@mochimedia.com').
+
+-export([start/0, stop/0]).
+-export([new_request/1, new_response/1]).
+-export([all_loaded/0, all_loaded/1, reload/0]).
+-export([test/0]).
+
+%% @spec start() -> ok
+%% @doc Start the MochiWeb server.
+start() ->
+    ensure_started(crypto),
+    application:start(mochiweb).
+
+%% @spec stop() -> ok
+%% @doc Stop the MochiWeb server.
+stop() ->
+    Res = application:stop(mochiweb),
+    application:stop(crypto),
+    Res.
+
+%% @spec test() -> ok
+%% @doc Run all of the tests for MochiWeb.
+test() ->
+    mochiweb_util:test(),
+    mochiweb_headers:test(),
+    mochiweb_cookies:test(),
+    mochihex:test(),
+    mochinum:test(),
+    mochijson:test(),
+    mochiweb_charref:test(),
+    mochiweb_html:test(),
+    test_request(),
+    ok.
+
+reload() ->
+    [c:l(Module) || Module <- all_loaded()].
+
+all_loaded() ->
+    all_loaded(filename:dirname(code:which(?MODULE))).
+
+all_loaded(Base) when is_atom(Base) ->
+    [];
+all_loaded(Base) ->
+    FullBase = Base ++ "/",
+    F = fun ({_Module, Loaded}, Acc) when is_atom(Loaded) ->
+                Acc;
+            ({Module, Loaded}, Acc) ->
+                case lists:prefix(FullBase, Loaded) of
+                    true ->
+                        [Module | Acc];
+                    false ->
+                        Acc
+                end
+        end,
+    lists:foldl(F, [], code:all_loaded()).
+
+
+%% @spec new_request({Socket, Request, Headers}) -> MochiWebRequest
+%% @doc Return a mochiweb_request data structure.
+new_request({Socket, {Method, {abs_path, Uri}, Version}, Headers}) ->
+    mochiweb_request:new(Socket,
+			 Method,
+			 Uri,
+			 Version,
+			 mochiweb_headers:make(Headers));
+% this case probably doesn't "exist".
+new_request({Socket, {Method, {absoluteURI, _Protocol, _Host, _Port, Uri},
+		      Version}, Headers}) ->
+    mochiweb_request:new(Socket,
+			 Method,
+			 Uri,
+			 Version,
+			 mochiweb_headers:make(Headers)).
+
+%% @spec new_response({Request, integer(), Headers}) -> MochiWebResponse
+%% @doc Return a mochiweb_response data structure.
+new_response({Request, Code, Headers}) ->
+    mochiweb_response:new(Request,
+			  Code,
+			  mochiweb_headers:make(Headers)).
+
+%% Internal API
+
+test_request() ->
+    R = mochiweb_request:new(z, z, "/foo/bar/baz%20wibble+quux?qs=2", z, []),
+    "/foo/bar/baz wibble quux" = R:get(path),
+    ok.
+
+ensure_started(App) ->
+    case application:start(App) of
+	ok ->
+	    ok;
+	{error, {already_started, App}} ->
+	    ok
+    end.

Added: incubator/couchdb/vendor/mochiweb/current/src/mochiweb_app.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/src/mochiweb_app.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/src/mochiweb_app.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/src/mochiweb_app.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,20 @@
+%% @author Bob Ippolito <bob@mochimedia.com>
+%% @copyright 2007 Mochi Media, Inc.
+
+%% @doc Callbacks for the mochiweb application.
+
+-module(mochiweb_app).
+-author('bob@mochimedia.com').
+
+-behaviour(application).
+-export([start/2,stop/1]).
+
+%% @spec start(_Type, _StartArgs) -> ServerRet
+%% @doc application start callback for mochiweb.
+start(_Type, _StartArgs) ->
+    mochiweb_sup:start_link().
+
+%% @spec stop(_State) -> ServerRet
+%% @doc application stop callback for mochiweb.
+stop(_State) ->
+    ok.

Added: incubator/couchdb/vendor/mochiweb/current/src/mochiweb_charref.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/vendor/mochiweb/current/src/mochiweb_charref.erl?rev=642953&view=auto
==============================================================================
--- incubator/couchdb/vendor/mochiweb/current/src/mochiweb_charref.erl (added)
+++ incubator/couchdb/vendor/mochiweb/current/src/mochiweb_charref.erl Mon Mar 31 03:30:27 2008
@@ -0,0 +1,295 @@
+%% @author Bob Ippolito <bob@mochimedia.com>
+%% @copyright 2007 Mochi Media, Inc.
+
+%% @doc Converts HTML 4 charrefs and entities to codepoints.
+-module(mochiweb_charref).
+-export([charref/1, test/0]).
+
+%% External API.
+
+%% @spec charref(S) -> integer() | undefined
+%% @doc Convert a decimal charref, hex charref, or html entity to a unicode
+%%      codepoint, or return undefined on failure.
+%%      The input should not include an ampersand or semicolon.
+%%      charref("#38") = 38, charref("#x26") = 38, charref("amp") = 38.
+charref(B) when is_binary(B) ->
+    charref(binary_to_list(B));
+charref([$#, C | L]) when C =:= $x orelse C =:= $X ->
+    try erlang:list_to_integer(L, 16)
+    catch
+        error:badarg -> undefined
+    end;
+charref([$# | L]) ->
+    try list_to_integer(L)
+    catch
+        error:badarg -> undefined
+    end;
+charref(L) ->
+    entity(L).
+    
+%% @spec test() -> ok
+%% @doc Run tests for mochiweb_charref.
+test() ->
+    1234 = charref("#1234"),
+    255 = charref("#xfF"),
+    255 = charref("#XFf"),
+    38 = charref("amp"),
+    undefined = charref("not_an_entity"),
+    ok.
+
+%% Internal API.
+
+entity("nbsp") -> 160;
+entity("iexcl") -> 161;
+entity("cent") -> 162;
+entity("pound") -> 163;
+entity("curren") -> 164;
+entity("yen") -> 165;
+entity("brvbar") -> 166;
+entity("sect") -> 167;
+entity("uml") -> 168;
+entity("copy") -> 169;
+entity("ordf") -> 170;
+entity("laquo") -> 171;
+entity("not") -> 172;
+entity("shy") -> 173;
+entity("reg") -> 174;
+entity("macr") -> 175;
+entity("deg") -> 176;
+entity("plusmn") -> 177;
+entity("sup2") -> 178;
+entity("sup3") -> 179;
+entity("acute") -> 180;
+entity("micro") -> 181;
+entity("para") -> 182;
+entity("middot") -> 183;
+entity("cedil") -> 184;
+entity("sup1") -> 185;
+entity("ordm") -> 186;
+entity("raquo") -> 187;
+entity("frac14") -> 188;
+entity("frac12") -> 189;
+entity("frac34") -> 190;
+entity("iquest") -> 191;
+entity("Agrave") -> 192;
+entity("Aacute") -> 193;
+entity("Acirc") -> 194;
+entity("Atilde") -> 195;
+entity("Auml") -> 196;
+entity("Aring") -> 197;
+entity("AElig") -> 198;
+entity("Ccedil") -> 199;
+entity("Egrave") -> 200;
+entity("Eacute") -> 201;
+entity("Ecirc") -> 202;
+entity("Euml") -> 203;
+entity("Igrave") -> 204;
+entity("Iacute") -> 205;
+entity("Icirc") -> 206;
+entity("Iuml") -> 207;
+entity("ETH") -> 208;
+entity("Ntilde") -> 209;
+entity("Ograve") -> 210;
+entity("Oacute") -> 211;
+entity("Ocirc") -> 212;
+entity("Otilde") -> 213;
+entity("Ouml") -> 214;
+entity("times") -> 215;
+entity("Oslash") -> 216;
+entity("Ugrave") -> 217;
+entity("Uacute") -> 218;
+entity("Ucirc") -> 219;
+entity("Uuml") -> 220;
+entity("Yacute") -> 221;
+entity("THORN") -> 222;
+entity("szlig") -> 223;
+entity("agrave") -> 224;
+entity("aacute") -> 225;
+entity("acirc") -> 226;
+entity("atilde") -> 227;
+entity("auml") -> 228;
+entity("aring") -> 229;
+entity("aelig") -> 230;
+entity("ccedil") -> 231;
+entity("egrave") -> 232;
+entity("eacute") -> 233;
+entity("ecirc") -> 234;
+entity("euml") -> 235;
+entity("igrave") -> 236;
+entity("iacute") -> 237;
+entity("icirc") -> 238;
+entity("iuml") -> 239;
+entity("eth") -> 240;
+entity("ntilde") -> 241;
+entity("ograve") -> 242;
+entity("oacute") -> 243;
+entity("ocirc") -> 244;
+entity("otilde") -> 245;
+entity("ouml") -> 246;
+entity("divide") -> 247;
+entity("oslash") -> 248;
+entity("ugrave") -> 249;
+entity("uacute") -> 250;
+entity("ucirc") -> 251;
+entity("uuml") -> 252;
+entity("yacute") -> 253;
+entity("thorn") -> 254;
+entity("yuml") -> 255;
+entity("fnof") -> 402;
+entity("Alpha") -> 913;
+entity("Beta") -> 914;
+entity("Gamma") -> 915;
+entity("Delta") -> 916;
+entity("Epsilon") -> 917;
+entity("Zeta") -> 918;
+entity("Eta") -> 919;
+entity("Theta") -> 920;
+entity("Iota") -> 921;
+entity("Kappa") -> 922;
+entity("Lambda") -> 923;
+entity("Mu") -> 924;
+entity("Nu") -> 925;
+entity("Xi") -> 926;
+entity("Omicron") -> 927;
+entity("Pi") -> 928;
+entity("Rho") -> 929;
+entity("Sigma") -> 931;
+entity("Tau") -> 932;
+entity("Upsilon") -> 933;
+entity("Phi") -> 934;
+entity("Chi") -> 935;
+entity("Psi") -> 936;
+entity("Omega") -> 937;
+entity("alpha") -> 945;
+entity("beta") -> 946;
+entity("gamma") -> 947;
+entity("delta") -> 948;
+entity("epsilon") -> 949;
+entity("zeta") -> 950;
+entity("eta") -> 951;
+entity("theta") -> 952;
+entity("iota") -> 953;
+entity("kappa") -> 954;
+entity("lambda") -> 955;
+entity("mu") -> 956;
+entity("nu") -> 957;
+entity("xi") -> 958;
+entity("omicron") -> 959;
+entity("pi") -> 960;
+entity("rho") -> 961;
+entity("sigmaf") -> 962;
+entity("sigma") -> 963;
+entity("tau") -> 964;
+entity("upsilon") -> 965;
+entity("phi") -> 966;
+entity("chi") -> 967;
+entity("psi") -> 968;
+entity("omega") -> 969;
+entity("thetasym") -> 977;
+entity("upsih") -> 978;
+entity("piv") -> 982;
+entity("bull") -> 8226;
+entity("hellip") -> 8230;
+entity("prime") -> 8242;
+entity("Prime") -> 8243;
+entity("oline") -> 8254;
+entity("frasl") -> 8260;
+entity("weierp") -> 8472;
+entity("image") -> 8465;
+entity("real") -> 8476;
+entity("trade") -> 8482;
+entity("alefsym") -> 8501;
+entity("larr") -> 8592;
+entity("uarr") -> 8593;
+entity("rarr") -> 8594;
+entity("darr") -> 8595;
+entity("harr") -> 8596;
+entity("crarr") -> 8629;
+entity("lArr") -> 8656;
+entity("uArr") -> 8657;
+entity("rArr") -> 8658;
+entity("dArr") -> 8659;
+entity("hArr") -> 8660;
+entity("forall") -> 8704;
+entity("part") -> 8706;
+entity("exist") -> 8707;
+entity("empty") -> 8709;
+entity("nabla") -> 8711;
+entity("isin") -> 8712;
+entity("notin") -> 8713;
+entity("ni") -> 8715;
+entity("prod") -> 8719;
+entity("sum") -> 8721;
+entity("minus") -> 8722;
+entity("lowast") -> 8727;
+entity("radic") -> 8730;
+entity("prop") -> 8733;
+entity("infin") -> 8734;
+entity("ang") -> 8736;
+entity("and") -> 8743;
+entity("or") -> 8744;
+entity("cap") -> 8745;
+entity("cup") -> 8746;
+entity("int") -> 8747;
+entity("there4") -> 8756;
+entity("sim") -> 8764;
+entity("cong") -> 8773;
+entity("asymp") -> 8776;
+entity("ne") -> 8800;
+entity("equiv") -> 8801;
+entity("le") -> 8804;
+entity("ge") -> 8805;
+entity("sub") -> 8834;
+entity("sup") -> 8835;
+entity("nsub") -> 8836;
+entity("sube") -> 8838;
+entity("supe") -> 8839;
+entity("oplus") -> 8853;
+entity("otimes") -> 8855;
+entity("perp") -> 8869;
+entity("sdot") -> 8901;
+entity("lceil") -> 8968;
+entity("rceil") -> 8969;
+entity("lfloor") -> 8970;
+entity("rfloor") -> 8971;
+entity("lang") -> 9001;
+entity("rang") -> 9002;
+entity("loz") -> 9674;
+entity("spades") -> 9824;
+entity("clubs") -> 9827;
+entity("hearts") -> 9829;
+entity("diams") -> 9830;
+entity("quot") -> 34;
+entity("amp") -> 38;
+entity("lt") -> 60;
+entity("gt") -> 62;
+entity("OElig") -> 338;
+entity("oelig") -> 339;
+entity("Scaron") -> 352;
+entity("scaron") -> 353;
+entity("Yuml") -> 376;
+entity("circ") -> 710;
+entity("tilde") -> 732;
+entity("ensp") -> 8194;
+entity("emsp") -> 8195;
+entity("thinsp") -> 8201;
+entity("zwnj") -> 8204;
+entity("zwj") -> 8205;
+entity("lrm") -> 8206;
+entity("rlm") -> 8207;
+entity("ndash") -> 8211;
+entity("mdash") -> 8212;
+entity("lsquo") -> 8216;
+entity("rsquo") -> 8217;
+entity("sbquo") -> 8218;
+entity("ldquo") -> 8220;
+entity("rdquo") -> 8221;
+entity("bdquo") -> 8222;
+entity("dagger") -> 8224;
+entity("Dagger") -> 8225;
+entity("permil") -> 8240;
+entity("lsaquo") -> 8249;
+entity("rsaquo") -> 8250;
+entity("euro") -> 8364;
+entity(_) -> undefined.
+



Mime
View raw message