diff options
| author | ssmith | 2006-05-14 02:02:10 -0700 |
|---|---|---|
| committer | ssmith | 2006-05-14 02:02:10 -0700 |
| commit | 0d39716d6493ab31780dc7bba9d376bd7218f233 (patch) | |
| tree | 0ba1c581db67a5318cd85ee3a201cb12714a4fa7 | |
| parent | be5e8817ca5570b66f57f2d56148c88ae73a8531 (diff) | |
Misc updates in preparation for encoder
| -rw-r--r-- | bencode.erl | 34 | ||||
| -rw-r--r-- | tests.erl | 39 |
2 files changed, 42 insertions, 31 deletions
diff --git a/bencode.erl b/bencode.erl index aaf737e..fb8e9f3 100644 --- a/bencode.erl +++ b/bencode.erl @@ -1,10 +1,17 @@ -module(bencode). --export([bdecode/1]). +-export([bdecode/1, bencode/1]). -import(string, [substr/2, substr/3, to_integer/1, chr/2]). +%% %% %% %% %% %% %% %% Decoding %% %% %% %% %% %% %% %% + +bencode(Val) when is_integer(Val) -> + "i" ++ integer_to_list(Val) ++ "e". + +%% %% %% %% %% %% %% %% Decoding %% %% %% %% %% %% %% %% + %% String bdecode([Char | _Rest] = Str) when Char >= $0, Char =< $9 -> ColIdx = chr(Str, $:), @@ -22,34 +29,33 @@ bdecode([Char | Rest]) when Char == $i -> %% List bdecode([Char | Rest]) when Char == $l -> - {List, Rem} = dolist(Rest), + {List, Rem} = declist(Rest), {{list, List}, Rem}; %% Dict bdecode([Char | Rest]) when Char == $d -> - {Dict, Rem} = dodict(Rest), + {Dict, Rem} = decdict(Rest), {{dict, Dict}, Rem}. -dolist(Str) -> - dolist(Str, []). +declist(Str) -> + declist(Str, []). -dolist([Char | Rest], List) when Char == $e -> +declist([Char | Rest], List) when Char == $e -> {List, Rest}; -dolist(Str, List) -> +declist(Str, List) -> {Result, Rest} = bdecode(Str), - dolist(Rest, lists:append(List, [Result])). + declist(Rest, lists:append(List, [Result])). -dodict(Str) -> - dodict(Str, dict:new()). +decdict(Str) -> + decdict(Str, dict:new()). -dodict([Char | Rest], Dict) when Char == $e -> +decdict([Char | Rest], Dict) when Char == $e -> {Dict, Rest}; -dodict(Str, Dict) -> +decdict(Str, Dict) -> {{string, Key}, Rest} = bdecode(Str), {Val, Rest2} = bdecode(Rest), - dodict(Rest2, dict:store(Key, Val, Dict)). - + decdict(Rest2, dict:store(Key, Val, Dict)). @@ -3,45 +3,50 @@ -include("eunit.hrl"). --export([start/0, - test_int1/0, test_int2/0, test_int3/0, test_int4/0, - test_string1/0, test_string2/0, - test_list1/0, test_list2/0, test_list3/0, test_list4/0, test_list5/0, - test_dict1/0]). +-export([start/0]). +-export([test_dec_int1/0, test_dec_int2/0, test_dec_int3/0, test_dec_int4/0, + test_dec_string1/0, test_dec_string2/0, + test_dec_list1/0, test_dec_list2/0, test_dec_list3/0, test_dec_list4/0, test_dec_list5/0, + test_dec_dict1/0]). +-export([test_enc_int1/0]). %% Integer decoding tests -test_int1() -> +test_enc_int1() -> + ?match("i999e", bencode:bencode(999)). + +%% Integer decoding tests +test_dec_int1() -> ?match({{int, 999}, []}, bencode:bdecode("i999e")). -test_int2() -> +test_dec_int2() -> ?match({{int, 0}, []}, bencode:bdecode("i0e")). -test_int3() -> +test_dec_int3() -> ?match({{int, 123}, "abc"}, bencode:bdecode("i123eabc")). -test_int4() -> +test_dec_int4() -> ?match({{int, -10}, []}, bencode:bdecode("i-10e")). %% String decoding tests -test_string1() -> +test_dec_string1() -> ?match({{string, ""}, []}, bencode:bdecode("0:")). -test_string2() -> +test_dec_string2() -> ?match({{string, "abcde"}, []}, bencode:bdecode("5:abcde")). %% List decoding tests -test_list1() -> +test_dec_list1() -> ?match({{list, []}, []}, bencode:bdecode("le")). -test_list2() -> +test_dec_list2() -> ?match({{list, [{int,1}]}, []}, bencode:bdecode("li1ee")). -test_list3() -> +test_dec_list3() -> ?match({{list, [{int,1}]}, []}, bencode:bdecode("li1ee")). -test_list4() -> +test_dec_list4() -> ?match({{list, [{int,1}, {int,2}]}, []}, bencode:bdecode("li1ei2ee")). -test_list5() -> +test_dec_list5() -> ?match({{list, [{int,1}, {list, [{int, 2}, {int, 3}]}]}, []}, bencode:bdecode("li1eli2ei3eee")). %% Dict decoding tests -test_dict1() -> +test_dec_dict1() -> {{dict, Dict}, []} = bencode:bdecode("d3:agei25e4:eyes4:bluee"), ?match([{"age", {int, 25}}, {"eyes", {string, "blue"}}], dict:to_list(Dict)). |
