Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Whip me. Beat me. Make me maintain AIX." (By Stephan Zielinski)


devel / comp.lang.c / Re: Embedded numbers sorting function (begging post)

SubjectAuthor
* Re: Embedded numbers sorting function (begging post)Kaz Kylheku
`- Re: Embedded numbers sorting function (begging post)Kaz Kylheku

1
Re: Embedded numbers sorting function (begging post)

<20220222154422.430@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20521&group=comp.lang.c#20521

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Embedded numbers sorting function (begging post)
Date: Tue, 22 Feb 2022 23:59:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 116
Message-ID: <20220222154422.430@kylheku.com>
References: <0865bc76-8d80-43c1-af79-87c3faf7c9d9n@googlegroups.com>
Injection-Date: Tue, 22 Feb 2022 23:59:20 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="604ccbf5808049cc8404bfb1ad893045";
logging-data="20367"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19kD4AIZbTimBIiPdO2e7UR2b4DB2DaDhQ="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:PP46Vrp38yEBtLvB7tZvxIIQCIE=
 by: Kaz Kylheku - Tue, 22 Feb 2022 23:59 UTC

On 2022-02-22, Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> Anyone got a string comparison function handy which compares strings
> intelligently by embedded numbers, so that foo2 compares earlier than
> foo11 ?

Yes; I just whipped up this piece of simplicity in 15 minutes:

$ ./encmp a1 a11 a2 b b3 c a1z a11z a2z a22z a22n a22p blt b4
a1
a1z
a2
a2z
a11
a11z
a22n
a22p
a22z
b b3
b4
blt
c

The strings are traversed left to right in parallel, as under typical
strcmp implementations.

Whenever a decimal digit is encountered in either string, the entire
sequence of decimal digits beginning with that digit is consumed as if
it were one character and turned into a value.

If two values occur in corresponding positions places in the strings, then there
is a value comparison: lower value is lower: a2 comes before a11,
and a2foo comes before a11bar.

If a value occurs in one string but not the other, the value is
considered lexicographically smaller so that "a42" comes before "abc".

If a value occurs in neither string, then the comparison is just
lexicographic by character value (I left that to just be a subtraction
of the char values, which could be signed or unsigned).

A string is lexicographically greater than its prefix, so a "a" is
before "a3" or "abc".

Code:

#include <stdlib.h>
#include <stdio.h>

int encmp(const char *a, const char *b)
{ while (*a && *b) {
int na = 0, nb = 0;
unsigned long va, vb;

if (*a >= '0' && *a <= '9') {
char *nx;
va = strtoul(a, &nx, 10);
a += (nx - a);
na = 1;
}

if (*b >= '0' && *b <= '9') {
char *nx;
vb = strtoul(b, &nx, 10);
b += (nx - b);
nb = 1;
}

if (na && nb) {
if (va < vb)
return -1;
if (va > vb)
return 1;
} else if (na) {
return -1;
} else if (nb) {
return 1;
} else {
if (*a != *b)
return *a - *b;
a++;
b++;
}
}

if (*b)
return -1;
if (*a)
return 1;
return 0;
}

int compar(const void *ap, const void *bp)
{ return encmp(*(const char **) ap, *(const char **) bp);
}

int main(int argc, char **argv)
{ if (argc) {
argc--;
argv++;
}

qsort(argv, argc, sizeof *argv, compar);

while (*argv)
puts(*argv++);

return 0;
}

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: Embedded numbers sorting function (begging post)

<20220222161457.35@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20522&group=comp.lang.c#20522

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Embedded numbers sorting function (begging post)
Date: Wed, 23 Feb 2022 00:17:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 103
Message-ID: <20220222161457.35@kylheku.com>
References: <0865bc76-8d80-43c1-af79-87c3faf7c9d9n@googlegroups.com>
<20220222154422.430@kylheku.com>
Injection-Date: Wed, 23 Feb 2022 00:17:07 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="604ccbf5808049cc8404bfb1ad893045";
logging-data="25590"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Uk4mWJ5TXL2/yn+Xass9rCtFJwzZchuo="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:DLab+2ZZDn/uQF3nI8Mz37OuDI8=
 by: Kaz Kylheku - Wed, 23 Feb 2022 00:17 UTC

On 2022-02-22, Kaz Kylheku <480-992-1380@kylheku.com> wrote:
> On 2022-02-22, Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
>> Anyone got a string comparison function handy which compares strings
>> intelligently by embedded numbers, so that foo2 compares earlier than
>> foo11 ?
>
> Yes; I just whipped up this piece of simplicity in 15 minutes:

Additional hack for considering sequences of spaces and tabs to be one space:

$ ./encmp "a 1 b" "a 11 b" "a 2 b" "a 22 b"
a 1 b
a 2 b
a 11 b
a 22 b

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int encmp(const char *a, const char *b)
{ static const char *blnk = "\t ";

while (*a && *b) {
int na = 0, nb = 0;
unsigned long va, vb;

if (*a >= '0' && *a <= '9') {
char *nx;
va = strtoul(a, &nx, 10);
a += (nx - a);
na = 1;
}

if (*b >= '0' && *b <= '9') {
char *nx;
vb = strtoul(b, &nx, 10);
b += (nx - b);
nb = 1;
}

if (na && nb) {
if (va < vb)
return -1;
if (va > vb)
return 1;
} else if (na) {
return -1;
} else if (nb) {
return 1;
} else {
int ca = *a, cb = *b;

if (strchr(blnk, ca)) {
ca = ' ';
a += strspn(a, blnk);
} else {
a++;
}

if (strchr(blnk, cb)) {
cb = ' ';
b += strspn(b, blnk);
} else {
b++;
}

if (ca != cb)
return ca - cb;
}
}

if (*b)
return -1;
if (*a)
return 1;
return 0;
}

int compar(const void *ap, const void *bp)
{ return encmp(*(const char **) ap, *(const char **) bp);
}

int main(int argc, char **argv)
{ if (argc) {
argc--;
argv++;
}

qsort(argv, argc, sizeof *argv, compar);

while (*argv)
puts(*argv++);

return 0;
}

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor