10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-12-23 21:03:33 +01:00
QCaml/common/lib/range.ml

52 lines
1.0 KiB
OCaml
Raw Normal View History

2024-01-17 13:59:05 +01:00
(** Type *)
2019-02-20 18:15:15 +01:00
type t = int list
2024-01-17 13:59:05 +01:00
(** Conversion *)
2019-02-20 18:15:15 +01:00
let to_int_list r = r
let expand_range r =
match String.split_on_char '-' r with
| s :: f :: [] ->
begin
let start = int_of_string s
and finish = int_of_string f
in
assert (start <= finish) ;
let rec do_work = function
| i when i=finish -> [ i ]
| i -> i::(do_work (i+1))
in do_work start
end
2019-03-21 16:32:41 +01:00
| r :: [] -> [int_of_string r]
2019-02-20 18:15:15 +01:00
| [] -> []
| _ -> invalid_arg "Only one range expected"
let of_string s =
match s.[0] with
| '0' .. '9' -> [ int_of_string s ]
| _ ->
2020-12-28 01:08:55 +01:00
assert (s.[0] = '[') ;
assert (s.[(String.length s)-1] = ']') ;
let s = String.sub s 1 ((String.length s) - 2) in
let l = String.split_on_char ',' s in
let l = List.map expand_range l in
List.concat l
|> List.sort_uniq compare
2019-02-20 18:15:15 +01:00
let to_string l =
"[" ^
(List.map string_of_int l
|> String.concat ",") ^
"]"
2024-01-17 13:59:05 +01:00
(** Printers *)
2019-12-02 14:58:48 +01:00
let pp ppf t =
2019-03-21 16:32:41 +01:00
Format.fprintf ppf "@[%s@]" (to_string t)