Commits

Anonymous committed 55f2df2

.

Comments (0)

Files changed (1)

dumbstreaming_it.ml

       ]
     ;
 
+    value return : 'a -> iteratee 'el 'a
+    ;
+
+    value throw_err : err_msg -> iteratee 'el 'a
+    ;
+
+    value peek : iteratee 'el (option 'el)
+    ;
+
     value bindI :
       iteratee 'el 'a -> ('a -> iteratee 'el 'b) -> iteratee 'el 'b
     ;
 
+    value drop_while : ('el -> bool) -> iteratee 'el unit
+    ;
+
+    value break_chars : (char -> bool) -> iteratee char string
+    ;
+
+    type enumeratee 'elo 'eli 'a = 
+      iteratee 'eli 'a -> iteratee 'elo (iteratee 'eli 'a)
+    ;
+
+    value limit : int -> enumeratee 'el 'el 'a
+    ;
+
   end
 ;
 
          : list (I.iteratee 'el 'a) -> I.iteratee 'el 'a
         ;
 
-        (* lead0 = accept leading zeroes or not.  default is true. *)
-        value read_uint30 : ?lead0:bool -> unit -> I.iteratee char int
+        (* leading zeroes accepted *)
+        value read_uint30 : I.iteratee char int
         ;
+
+        (* leading zeroes = error *)
+        value read_uint30nz : I.iteratee char int
+        ;
+
       end
      =
       struct
           ]
         ;
 
+
+        module type SIGNED_INT
+         =
+          sig
+            type t;
+            value max_int : t;
+            (* min_int = -max_int - 1 *)
+            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
+        ;
+
+        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_noleadingzeroes : 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 ten = of_int 10
+            ;
+
+            value rec digits n =
+              Pervasives.( + )
+                1
+                (let n' = n / ten in
+                 if n' =? zero
+                 then 0
+                 else digits n'
+                )
+            ;
+
+            value rem a b = (a - (a / b) * b)
+            ;
+
+            module P = Pervasives;
+
+            value string_reverse_inplace str =
+              let len = String.length str in
+              let len1 = P.( - ) len 1 in
+              let len2 = P.( / ) len 2 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 min_int; *)
+            value max_int_digits = digits max_int;
+
+            value to_base b n =
+              let buf = Buffer.create max_int_digits in
+              let intb = to_int b in
+              let digit n =
+                let n = to_int n in
+                ( assert (n >= 0 && n < intb)
+                ; assert (n < 10)
+                ; Char.chr (P.( + ) n (Char.code '0'))
+                ) in
+              let rec loop n =
+                let d = rem n b in
+                let () = Buffer.add_char buf (digit d) 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 = to_base ten
+            ;
+
+            value max_int_dec = to_dec max_int;
+
+            value from_base_neg b maxstr str =
+              let rec loop acc i =
+                if i < 0
+                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
+                let len = String.length str in
+                if len = 0
+                then `Empty
+                else
+                let maxlen = String.length maxstr in
+                if len > maxlen
+                then `Overflow
+                else if len = maxlen && str > maxstr
+                then `Overflow
+                else
+                  try
+                    `Ok (loop zero (P.( - ) len 1))
+                  with
+                  [ SInt_not_a_number _ -> `Not_a_number ]
+            ;
+
+            value from_dec_neg = from_base_neg ten max_int_dec
+            ;
+
+            value read_unsigned_noleadingzeroes
+             :
+              iteratee char S.t
+             =
+              let is_digit c = (c <= '9' && c >= '0') in
+              (I.limit max_int_digits & I.break_chars is_digit) >>= fun
+              [ I.IE_done str ->
+                  let () = assert (String.length str <= max_int_digits) in
+                  peek >>= fun optch ->
+                  let next_is_digit = match optch with
+                    [ None -> False | Some ch -> is_digit ch ] in
+                  if next_is_digit
+                  then I.throw_err SInt_overflow
+                  else
+                    match from_dec_neg str with
+                    [ `Empty | `Not_a_number -> I.throw_err (SInt_not_a_number
+                        "read_unsigned_noleadingzeroes")
+                    | `Ok r -> I.return (-r)
+                    | `Overflow -> I.throw_err SInt_overflow
+                    ]
+              | I.IE_cont (Some e) _ -> I.throw_err e
+              | I.IE_cont None _ -> I.throw_err SInt_overflow
+              ]
+            ;
+
+          end
+        ;
+
+        module SInt = SInt_F(SInt_T)
+        ;
+
+
+        (* leading zeroes = error *)
+        value read_uint30nz
+         : I.iteratee char int
+         =
+          SInt.read_unsigned_noleadingzeroes
+        ;
+
+        value drop_zeroes = I.drop_while ( ( = ) '0' )
+        ;
+
+        value read_uint30 =
+          drop_zeroes >>= fun () ->
+          read_uint30nz
+        ;
+
       end
     ;
 
       itlist_anyresult_lasterror [it1; it2]
     ;
 
-
-    .
-
-    value read_packet get_piece_it combine_it firstchar =
-      .
+    value read_packet _get_piece_it _combine_it _firstchar =
+      failwith "kva"
     ;
 
 
 
-    value read get_piece_it combine_it =
+    value read _get_piece_it _combine_it =
       
-
+(*
       (some /* mapI */ read_packet get_piece_it combine_it)
       /* itpair_anyresult_lasterror */
       (read_eos >>= fun () -> None)
+*)
+      failwith "kva"
+
     ;
 
   end