Malcolm McLean <(E-Mail Removed)> writes:

> (But I do wonder if Microsoft nicked my compnumeric function which

> sorts strings with embedded numbers. Did they generate the idea

> independently? I'd really like to know.)
Unless you think that GNU also "nicked" it from you, there's the

strverscmp function as a third independent invention:

-- Function: int strverscmp (const char *S1, const char *S2)

The `strverscmp' function compares the string S1 against S2,

considering them as holding indices/version numbers. Return value

follows the same conventions as found in the `strverscmp'

function. In fact, if S1 and S2 contain no digits, `strverscmp'

behaves like `strcmp'.

Basically, we compare strings normally (character by character),

until we find a digit in each string - then we enter a special

comparison mode, where each sequence of digits is taken as a

whole. If we reach the end of these two parts without noticing a

difference, we return to the standard comparison mode. There are

two types of numeric parts: "integral" and "fractional" (those

begin with a '0'). The types of the numeric parts affect the way

we sort them:

* integral/integral: we compare values as you would expect.

* fractional/integral: the fractional part is less than the

integral one. Again, no surprise.

* fractional/fractional: the things become a bit more complex.

If the common prefix contains only leading zeroes, the

longest part is less than the other one; else the comparison

behaves normally.

strverscmp ("no digit", "no digit")

=> 0 /* same behavior as strcmp. */

strverscmp ("item#99", "item#100")

=> <0 /* same prefix, but 99 < 100. */

strverscmp ("alpha1", "alpha001")

=> >0 /* fractional part inferior to integral one. */

strverscmp ("part1_f012", "part1_f01")

=> >0 /* two fractional parts. */

strverscmp ("foo.009", "foo.0")

=> <0 /* idem, but with leading zeroes only. */

This function is especially useful when dealing with filename

sorting, because filenames frequently hold indices/version numbers.

`strverscmp' is a GNU extension.

--

char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]

={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p

=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+

2:{i++;if(i)break;else default:continue;if(0)case 1

utchar(a[i&15]);break;}}}