Commits

Anonymous committed 9433b13

encoding atom and string.

Comments (0)

Files changed (2)

   in
     print_ 0;;
 
+let init_buf buf =
+  String.set buf 0 (char_of_int 131);
+  1;;
 
-(* encode to BERT binary: erlterm -> string
-   In future, this should be in C code *)
+(** these funcs have typical signature:
+    [encode_* term buffer offset] -> offset + bytes_written **)
 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;
+      2+ofs;
     end
   else if i < 0xFFFFFFFF then (* INTEGER_EXT *)
     let j = Int32.of_int i in
 	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;
+      5+ofs;
     end
   else (* SMALL_BIG_EXT *) 
     raise Not_supported;;
   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;;
+    32+ofs;;
 
-let encode_atom atom =
-  if String.length atom > 255 then (* ATOM_EXT *) 
-    "d"
-  else (* SMALL_ATOM_EXT *)
-    "q";;
+let encode_atom atom buf ofs=
+  let len = String.length atom in
+    if len < 256 then (* SMALL_ATOM_EXT *)
+      let str = Printf.sprintf "q %s" atom in
+	begin 
+	  String.set str 1 (char_of_int len);
+	  String.blit str 0 buf ofs (len+2);
+	  ofs + String.length str;
+	end
+    else (* ATOM_EXT *)
+      let str = Printf.sprintf "d  %s" atom in
+	begin
+	  String.set str 1 (char_of_int (len/256));
+	  String.set str 2 (char_of_int (len mod 256));
+	  String.blit str 0 buf ofs (len+3);
+	  ofs + String.length str;
+	end;;
+
+let encode_string str buf ofs =   (* STRING_EXT *) 
+  let len = String.length str in
+  let tmp_str = Printf.sprintf "k  %s" str in
+    String.set tmp_str 1 (char_of_int (len/256));
+    String.set tmp_str 2 (char_of_int (len mod 256));
+    String.blit tmp_str 0 buf ofs (len+3);
+    ofs + String.length str;;
+
+let encode_binary bin = 
+  (* BINARY_EXT *) "m";;
 
 let encode_tuple tuple = function
   | Tuple(t) when List.length t < 256 -> (* SMALL_TUPLE_EXT *)
       "i";
   | _ -> raise Not_supported;;
 
-let encode_string str = 
-  (* STRING_EXT *) "k";;
-
 let encode_list list = 
   (* LIST_EXT *) "l";;
 
-let encode_binary bin = 
-  (* BINARY_EXT *) "m";;
 
 
 let _ = 
   let str = String.create 64 in
-  let i = Bert.encode_int 774567 str 0 in
+  let _ = Bert.init_buf str in
+  let i = Bert.encode_int 774567 str 1 in
   let j = Bert.encode_float 234.354 str i in
+  let k = Bert.encode_atom "HAHAHAHA" str j in
+  let l = Bert.encode_string "HAHAHAHA" str k in
     Bert.print_binary_string str;
 (*    print_endline str; *)
-    j;;
+    l;;
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.