xref: /freebsd/lib/libc/string/strverscmp.3 (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1.\" SPDX-License-Identifier: BSD-2-Clause
2.\" Copyright (c) 2022 Aymeric Wibo <obiwac@gmail.com>
3.Dd July 11, 2022
4.Dt STRVERSCMP 3
5.Os
6.Sh NAME
7.Nm strverscmp
8.Nd compare strings according to natural order
9.Sh LIBRARY
10.Lb libc
11.Sh SYNOPSIS
12.In string.h
13.Ft int
14.Fn strverscmp "const char *s1" "const char *s2"
15.Sh DESCRIPTION
16The
17.Fn strverscmp
18function
19compares the null-terminated strings
20.Fa s1
21and
22.Fa s2
23according to their natural order
24and returns an integer greater than, equal to, or less than 0,
25depending on whether
26.Fa s1
27is greater than, equal to, or less than
28.Fa s2 .
29.Pp
30More specifically, this natural order is found by iterating over both
31strings until a difference is found.
32If the difference is between non-decimal characters,
33.Fn strverscmp
34acts like
35.Xr strcmp 3
36(thus, the ordering would be "a", "b", "train").
37If a decimal digit is found, the whole number is read and compared
38(thus, the ordering would be "9", "10", "420" which is different to lexicographic order,
39what
40.Xr strcmp 3
41would have done).
42Numbers with leading zeroes are interpreted as fractional parts (even without a decimal point),
43and numbers with more leading zeroes are placed before numbers with fewer leading zeroes
44(thus, the ordering would be "000", "00", "01", "010", "09", "0", "1", "9", "10").
45.Sh SEE ALSO
46.Xr strcmp 3 ,
47.Xr versionsort 3
48.Sh STANDARDS
49The
50.Fn strverscmp
51function is a GNU extension and conforms to no standard.
52.Sh HISTORY
53The
54.Fn strverscmp
55function was added in
56.Fx 14.0 .
57