Commits

Anonymous committed caaa47a

(enum_string, Reading_num) -> ocaml-iteratees

Comments (0)

Files changed (1)

src/dbi/decimal.ml

   struct
     module IO = Pure_IO;
     module I = Iteratees.Make(IO);
-
-    (* TODO: move to ocaml-iteratees *)
-    module It_add
-     :
-      sig
-        value num_of_string_fix : string -> IO.m Num.num;
-      end
-     =
-      struct
-        open I
-        ;
-        open I.Ops
-        ;
-        module IO = It_IO
-        ;
-
-
-        (* [Enum_string_partial] is raised by [enum_string ~strict:True]
-           when iteratee has returned result without reading the whole
-           string.
-        *)
-        exception Enum_string_partial
-        ;
-
-        value enum_string
-          ?(chunk_size=4096)
-          str
-         :
-          enumerator char 'a
-         =
-          let str_len = String.length str in
-          let arr_sz = min str_len chunk_size in
-          let arr = Array.make arr_sz '\x00' in
-          let fill_array ofs : int =
-            let ilen = min arr_sz (str_len - ofs) in
-            let imax = ilen - 1 in
-            ( for i = 0 to imax do
-                ( arr.(i) := str.[ofs + i]
-                )
-              done
-            ; ilen
-            )
-          in
-          inner 0
-          where rec inner ofs it =
-            match it with
-            [ IE_done _ | IE_cont (Some _) _ -> IO.return it
-            | IE_cont None k ->
-                let len = fill_array ofs in
-                if len = 0
-                then
-                  IO.return it
-                else
-                  k (Chunk (S.of_array_sub arr 0 len)) >>% fun (it, _s) ->
-                  inner (ofs + len) it
-            ]
-        ;
-
-(*
-        value test_enum_string : unit =
-         ignore (IO.runIO (
-          (enum_string ~chunk_size:3 "abcdefg" (break_chars (fun _ -> False)))
-          >>% fun it ->
-          (I.run it)
-          >>% fun res -> (failwith "res=%s" res ; IO.return ())
-         ))
-        ;
-*)
-
-        exception ENum of string
-        ;
-
-        value num_err msg = throw_err (ENum msg)
-        ;
-
-        value is_decimal_point = fun [ '.' | ',' -> True | _ -> False ]
-        ;
-
-        value is_digit c = (c <= '9' && c >= '0')
-          and is_not_digit c = (c < '0' || c > '9')
-        ;
-
-        value num_fix_unsigned =
-          let ten = Num.num_of_int 10 in
-          break_chars is_not_digit
-          >>= fun before_point ->
-          peek
-          >>= fun optc ->
-          match optc with
-          [ Some c when is_decimal_point c ->
-              junk >>= fun () ->
-              return (String.make 1 c)
-          | _ ->
-              return ""
-          ]
-          >>= fun point ->
-          break_chars is_not_digit >>= fun after_point ->
-          if before_point = "" && after_point = ""
-          then
-            if point = ""
-            then
-              num_err "not a number (no digits, no decimal point)"
-            else
-              num_err & Printf.sprintf
-                "decimal number can't consist of %S only"
-                point
-          else
-            let scale = String.length after_point in
-            return &
-            Num.div_num
-              (Num.num_of_string (before_point ^ after_point))
-              (Num.power_num ten (Num.num_of_int scale))
-        ;
-
-
-        value num_fix =
-          let num_negate = fun n -> Num.mult_num (Num.num_of_int (-1)) n in
-
-          peek
-          >>= fun optc ->
-          match optc with
-          [ None -> num_err "EOF while reading number"
-          | Some '-' -> junk >>= fun () -> return num_negate
-          | Some '+' -> junk >>= fun () -> return identity
-          | _ -> return identity
-          ]
-          >>= fun make_sign ->
-          num_fix_unsigned >>= fun num_unsigned ->
-          return (make_sign num_unsigned)
-        ;
-
-        value is_whitespace = fun [ ' ' | '\t' -> True | _ -> False ]
-        ;
-
-        value num_of_string_fix str : IO.m Num.num =
-          (I.run %<< enum_string str
-            (I.drop_while is_whitespace >>= fun () ->
-             num_fix >>= fun r ->
-             peek >>= fun
-             [ None -> return r
-             | Some c -> num_err (Printf.sprintf
-                 "garbage after number: char %C" c)
-             ]
-            )
-          )
-        ;
-
-      end
-    ;
-
-    open It_add
-    ;
-
-(*
-    value read_num ;
-*)
+    module NN = I.Reading_num(Num);
+    include NN;
 
     open I.Ops
     ;
 
     value num_of_string_fix str =
-      match Pure_IO.runIO (It_add.num_of_string_fix str) with
+      match Pure_IO.runIO (num_of_string_fix str) with
       [ `Ok r -> r
       | `Error e ->
           raise & match e with
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.