mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
add CCString.compare_versions
This commit is contained in:
parent
6cb74b0115
commit
8c9b5601a8
2 changed files with 40 additions and 0 deletions
|
|
@ -198,6 +198,30 @@ module Split = struct
|
|||
else Some (String.sub s 0 i, String.sub s (i+1) (String.length s - i - 1))
|
||||
end
|
||||
|
||||
let compare_versions a b =
|
||||
let of_int s = try Some (int_of_string s) with _ -> None in
|
||||
let rec cmp_rec a b = match a(), b() with
|
||||
| None, None -> 0
|
||||
| Some _, None -> 1
|
||||
| None, Some _ -> -1
|
||||
| Some x, Some y ->
|
||||
match of_int x, of_int y with
|
||||
| None, None ->
|
||||
let c = String.compare x y in
|
||||
if c<>0 then c else cmp_rec a b
|
||||
| Some _, None -> 1
|
||||
| None, Some _ -> -1
|
||||
| Some x, Some y ->
|
||||
let c = Pervasives.compare x y in
|
||||
if c<>0 then c else cmp_rec a b
|
||||
in
|
||||
cmp_rec (Split.gen_cpy ~by:"." a) (Split.gen_cpy ~by:"." b)
|
||||
|
||||
(*$Q
|
||||
Q.(pair printable_string printable_string) (fun (a,b) -> \
|
||||
CCOrd.equiv (compare_versions a b) (CCOrd.opp (compare_versions b a)))
|
||||
*)
|
||||
|
||||
let repeat s n =
|
||||
assert (n>=0);
|
||||
let len = String.length s in
|
||||
|
|
|
|||
|
|
@ -312,6 +312,22 @@ module Split : sig
|
|||
*)
|
||||
end
|
||||
|
||||
(** {2 Utils} *)
|
||||
|
||||
val compare_versions : string -> string -> int
|
||||
(** [compare_versions a b] compares {i version strings} [a] and [b],
|
||||
considering that numbers are above text.
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
(*$T
|
||||
compare_versions "0.1.3" "0.1" > 0
|
||||
compare_versions "10.1" "2.0" > 0
|
||||
compare_versions "0.1.alpha" "0.1" > 0
|
||||
compare_versions "0.3.dev" "0.4" < 0
|
||||
compare_versions "0.foo" "0.0" < 0
|
||||
compare_versions "1.2.3.4" "01.2.4.3" < 0
|
||||
*)
|
||||
|
||||
(** {2 Slices} A contiguous part of a string *)
|
||||
|
||||
module Sub : sig
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue