factorization

This commit is contained in:
Mylloon 2023-06-14 16:37:30 +02:00
parent 98f2058da6
commit bf337663df
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -10,7 +10,14 @@ let parse_header reader =
| _ -> failwith "Invalid number of fields"
;;
let decode_name_simple reader =
let rec parse_question reader =
let reader', name_b = decode_name_simple reader in
let name = String.to_bytes name_b in
match List.init 2 (fun offset -> unpack_short_be reader' (offset * 2)) with
| [ type_; class_ ] -> { name; type_; class_ }
| _ -> failwith "Invalid number of fields"
and decode_name_simple reader =
let rec read_parts parts pos =
let length = int_of_char (Bytes.get reader pos) in
if length = 0
@ -23,11 +30,3 @@ let decode_name_simple reader =
let offset, parts = read_parts [] 0 in
bytes_forward reader offset, String.concat "." parts
;;
let parse_question reader =
let reader', name_b = decode_name_simple reader in
let name = String.to_bytes name_b in
match List.init 2 (fun offset -> unpack_short_be reader' (offset * 2)) with
| [ type_; class_ ] -> { name; type_; class_ }
| _ -> failwith "Invalid number of fields"
;;