strverscmp -- compare strings holding name and indices/version numbers


#include <string.h>

int strverscmp(const char *s1, const char *s2);


The strversmp function shall compare two strings in a similar manner to strcmp. If s1 and s2 contain no digits, strversmp shall behave as strcmp.

The strings are compared by scanning from left to right. If a digit or sequence of digits is encountered in both strings at the same position, the digit sequence is specially compared, as described below. If the digit sequences compared equal, the string comparison resumes in both s1 and s2 after the digit sequence.

Digit sequences are classified as either "integral" or "fractional". A fractional digit sequence begins with a '0'; otherwise the digit sequence shall be treated as an integral digit sequence.

If two integral digit sequences are encountered, they shall be compared as integers for equality. A fractional digit sequence shall always compare less than an integral digit sequence. If two fractional digit sequences are being compared, then if the common prefix contains only leading zeroes, the longer part shall compare less than the shorter; otherwise the comparison shall be strictly numeric.


Table 7-1. Examples

CallReturn Value
strverscmp( "no digit", "no digit")0 /* same behavior as strcmp */
strverscmp( "item#99", "item#100")< 0 /* same prefix, but 99 < 100 */
strverscmp( "alpha1", "alpha001")> 0 /* fractional part inferior to integral */
strverscmp( "part1_f012", "part1_f01")> 0 /* two fractional parts */
strverscmp( "foo.009", "foo.0")< 0 /* two fractional parts but with leading zeroes only */