Skip to content

Commit

Permalink
Implement sscanf in terms of ksscanf
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
  • Loading branch information
rgrinberg committed Nov 4, 2019
1 parent 5143254 commit 171a297
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/stdune/scanf.ml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module Scanf = Dune_caml.Scanf

let try_ f =
match f () with
| exception End_of_file -> Error ()
let unescaped x =
match Scanf.unescaped x with
| exception Scanf.Scan_failure _ -> Error ()
| exception (Failure _) -> Error ()
| x -> Ok x

let unescaped x = try_ (fun () -> Scanf.unescaped x)

let sscanf x fmt f = try_ (fun () -> Scanf.sscanf x fmt f)
let sscanf x fmt f =
let module M = struct
exception E
end in
match Scanf.ksscanf x (fun _ _ -> raise_notrace M.E) fmt f with
| exception M.E -> Error ()
| x -> Ok x

0 comments on commit 171a297

Please sign in to comment.