Commits

Anonymous committed 89e6797

binary encoding of int, float is implemented. need testing.

Comments (0)

Files changed (2)

 
 (* encode to BERT binary: erlterm -> string
    In future, this should be in C code *)
-let encode term = function 
-  | Int(i) when i < 256 -> (* SMALL_INTEGER_EXT *)
-      Printf.sprintf "a%.2c" (char_of_int i);
-  | Int(i) when i < 0xFFFFFFFF -> (* SMALL_INTEGER_EXT *)
-      Printf.sprintf "b%d" i;
-  | Float(f) -> (* FLOAT_EXT *)
-      "c";
-  | Atom(atom) when String.length atom > 255 -> (* ATOM_EXT *) "d";
+let encode_int i buf ofs =
+  if i < 256 then (* SMALL_INTEGER_EXT *)
+    begin 
+      String.set buf ofs 'a';
+      String.set buf (ofs+1) (char_of_int i);
+      2;
+    end
+  else if i < 0xFFFFFFFF then (* INTEGER_EXT *)
+    let j = Int32.of_int i in
+    let ff = Int32.of_int 0xFF in
+      begin
+	String.set buf ofs 'b';
+	String.set buf (ofs+1) (char_of_int (Int32.to_int (Int32.logand ff (Int32.shift_right j 24))));
+	String.set buf (ofs+2) (char_of_int (Int32.to_int (Int32.logand ff (Int32.shift_right j 16))));
+	String.set buf (ofs+3) (char_of_int (Int32.to_int (Int32.logand ff (Int32.shift_right j 8))));
+	String.set buf (ofs+4) (char_of_int (Int32.to_int (Int32.logand ff j)));
+      5;
+    end
+  else (* SMALL_BIG_EXT *) 
+    raise Not_supported;;
+(**    begin String.set buf ofs 'n'; 1 end;; (* LARGE_BIG_EXT -> "o" *) **)
+
+let encode_float f buf ofs = 
+   (* FLOAT_EXT 'c' *)
+  let str = Printf.sprintf "c%.20e" f in
+  let len = min (String.length buf - ofs) (String.length str) in
+    String.blit str 0 buf ofs len;
+    len;;
+
+let encode_atom atom =
+  if String.length atom > 255 then (* ATOM_EXT *) 
+    "d"
+  else (* SMALL_ATOM_EXT *)
+    "q";;
+
+let encode_tuple tuple = function
   | Tuple(t) when List.length t < 256 -> (* SMALL_TUPLE_EXT *)
       "h";
   | Tuple(t) -> (* LARGE_TUPLE_EXT *)
       "i";
-  | String(str)-> (* STRING_EXT *) "k";
-  | List(list)->  (* LIST_EXT *) "l";
-  | Binary(bin)-> (* BINARY_EXT *) "m";
-  | Int(i) -> (* SMALL_BIG_EXT *) "n"; (* LARGE_BIG_EXT -> "o" *)
-  | Atom(atom) -> (* SMALL_ATOM_EXT *) "q";
-  | _ -> raise Unknown_type;;
+  | _ -> raise Not_supported;;
 
+let encode_string str = 
+  (* STRING_EXT *) "k";;
+
+let encode_list list = 
+  (* LIST_EXT *) "l";;
+
+let encode_binary bin = 
+  (* BINARY_EXT *) "m";;
+
 	  [print_termfile "termwriter.bert"];
       | list -> 
 	  List.map print_termfile list;;
+
+let _ = 
+  let str = String.create 64 in
+  let i = Bert.encode_int 774567 str 0 in
+  let j = Bert.encode_float 234.354 str i in
+    Bert.print_binary_string str;
+(*    print_endline str; *)
+    j;;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.