From bf337663df14a764950638bb26d43bad89b9b7cb Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 14 Jun 2023 16:37:30 +0200 Subject: [PATCH] factorization --- lib/response.ml | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/response.ml b/lib/response.ml index 6221034..56402d4 100644 --- a/lib/response.ml +++ b/lib/response.ml @@ -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" -;;