QCaml/Utils/Range.ml

47 lines
984 B
OCaml
Raw Normal View History

2019-02-20 18:15:15 +01:00
type t = int list
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 ]
| _ ->
assert (s.[0] = '[') ;
assert (s.[(String.length s)-1] = ']') ;
let s = String.sub s 1 ((String.length s) - 2) in
2019-03-21 16:32:41 +01:00
let l = String.split_on_char ',' s in
2019-02-20 18:15:15 +01:00
let l = List.map expand_range l in
List.concat l
|> List.sort_uniq compare
let to_string l =
"[" ^
(List.map string_of_int l
|> String.concat ",") ^
"]"
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)
2019-02-20 18:15:15 +01:00