(************************************************************************)
(*         *      The Rocq Prover / The Rocq Development Team           *)
(*  v      *         Copyright INRIA, CNRS and contributors             *)
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
(*   \VV/  **************************************************************)
(*    //   *    This file is distributed under the terms of the         *)
(*         *     GNU Lesser General Public License Version 2.1          *)
(*         *     (see LICENSE file for the text of the license)         *)
(************************************************************************)
(** * Conversion between decimal numbers and Coq strings *)
(** Pretty straightforward, which is precisely the point of the [Decimal.int] datatype. The only catch is [Decimal.Nil] : we could choose to convert it as [""] or as ["0"]. In the first case, it is awkward to consider "" (or "-") as a number, while in the second case we don't have a perfect bijection. Since the second variant is implemented thanks to the first one, we provide both. *)
Local Open Scope string_scope.
(** Parsing one char *)
Definition uint_of_char (a:ascii)(d:option uint) :=
match d with
| None => None
| Some d =>
match a with
| "0" => Some (D0 d)
| "1" => Some (D1 d)
| "2" => Some (D2 d)
| "3" => Some (D3 d)
| "4" => Some (D4 d)
| "5" => Some (D5 d)
| "6" => Some (D6 d)
| "7" => Some (D7 d)
| "8" => Some (D8 d)
| "9" => Some (D9 d)
| _ => None
end
end%char.

Lemma uint_of_char_spec c d d' :
uint_of_char c (Some d) = Some d' ->
(c = "0" /\ d' = D0 d \/
c = "1" /\ d' = D1 d \/
c = "2" /\ d' = D2 d \/
c = "3" /\ d' = D3 d \/
c = "4" /\ d' = D4 d \/
c = "5" /\ d' = D5 d \/
c = "6" /\ d' = D6 d \/
c = "7" /\ d' = D7 d \/
c = "8" /\ d' = D8 d \/
c = "9" /\ d' = D9 d)%char.