(************************************************************************)
(*         *      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 hexadecimal numbers and Coq strings *)
(** Pretty straightforward, which is precisely the point of the [Hexadecimal.int] datatype. The only catch is [Hexadecimal.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.  Hexadecimal digits are lower case ('a'..'f'). We ignore upper case digits ('A'..'F') for the sake of simplicity. *)
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)
| "a" => Some (Da d)
| "b" => Some (Db d)
| "c" => Some (Dc d)
| "d" => Some (Dd d)
| "e" => Some (De d)
| "f" => Some (Df 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 \/
c = "a" /\ d' = Da d \/
c = "b" /\ d' = Db d \/
c = "c" /\ d' = Dc d \/
c = "d" /\ d' = Dd d \/
c = "e" /\ d' = De d \/
c = "f" /\ d' = Df d)%char.