]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/mathql/mQueryUtil.ml
while construction inserted
[helm.git] / helm / ocaml / mathql / mQueryUtil.ml
index 22d1f91e5d27bf53d0920840063e62f2408f745e..1051e2a7e63113c0c1b569ea347b09caed804018 100644 (file)
@@ -77,3 +77,29 @@ let rec add_assoc ap = function
    | []                                  -> [ap]
    | head :: tail when fst head = fst ap -> ap :: tail
    | head :: tail                        -> head :: add_assoc ap tail
+
+(* int of string ************************************************************)
+
+type t = End
+       | Space
+       | Figure of int
+       | Error
+
+let int_of_string s =
+   let l = String.length s in
+   let get_t i =
+      if i = l then End else
+      match s.[i] with
+         | ' ' | '\t' | '\r' | 'n' -> Space
+        | '0' .. '9'              -> Figure (Char.code s.[i] - Char.code '0')
+        | _                       -> Error
+   in
+   let rec aux i xv = match get_t i, xv with
+      | Error, _ 
+      | End, None        -> raise (Failure "int_of_string") 
+      | End, Some v      -> v
+      | Space, xv        -> aux (succ i) xv
+      | Figure f, None   -> aux (succ i) (Some f)
+      | Figure f, Some v -> aux (succ i) (Some (10 * v + f))
+   in
+   aux 0 None