From 8462b89bdfee61527ba8cabd0160b7786c70c828 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 30 Jan 2017 14:46:13 +0100 Subject: [PATCH] add `Char.{of_int{,_exn},to_int}` (close #95) --- src/core/CCChar.ml | 4 ++++ src/core/CCChar.mli | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/core/CCChar.ml b/src/core/CCChar.ml index 07905d8f..848594a0 100644 --- a/src/core/CCChar.ml +++ b/src/core/CCChar.ml @@ -12,6 +12,10 @@ let compare = Char.compare let pp = Buffer.add_char let print = Format.pp_print_char +let of_int_exn = Char.chr +let of_int c = try Some (of_int_exn c) with _ -> None +let to_int = Char.code + let lowercase_ascii c = if c >= 'A' && c <= 'Z' then Char.unsafe_chr (Char. code c + 32) diff --git a/src/core/CCChar.mli b/src/core/CCChar.mli index 33e768e7..351e2617 100644 --- a/src/core/CCChar.mli +++ b/src/core/CCChar.mli @@ -17,5 +17,18 @@ val uppercase_ascii : t -> t (** See {!Char} @since 0.20 *) +val of_int_exn : int -> t +(** Alias to {!Char.chr} + @raise Invalid_argument if the int is not within [0,...,255] + @since NEXT_RELEASE *) + +val of_int : int -> t option +(** Safe version of {!of_int} + @since NEXT_RELEASE *) + +val to_int : t -> int +(** Alias to {!Char.code} + @since NEXT_RELEASE *) + val pp : Buffer.t -> t -> unit val print : Format.formatter -> t -> unit