Naive implementation of strverscmp(3)
Dan Nelson
dnelson at allantgroup.com
Wed Feb 15 08:05:26 PST 2006
In the last episode (Feb 15), Ulrich Spoerlein said:
> Peter Jeremy wrote:
> >>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.
>
> This would require patching the gqview configure script, but I can live
> with that.
>
> >For the first point, consider
> > strverscmp("jan25", "janx25");
>
> This fell victim to a rearranging of the while loop. Thanks for
> pointing that out!
>
> Attached is an updated version, which now also takes leading zeros
> into account. It still differs from the GNU version, because
> strverscmp("foo.009", "foo.0") > 0 In my book, '009' is just greater
> than zero, no matter what. If someone could explain to me, why the
> GNU folks do it the other way round, I could try implementing that
> too.
This looks a lot like strnatcmp, which is "natural sort" or "do what I
mean" sort :)
http://sourcefrog.net/projects/natsort/
Your function is simpler than the C implementation on that site, but
falls over when a run of numbers exceeds 2^31 (raise it to 2^64 if you
use strtoull, but that's as high as you can yet).
--
Dan Nelson
dnelson at allantgroup.com
More information about the freebsd-hackers
mailing list