Commits

Anonymous committed 9b0aa34

encoding lists and tuples implemented.

Comments (0)

Files changed (1)

     String.blit str 0 buf ofs (String.length str);
     ofs + String.length str;;
 
-let encode_tuple tuple = function
-  | Tuple(t) when List.length t < 256 -> (* SMALL_TUPLE_EXT *)
-      "h";
-  | Tuple(t) -> (* LARGE_TUPLE_EXT *)
-      "i";
-  | _ -> raise Not_supported;;
+let rec encode_tuple tuple buf ofs =
+  let rec enc_tuple_ l nofs =
+    match l with
+      | [] -> nofs;
+      | hd::tl -> 
+	  let offset = encode_term hd buf nofs in
+	    enc_tuple_ tl offset
+  in
+  let len = List.length tuple in
+    if List.length tuple < 256 then begin (* SMALL_TUPLE_EXT *)
+      String.set buf 0 'h';
+      String.set buf 1 (char_of_int len);
+      enc_tuple_ tuple (ofs+2);
+    end else begin
+      (* LARGE_TUPLE_EXT *)
+      let (a,b,c,d) = encode_int32 len in
+	String.set buf 0 'i';
+	String.set buf 1 a;
+	String.set buf 2 b;
+	String.set buf 3 c;
+	String.set buf 4 d;
+	enc_tuple_ tuple (ofs+5);
+    end
 
-let encode_list list = 
-  (* LIST_EXT *) "l";;
-
-let encode_term term buf ofs =
+and encode_list list buf ofs = (* LIST_EXT *)
+  let rec enc_list_ l nofs =
+    match l with
+      | [] -> nofs;
+      | hd::tl ->
+	  let offset = encode_term hd buf nofs in
+	    enc_list_ tl offset 
+  in
+  let len = List.length list in
+    String.set buf 0 'l';
+    String.set buf 1 (char_of_int len);
+    enc_list_ list (ofs+2)
+      
+and encode_term term buf ofs =
   String.set buf ofs (char_of_int 131);
   match term with
     | Int(i) ->   encode_int i buf (ofs+1); 
     | Atom(a)->   encode_atom a buf (ofs+1);
     | String(s)-> encode_string s buf (ofs+1);
     | Binary(b)-> encode_binary b buf (ofs+1);
+    | Tuple(t)->  encode_tuple t buf (ofs+1);
+    | List(l)->   encode_list l buf (ofs+1);
     | _ -> raise Unknown_type;;