aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-11-23 22:13:07 +0000
committerMartin Ashby <martin@ashbysoft.com>2023-11-23 22:13:07 +0000
commit3e5a03b51c8debf0b9d41ca477d44f8fb976d65b (patch)
tree567181fc0077d5f2034e2e3b32f75e3ae75a227f
parentcce8f3045f14aa9a2ab69852cd60f4f88550f722 (diff)
downloadroctorrent-3e5a03b51c8debf0b9d41ca477d44f8fb976d65b.tar.gz
roctorrent-3e5a03b51c8debf0b9d41ca477d44f8fb976d65b.tar.bz2
roctorrent-3e5a03b51c8debf0b9d41ca477d44f8fb976d65b.tar.xz
roctorrent-3e5a03b51c8debf0b9d41ca477d44f8fb976d65b.zip
Add bencoded number decoding
-rw-r--r--main.roc44
1 files changed, 43 insertions, 1 deletions
diff --git a/main.roc b/main.roc
index 033ff1d..c90531f 100644
--- a/main.roc
+++ b/main.roc
@@ -25,10 +25,52 @@ expect
res == Ok (Str.toUtf8 "foo")
expect
- errCases = ["foo", "1:foo", "4:foo", ":", "foo:", "1:", "::"]
+ errCases = [
+ "",
+ "foo",
+ "1:foo",
+ "4:foo",
+ ":",
+ "foo:",
+ "1:",
+ "::",
+ ]
List.all errCases \t ->
res = bDecodeStr (Str.toUtf8 t)
res == Err Malformatted
+bDecodeNum: List U8 -> Result I64 [Malformatted]
+bDecodeNum = \i ->
+ when i is
+ ['i', .., 'e'] ->
+ ii = List.sublist i {start: 1, len: ((List.len i) - 2)}
+ when (Str.fromUtf8 ii) is
+ Ok iii ->
+ when (Str.toI64 iii) is
+ Ok res -> Ok res
+ Err _err -> Err Malformatted
+ Err _err -> Err Malformatted
+ _ -> Err Malformatted
+
+expect
+ res = bDecodeNum (Str.toUtf8 "i32e")
+ res == Ok 32i64
+
+expect
+ errCases = [
+ "",
+ "foo",
+ "i32",
+ "32e",
+ "ifooe",
+ "ie",
+ "i",
+ "e",
+ "i522222222222222222343423432434322e",
+ ]
+ List.all errCases \t ->
+ res = bDecodeNum (Str.toUtf8 t)
+ res == Err Malformatted
+
main =
Stdout.line "Hello, World"