package numeric_string

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type t = Base.string

Numeric string comparison, also sometimes called "natural comparison", is the string ordering that orders numeric subcomponents as numbers, so that e.g. "xyz2" comes before "xyz10", rather than after (as it does when they are compared as plain strings). As a spec this is incomplete, and various implementations around the web fill in the missing details differently. This one errs on the side of doing less, rather than more, and in particular doesn't do any special handling of minus signs or decimal points.

Precisely: first, split both input strings into a possibly-empty initial segment of non-numeric characters, followed by alternating non-empty segments of numeric characters and non-numeric characters. Then compare corresponding segments, using string comparison on non-numeric segments and integer comparison on numeric segments; if you run out of segments on one side, the shorter segment list compares smaller. (Allowing the initial non-numeric segment to be empty ensures we always compare corresponding segments of the same kind).

Leading zeroes in numeric components complicate the above spec a little, since they mean two distinct strings can have equal numeric value. The choice we make is to sort by numeric value first, but break ties by string length, so that e.g. "1" < "01" < "2" < "02". This ensures that numeric string equality coincides with ordinary string equality, so that e.g. sets and maps of strings behave as you'd expect (although they have distinct serializations / orderings, so they're not identical). See the README for more discussion.

The comparison functions do not allocate, so that they can be used in as wide a variety of contexts as possible.

include Base.Comparable.S with type t := t
val (>=) : t -> t -> bool
val (<=) : t -> t -> bool
val (=) : t -> t -> bool
val (>) : t -> t -> bool
val (<) : t -> t -> bool
val (<>) : t -> t -> bool
val equal : t -> t -> bool
val compare : t -> t -> int
val min : t -> t -> t
val max : t -> t -> t
val ascending : t -> t -> int
val descending : t -> t -> int
val between : t -> low:t -> high:t -> bool
val clamp_exn : t -> min:t -> max:t -> t
val clamp : t -> min:t -> max:t -> t Base__.Or_error.t
type comparator_witness
val comparator : (t, comparator_witness) Base__Comparator.comparator
include Base.Sexpable.S with type t := t
val t_of_sexp : Sexplib0__.Sexp.t -> t
val sexp_of_t : t -> Sexplib0__.Sexp.t
OCaml

Innovation. Community. Security.