xref: /freebsd/lib/libc/string/strverscmp.3 (revision 09ec5e67a7a6605e5a58a2e3e82dab243a90609a)
105c9a015SAymeric Wibo.\" SPDX-License-Identifier: BSD-2-Clause
205c9a015SAymeric Wibo.\" Copyright (c) 2022 Aymeric Wibo <obiwac@gmail.com>
3*09ec5e67SMina Galić.Dd August 31, 2023
405c9a015SAymeric Wibo.Dt STRVERSCMP 3
505c9a015SAymeric Wibo.Os
605c9a015SAymeric Wibo.Sh NAME
705c9a015SAymeric Wibo.Nm strverscmp
805c9a015SAymeric Wibo.Nd compare strings according to natural order
905c9a015SAymeric Wibo.Sh LIBRARY
1005c9a015SAymeric Wibo.Lb libc
1105c9a015SAymeric Wibo.Sh SYNOPSIS
1205c9a015SAymeric Wibo.In string.h
1305c9a015SAymeric Wibo.Ft int
1405c9a015SAymeric Wibo.Fn strverscmp "const char *s1" "const char *s2"
1505c9a015SAymeric Wibo.Sh DESCRIPTION
1605c9a015SAymeric WiboThe
1705c9a015SAymeric Wibo.Fn strverscmp
1805c9a015SAymeric Wibofunction
1905c9a015SAymeric Wibocompares the null-terminated strings
2005c9a015SAymeric Wibo.Fa s1
2105c9a015SAymeric Wiboand
2205c9a015SAymeric Wibo.Fa s2
2305c9a015SAymeric Wiboaccording to their natural order
2405c9a015SAymeric Wiboand returns an integer greater than, equal to, or less than 0,
2505c9a015SAymeric Wibodepending on whether
2605c9a015SAymeric Wibo.Fa s1
2705c9a015SAymeric Wibois greater than, equal to, or less than
2805c9a015SAymeric Wibo.Fa s2 .
2905c9a015SAymeric Wibo.Pp
3005c9a015SAymeric WiboMore specifically, this natural order is found by iterating over both
3105c9a015SAymeric Wibostrings until a difference is found.
3205c9a015SAymeric WiboIf the difference is between non-decimal characters,
3305c9a015SAymeric Wibo.Fn strverscmp
3405c9a015SAymeric Wiboacts like
3505c9a015SAymeric Wibo.Xr strcmp 3
3605c9a015SAymeric Wibo(thus, the ordering would be "a", "b", "train").
3705c9a015SAymeric WiboIf a decimal digit is found, the whole number is read and compared
3805c9a015SAymeric Wibo(thus, the ordering would be "9", "10", "420" which is different to lexicographic order,
3905c9a015SAymeric Wibowhat
4005c9a015SAymeric Wibo.Xr strcmp 3
4105c9a015SAymeric Wibowould have done).
4205c9a015SAymeric WiboNumbers with leading zeroes are interpreted as fractional parts (even without a decimal point),
4305c9a015SAymeric Wiboand numbers with more leading zeroes are placed before numbers with fewer leading zeroes
4405c9a015SAymeric Wibo(thus, the ordering would be "000", "00", "01", "010", "09", "0", "1", "9", "10").
4505c9a015SAymeric Wibo.Sh SEE ALSO
4605c9a015SAymeric Wibo.Xr strcmp 3 ,
4705c9a015SAymeric Wibo.Xr versionsort 3
4805c9a015SAymeric Wibo.Sh STANDARDS
4905c9a015SAymeric WiboThe
5005c9a015SAymeric Wibo.Fn strverscmp
5105c9a015SAymeric Wibofunction is a GNU extension and conforms to no standard.
5205c9a015SAymeric Wibo.Sh HISTORY
5305c9a015SAymeric WiboThe
5405c9a015SAymeric Wibo.Fn strverscmp
5505c9a015SAymeric Wibofunction was added in
56*09ec5e67SMina Galić.Fx 13.2 .
57