Move find_in_binary from couch_httpd to couch_util Simple move so that we can test it from etap. Seems a bit awkward to export from couch_httpd so I moved it to the util module.
Fixes COUCHDB-1953 Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/25cceb0c Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/25cceb0c Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/25cceb0c Branch: refs/heads/1953-fix-mime-multipart-parsing Commit: 25cceb0c51f62f032e2933a6445391ebbdd9068b Parents: a4b1aa3 Author: Paul J. Davis <[email protected]> Authored: Fri Jan 24 18:59:04 2014 -0600 Committer: Paul J. Davis <[email protected]> Committed: Fri Jan 24 19:03:00 2014 -0600 ---------------------------------------------------------------------- src/couchdb/couch_httpd.erl | 32 +------------------------------- src/couchdb/couch_util.erl | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/25cceb0c/src/couchdb/couch_httpd.erl ---------------------------------------------------------------------- diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 11acf1c..9245f4b 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -1003,7 +1003,7 @@ split_header(Line) -> mochiweb_util:parse_header(Value)}]. read_until(#mp{data_fun=DataFun, buffer=Buffer}=Mp, Pattern, Callback) -> - case find_in_binary(Pattern, Buffer) of + case couch_util:find_in_binary(Pattern, Buffer) of not_found -> Callback2 = Callback(Buffer), {Buffer2, DataFun2} = DataFun(), @@ -1079,36 +1079,6 @@ check_for_last(#mp{buffer=Buffer, data_fun=DataFun}=Mp) -> data_fun = DataFun2}) end. -find_in_binary(_B, <<>>) -> - not_found; - -find_in_binary(B, Data) -> - case binary:match(Data, [B], []) of - nomatch -> - MatchLength = erlang:min(byte_size(B), byte_size(Data)), - match_prefix_at_end(binary:part(B, {0, MatchLength}), - binary:part(Data, {byte_size(Data), -MatchLength}), - MatchLength, byte_size(Data) - MatchLength); - {Pos, _Len} -> - {exact, Pos} - end. - -match_prefix_at_end(Prefix, Data, PrefixLength, N) -> - FirstCharMatches = binary:matches(Data, [binary:part(Prefix, {0, 1})], []), - match_rest_of_prefix(FirstCharMatches, Prefix, Data, PrefixLength, N). - -match_rest_of_prefix([], _Prefix, _Data, _PrefixLength, _N) -> - not_found; - -match_rest_of_prefix([{Pos, _Len} | Rest], Prefix, Data, PrefixLength, N) -> - case binary:match(binary:part(Data, {PrefixLength, Pos - PrefixLength}), - [binary:part(Prefix, {0, PrefixLength - Pos})], []) of - nomatch -> - match_rest_of_prefix(Rest, Prefix, Data, PrefixLength, N); - {_Pos, _Len1} -> - {partial, N + Pos} - end. - validate_bind_address(Address) -> case inet_parse:address(Address) of {ok, _} -> ok; http://git-wip-us.apache.org/repos/asf/couchdb/blob/25cceb0c/src/couchdb/couch_util.erl ---------------------------------------------------------------------- diff --git a/src/couchdb/couch_util.erl b/src/couchdb/couch_util.erl index afe3528..2509bef 100644 --- a/src/couchdb/couch_util.erl +++ b/src/couchdb/couch_util.erl @@ -29,6 +29,7 @@ -export([encode_doc_id/1]). -export([with_db/2]). -export([rfc1123_date/0, rfc1123_date/1]). +-export([find_in_binary/2]). -include("couch_db.hrl"). @@ -487,3 +488,34 @@ month(9) -> "Sep"; month(10) -> "Oct"; month(11) -> "Nov"; month(12) -> "Dec". + + +find_in_binary(_B, <<>>) -> + not_found; + +find_in_binary(B, Data) -> + case binary:match(Data, [B], []) of + nomatch -> + MatchLength = erlang:min(byte_size(B), byte_size(Data)), + match_prefix_at_end(binary:part(B, {0, MatchLength}), + binary:part(Data, {byte_size(Data), -MatchLength}), + MatchLength, byte_size(Data) - MatchLength); + {Pos, _Len} -> + {exact, Pos} + end. + +match_prefix_at_end(Prefix, Data, PrefixLength, N) -> + FirstCharMatches = binary:matches(Data, [binary:part(Prefix, {0, 1})], []), + match_rest_of_prefix(FirstCharMatches, Prefix, Data, PrefixLength, N). + +match_rest_of_prefix([], _Prefix, _Data, _PrefixLength, _N) -> + not_found; + +match_rest_of_prefix([{Pos, _Len} | Rest], Prefix, Data, PrefixLength, N) -> + case binary:match(binary:part(Data, {PrefixLength, Pos - PrefixLength}), + [binary:part(Prefix, {0, PrefixLength - Pos})], []) of + nomatch -> + match_rest_of_prefix(Rest, Prefix, Data, PrefixLength, N); + {_Pos, _Len1} -> + {partial, N + Pos} + end.
