Date: Tue, 14 Feb 2006 22:25:03 +0100 From: Ulrich Spoerlein <q@galgenberg.net> To: hackers@freebsd.org Subject: Naive implementation of strverscmp(3) Message-ID: <20060214212503.GE1107@galgenberg.net>
next in thread | raw e-mail | index | archive | help
--EDJsL2R9iCFAt7IV Content-Type: multipart/mixed; boundary="kbCYTQG2MZjuOjyn" Content-Disposition: inline --kbCYTQG2MZjuOjyn Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello Hackers, this is probably not the right list, but I'd like to collect reviews of=20 a strverscmp(3) function I wrote. It is used by the graphics/gqview port=20 (if present in libc) and since I want/need that functionality I whipped=20 up a somewhat working version. It tries to sort strings like "jan1", "jan2", "jan10", etc. into the=20 "natural" order. Is there a chance this might get included into libc? Or is it considered=20 bloat? The GNU version can be found here http://refspecs.freestandards.org/LSB_2.0.1/LSB-generic/LSB-generic/baselib= -strverscmp.html Quite frankly, I don't understand their integral/fraction distinction,=20 and my version differs in that regard. See the return values of the=20 attached sample code. Ulrich Spoerlein --=20 PGP Key ID: 20FEE9DD Encrypted mail welcome! Fingerprint: AEC9 AF5E 01AC 4EE1 8F70 6CBD E76E 2227 20FE E9DD Which is worse: ignorance or apathy? Don't know. Don't care. --kbCYTQG2MZjuOjyn Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="strverscmp.c" Content-Transfer-Encoding: quoted-printable #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #ifdef __FreeBSD__ int strverscmp(const char *s1, const char *s2); int strverscmp(const char *s1, const char *s2) { static const char *digits =3D "0123456789"; int ret; long n1, n2; size_t p1, p2; do { p1 =3D strcspn(s1, digits); p2 =3D strcspn(s2, digits); =20 /* Different prefix */ if ((ret =3D strncmp(s1, s2, p1)) !=3D 0) return ret; s1 +=3D p1; s2 +=3D p2; n1 =3D strtol(s1, NULL, 10); n2 =3D strtol(s2, NULL, 10); =20 if (n1 < n2) return -1; else if (n1 > n2) return 1; /* Numbers are equal or not present, try with next ones. */ p1 =3D strspn(s1, digits); p2 =3D strspn(s2, digits); s1 +=3D p1; s2 +=3D p2; } while (p1 =3D=3D p2 && p1 !=3D 0 && p1 !=3D 0); =20 return strcmp(s1, s2); } #endif int main(int argc, char **argv) { char **array, *temp; int i, j, n =3D 10; array =3D (char **) calloc(n, sizeof(char *)); array[0] =3D strdup("jan2"); array[1] =3D strdup("jan10"); array[2] =3D strdup("jan"); array[3] =3D strdup("jan0a"); array[4] =3D strdup("jan17b"); array[5] =3D strdup("jan17a"); array[6] =3D strdup("jan17x1234"); array[7] =3D strdup("jan17x123"); array[8] =3D strdup("jan17x123a"); array[9] =3D strdup("jan9"); /* Bubble sort */ for (i=3D0; i<n; ++i) { for (j=3Di; j<n; ++j) { if (strverscmp(array[i], array[j]) > 0) { temp =3D array[j]; array[j] =3D array[i]; array[i] =3D temp; } } } for (i=3D0; i<n; ++i) { printf("%s\n", array[i]); } printf("%d\n", strverscmp("a", "a")); printf("%d\n", strverscmp("item#99", "item#100")); printf("%d\n", strverscmp("alpha1", "alpha001")); printf("%d\n", strverscmp("part1_f012", "part1_f01")); printf("%d\n", strverscmp("foo.009", "foo.0")); return 0; } --kbCYTQG2MZjuOjyn-- --EDJsL2R9iCFAt7IV Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (FreeBSD) iD8DBQFD8kqv524iJyD+6d0RAsbZAKCKBHkUK8cod2EdbOB3CLqdCTIE2QCfckpD yVuxZuyhNC/UfOA4gT3IFXg= =A2pU -----END PGP SIGNATURE----- --EDJsL2R9iCFAt7IV--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060214212503.GE1107>