Date: Wed, 15 Feb 2006 19:05:32 +1100 From: Peter Jeremy <peterjeremy@optushome.com.au> To: hackers@freebsd.org Subject: Re: Naive implementation of strverscmp(3) Message-ID: <20060215080532.GB684@turion.vk2pj.dyndns.org> In-Reply-To: <20060214212503.GE1107@galgenberg.net> References: <20060214212503.GE1107@galgenberg.net>
next in thread | previous in thread | raw e-mail | index | archive | help
[-- Attachment #1 --] On Tue, 2006-Feb-14 22:25:03 +0100, Ulrich Spoerlein wrote: >this is probably not the right list, but I'd like to collect reviews of >a strverscmp(3) function I wrote. It is used by the graphics/gqview port ... >Is there a chance this might get included into libc? Or is it considered >bloat? I don't think it belongs in libc. Maybe libutil. >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, I don't think their description makes sense - it's not clear what the point is or where it would be used. If '.' was also a magic character then (IMHO) it would make more sense. >int >strverscmp(const char *s1, const char *s2) >{ > static const char *digits = "0123456789"; > int ret; > long n1, n2; > size_t p1, p2; > > do { > p1 = strcspn(s1, digits); > p2 = strcspn(s2, digits); > + if (p1 != p2) break; + > /* Different prefix */ > if ((ret = strncmp(s1, s2, p1)) != 0) > return ret; ... > /* Numbers are equal or not present, try with next ones. */ > p1 = strspn(s1, digits); > p2 = strspn(s2, digits); You can avoid these strspn() calls [which are not cheap] by saving the endptr values from the strtol() calls above. > s1 += p1; > s2 += p2; - } while (p1 == p2 && p1 != 0 && p1 != 0); + } while (p1 == p2 && p1 != 0); For the first point, consider strverscmp("jan25", "janx25"); -- Peter Jeremy [-- Attachment #2 --] -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (FreeBSD) iD8DBQFD8uDK/opHv/APuIcRAhugAJ4ibgn65igh77GBpMI1IVeYci6p1wCeLEXp +iAYRxDwLLK1thDEgoCHbco= =bgEQ -----END PGP SIGNATURE-----
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060215080532.GB684>
