Commits

Anonymous committed e7c1f64

reading ints moved to ocaml-iteratees

Comments (0)

Files changed (4)

     value junk : iteratee 'el unit
     ;
 
+    value read_uint : iteratee char int
+    ;
+
+    value read_uint64 : iteratee char int64
+    ;
+
     module Ops
      :
       sig

dumbstreaming_it.ml

       -> I.iteratee char (option 'a)
     ;
 
-
-    (* for tests: *)
-    value read_uint : I.iteratee char int;
-    value read_uint_nz : I.iteratee char int;
-    value read_int : I.iteratee char int;
-    value read_int_nz : I.iteratee char int;
-
-    value read_uint32 : I.iteratee char int32;
-    value read_uint32_nz : I.iteratee char int32;
-    value read_int32 : I.iteratee char int32;
-    value read_int32_nz : I.iteratee char int32;
-
-    value read_uint64 : I.iteratee char int64;
-    value read_uint64_nz : I.iteratee char int64;
-    value read_int64 : I.iteratee char int64;
-    value read_int64_nz : I.iteratee char int64;
-    (* . *)
-
     value read_lengths : int -> I.iteratee char (list int)
     ;
 
     value ie_cont k = I.IE_cont None k
     ;
 
-    (* todo: move to ocaml-iteratees: *)
-    module It_add
-     :
-      sig
-        value itlist_anyresult_lasterror
-         : list (I.iteratee 'el 'a) -> I.iteratee 'el 'a
-        ;
-
-        value read_uint : I.iteratee char int;
-        value read_uint_nz : I.iteratee char int;
-        value read_int : I.iteratee char int;
-        value read_int_nz : I.iteratee char int;
-
-        value read_uint32 : I.iteratee char int32;
-        value read_uint32_nz : I.iteratee char int32;
-        value read_int32 : I.iteratee char int32;
-        value read_int32_nz : I.iteratee char int32;
-
-        value read_uint64 : I.iteratee char int64;
-        value read_uint64_nz : I.iteratee char int64;
-        value read_int64 : I.iteratee char int64;
-        value read_int64_nz : I.iteratee char int64;
-
-      end
-     =
-      struct
-
-        module IO = I.It_IO;
-
-        value ( & ) f x = f x;
-
-        value ( >>% ) = IO.bind_rev;
-
-        open I;
-        value ( >>= ) m f = bindI f m;
- 
-        value itlist_anyresult_lasterror = I.itlist_anyresult_lasterror
-        ;
-
-        module type SIGNED_INT
-         =
-          sig
-            type t;
-            value max_int : t;
-            (* min_int = -max_int - 1 *)
-
-            (* must work for small ints and small numbers:t : *)
-            value of_int : int -> t;
-            value to_int : t -> int;
-
-            (* may overflow silently: *)
-            value ( + ) : t -> t -> t;
-            value ( - ) : t -> t -> t;
-            value ( * ) : t -> t -> t;
-
-            (* should truncate towards zero: *)
-            value ( / ) : t -> t -> t;
-
-            value ( <? ) : t -> t -> bool;
-            value ( =? ) : t -> t -> bool;
-          end
-        ;
-
-        module SInt_T : SIGNED_INT with type t = int
-         =
-          struct
-            type t = int;
-            value max_int = Pervasives.max_int;
-            value of_int x = x;
-            value to_int x = x;
-            value ( + ) = Pervasives.( + );
-            value ( - ) = Pervasives.( - );
-            value ( * ) = Pervasives.( * );
-            value ( / ) = Pervasives.( / );
-            value ( <? ) = Pervasives.( < );
-            value ( =? ) = Pervasives.( == );
-          end
-        ;
-
-        module SInt32_T : SIGNED_INT with type t = int32
-         =
-          struct
-            type t = int32;
-            value max_int = Int32.max_int;
-            value of_int = Int32.of_int;
-            value to_int = Int32.to_int;
-            value ( + ) = Int32.add;
-            value ( - ) = Int32.sub;
-            value ( * ) = Int32.mul;
-            value ( / ) = Int32.div;
-            value ( <? ) a b = (Int32.compare a b) < 0;
-            value ( =? ) a b = (Int32.compare a b) = 0;
-          end
-        ;
-
-        module SInt64_T : SIGNED_INT with type t = int64
-         =
-          struct
-            type t = int64;
-            value max_int = Int64.max_int;
-            value of_int = Int64.of_int;
-            value to_int = Int64.to_int;
-            value ( + ) = Int64.add;
-            value ( - ) = Int64.sub;
-            value ( * ) = Int64.mul;
-            value ( / ) = Int64.div;
-            value ( <? ) a b = (Int64.compare a b) < 0;
-            value ( =? ) a b = (Int64.compare a b) = 0;
-          end
-        ;
-
-        exception SInt_overflow;
-        exception SInt_not_a_number of string;
-
-        module SInt_F (S : SIGNED_INT)
-         :
-          sig
-            (* value digits : S.t -> int; *)
-
-            value read_unsigned_gen : ~allow0:bool -> iteratee char S.t;
-            value read_signed_gen : ~allow0:bool -> iteratee char S.t;
-          end
-         =
-          struct
-            open S;
-
-            value zero = of_int 0
-            ;
-
-            value ( ~- ) n = zero - n
-              and ( >? ) a b = not (a <? b) && not (a =? b)
-              and ( >=? ) a b = not (a <? b)
-              and ( <>? ) a b = not (a =? b)
-            ;
-
-            value one = of_int 1
-            ;
-
-            value minus_one = (- one)
-            ;
-
-            value min_int = (- max_int) - one
-            ;
-
-            value ten = of_int 10
-            ;
-
-            value rec digits_count n =
-              Pervasives.( + )
-                1
-                (let n' = n / ten in
-                 if n' =? zero
-                 then 0
-                 else digits_count n'
-                )
-            ;
-
-            value rem a b = (a - (a / b) * b)
-            ;
-
-            module P = Pervasives;
-
-            value string_reverse_inplace str = P.(
-              let len = String.length str in
-              let len1 = len - 1 in
-              let len2 = len / 2 - 1 in
-              ( for i = 0 to len2
-                do
-                  let j = P.( - ) len1 i in
-                  let tmp = str.[i] in
-                  ( str.[i] := str.[j]
-                  ; str.[j] := tmp
-                  )
-                done
-              ; str
-              )
-            );
-
-            value min_int_digits = digits_count min_int;
-            value max_int_digits = digits_count max_int;
-
-            value to_base_abs b n =
-              let buf = Buffer.create max_int_digits in
-              let intb = to_int b in
-              let digit n =
-                let n = abs (to_int n) in
-                ( assert (n < intb)
-                ; assert (n < 10)
-                ; Char.chr (P.( + ) n (Char.code '0'))
-                ) in
-              let rec loop n =
-                let d = rem n b in
-                let c = digit d in
-                let () = Buffer.add_char buf c in
-                let n' = n / b in
-                if n' =? zero
-                then string_reverse_inplace (Buffer.contents buf)
-                else loop n'
-              in
-                loop n
-            ;
-
-            value to_dec_abs = to_base_abs ten
-            ;
-
-            value max_int_dec_abs = to_dec_abs max_int;
-
-            value min_int_dec_abs = to_dec_abs min_int;
-
-
-            value from_base_neg b ~maxstr str =
-              let len = String.length str in
-              let rec loop acc i =
-                if i = len
-                then acc
-                else
-                  let digit ch =
-                    if (ch >= '0' && ch <= '9')
-                    then
-                      let d = of_int (P.( - ) (Char.code ch) (Char.code '0')) in
-                      if d >=? b
-                      then raise (SInt_not_a_number "")
-                      else d
-                    else assert False
-                  in
-                  let ch = str.[i] in
-                  let acc' = acc * b - digit ch in
-                  loop acc' (P.( + ) i 1)
-              in
-                if len = 0
-                then `Empty
-                else
-                let maxlen = String.length maxstr in
-                if len > maxlen
-                then
-                  let () = dbgn "(from_base: len>maxlen) %!" in
-                  `Overflow
-                else if len = maxlen && str > maxstr
-                then
-                  let () = dbgn "(from_base: str>maxstr: %S > %S) %!"
-                    str maxstr
-                  in
-                  `Overflow
-                else
-                  try
-                    `Ok (loop zero 0)
-                  with
-                  [ SInt_not_a_number _ -> `Not_a_number ]
-            ;
-
-            value from_dec_neg ~maxstr = from_base_neg ~maxstr ten
-            ;
-
-            value is_digit c = (c <= '9' && c >= '0')
-            ;
-
-            value is_not_digit c = not (is_digit c)
-            ;
-
-            value inan msg = I.throw_err (SInt_not_a_number msg)
-            ;
-
-            value peek_digit =
-              peek >>= fun optc ->
-              I.return (
-                match optc with
-                [ Some c when is_digit c -> optc
-                | None | Some _ -> None
-                ]
-              )
-            ;
-
-            value read_gen
-              ~allow0
-              ~max_num_digits
-              ~maxstr
-              ~sign
-             :
-              iteratee char S.t
-             =
-              let rec read_beginning ~read0 =
-                peek_digit >>= fun optd ->
-                match optd with
-                [ None ->
-                    if read0
-                    then I.return (Some zero)
-                    else inan "begins with not a digit"
-                | Some d ->
-                    match (d, read0, allow0) with
-                    [ ('0', _, True)
-                    | ('0', False, False) ->
-                      I.junk >>= fun () ->
-                      read_beginning ~read0:True
-                    | (_, True, False) -> inan "leading zeroes"
-                    | (_, False, _) -> I.return None
-                    | (_, True, True) -> I.return None
-                    ]
-                ]
-              in
-              read_beginning ~read0:False >>= fun
-              [ Some r -> I.return r
-              | None ->
-                   (I.limit max_num_digits &
-                    I.break_chars is_not_digit
-                   ) >>= fun it ->
-                   I.joinI (I.return it) >>= fun res ->
-                   peek_digit >>= fun optd ->
-                   match (it, res, optd) with
-                   [ (I.IE_done _, _, Some _) ->
-                       assert False
-                       (* limit should return IE_cont *)
-                   | (I.IE_cont (Some _) _, _, _) ->
-                       assert False
-                       (* joinI should raise this error *)
-                   | (I.IE_done str, _, None)
-                   | (I.IE_cont None _, str, None) ->
-                       let () = assert
-                         (String.length str <= max_num_digits) in
-                       match from_dec_neg ~maxstr str with
-                       [ `Not_a_number -> assert False
-                       | `Empty -> assert False
-                       | `Ok r -> I.return (r * (-sign))
-                       | `Overflow -> I.throw_err SInt_overflow
-                       ]
-
-                   | (IE_cont _ _, _, Some _) ->
-                       let () = dbgn "(read_gen: cont/digit) %!" in
-                       I.throw_err SInt_overflow
-                   ]
-              ]
-            ;
-
-            value read_unsigned_gen ~allow0 =
-              read_gen
-                ~allow0
-                ~max_num_digits:max_int_digits
-                ~sign:one
-                ~maxstr:max_int_dec_abs
-            ;
-
-            value read_negative_gen ~allow0 =
-              read_gen
-                ~allow0
-                ~max_num_digits:min_int_digits
-                ~sign:minus_one
-                ~maxstr:min_int_dec_abs
-            ;
-
-            value read_signed_gen
-              ~allow0
-             :
-              iteratee char S.t
-             =
-              peek >>= fun
-              [ Some '-' -> junk >>= fun () -> read_negative_gen ~allow0
-              | Some '+' -> junk >>= fun () -> read_unsigned_gen ~allow0
-              | _ -> read_unsigned_gen ~allow0
-              ]
-            ;
-
-          end
-        ;
-
-        module SInt = SInt_F(SInt_T)
-        ;
-
-        module SInt32 = SInt_F(SInt32_T)
-        ;
-
-        module SInt64 = SInt_F(SInt64_T)
-        ;
-
-        value read_uint_nz = SInt.read_unsigned_gen ~allow0:False;
-        value read_uint = SInt.read_unsigned_gen ~allow0:True;
-        value read_int_nz = SInt.read_signed_gen ~allow0:False;
-        value read_int = SInt.read_signed_gen ~allow0:True;
-
-        value read_uint32_nz = SInt32.read_unsigned_gen ~allow0:False;
-        value read_uint32 = SInt32.read_unsigned_gen ~allow0:True;
-        value read_int32_nz = SInt32.read_signed_gen ~allow0:False;
-        value read_int32 = SInt32.read_signed_gen ~allow0:True;
-
-        value read_uint64_nz = SInt64.read_unsigned_gen ~allow0:False;
-        value read_uint64 = SInt64.read_unsigned_gen ~allow0:True;
-        value read_int64_nz = SInt64.read_signed_gen ~allow0:False;
-        value read_int64 = SInt64.read_signed_gen ~allow0:True;
-
-      end
-    ;
-    (* / todo: move to ocaml-iteratees *)
-
-    open It_add
-    ;
-
     open Printf
     ;
 
     module IO = I.It_IO;
     open I.Ops;
 
-
-    value read_uint = It_add.read_uint;
-    value read_uint_nz = It_add.read_uint_nz;
-    value read_int = It_add.read_int;
-    value read_int_nz = It_add.read_int_nz;
-
-    value read_uint32 = It_add.read_uint32;
-    value read_uint32_nz = It_add.read_uint32_nz;
-    value read_int32 = It_add.read_int32;
-    value read_int32_nz = It_add.read_int32_nz;
-
-    value read_uint64 = It_add.read_uint64;
-    value read_uint64_nz = It_add.read_uint64_nz;
-    value read_int64 = It_add.read_int64;
-    value read_int64_nz = It_add.read_int64_nz;
-
-
     value itpair_anyresult_lasterror it1 it2 =
-      itlist_anyresult_lasterror [it1; it2]
+      I.itlist_anyresult_lasterror [it1; it2]
     ;
 
-
     exception Dumbstreaming of string
     ;
 
 
       let read_len
        : I.iteratee char int
-       = read_space >>= fun () -> read_uint
+       = read_space >>= fun () -> I.read_uint
       in
 
       let enumerate_over_lens
     ;
 
     value read_some ~get_piece_it ~combine_it : I.iteratee _ _ =
-      read_uint64 >>= fun packet_length ->
+      I.read_uint64 >>= fun packet_length ->
       let read_data_gen = read_data ~get_piece_it ~combine_it
       in
       let read_ver1 () =
         read_data_gen ~totalsize:packet_length ~lens:[pl]
 
       and read_ver2 () =
-        read_uint >>= fun count ->
+        I.read_uint >>= fun count ->
         let () = assert (count >= 0) in
         read_lengths count >>= fun lengths ->
         let lengths_count = List.length lengths in

dumbstreaming_it.mli

     ;
 
 
-
-    (* for tests: *)
-    value read_uint : I.iteratee char int;
-    value read_uint_nz : I.iteratee char int;
-    value read_int : I.iteratee char int;
-    value read_int_nz : I.iteratee char int;
-
-    value read_uint32 : I.iteratee char int32;
-    value read_uint32_nz : I.iteratee char int32;
-    value read_int32 : I.iteratee char int32;
-    value read_int32_nz : I.iteratee char int32;
-
-    value read_uint64 : I.iteratee char int64;
-    value read_uint64_nz : I.iteratee char int64;
-    value read_int64 : I.iteratee char int64;
-    value read_int64_nz : I.iteratee char int64;
-    (* . *)
-
-
     (* for tests: *)
     value read_lengths : int -> I.iteratee char (list int)
     ;
 value ( % ) f g = fun x -> f (g x);
 module IO = I.It_IO;
 
+
+value gather_to_string = I.break_chars (fun _ -> False)
+;
+
 value enum1 s i =
   match i with
   [ I.IE_cont None k -> k s >>% IO.return % fst
   ]
 ;
 
-value test_int (reader : I.iteratee char int32) string =
-  let stream = I.Chunk (I.Subarray.of_string string) in
-  let () = Printf.printf "%S -> %!" string in
-  match IO.runIO ((enum1 stream reader) >>% I.run) with
-  [ `Ok r -> Printf.printf "ok %li\n%!" r
-  | `Error e -> Printf.printf "error \"%s\"\n%!"
-      (printexc e)
-  ]
-;
-
-
-open Printf
-;
-
-
-value () =
-  let max_int = Int64.of_int32 Int32.max_int in
-  let pr = Printf.sprintf "%Li" in
-  let samples_u =
-    [ "0"
-    ; "00"
-    ; "123"
-    ; "+123"
-    ; "0123"
-    ; "-123"
-    ; pr max_int
-    ; "000000" ^ pr max_int
-    ; pr (Int64.add 1L max_int)
-    ; pr (Int64.add 2L max_int)
-    ; "19223372036854775806"
-    ]
-  in
-  let samples = List.concat
-    [ samples_u
-    ; List.map (fun s -> "-" ^ s) samples_u
-    ; List.map (fun s -> "+" ^ s) samples_u
-    ]
-  in
-    ( printf "reading unsigned int32 with leading zeroes allowed:\n"
-    ; List.iter (test_int D.read_uint32) samples
-    ; print_newline ()
-    ; printf "reading unsigned int32 with leading zeroes forbidden:\n"
-    ; List.iter (test_int D.read_uint32_nz) samples
-    ; print_newline ()
-
-    ; printf "reading signed int32 with leading zeroes allowed:\n"
-    ; List.iter (test_int D.read_int32) samples
-    ; print_newline ()
-    ; printf "reading signed int32 with leading zeroes forbidden:\n"
-    ; List.iter (test_int D.read_int32_nz) samples
-    ; print_newline ()
-    )
-;
-
-
-
-
-value gather_to_string = I.break_chars (fun _ -> False)
-;
-
-
 value test_read_lengths count string =
   let stream = I.Chunk (I.Subarray.of_string string) in
   let () = Printf.printf "%S -> %!" string in