From: Isaac Dunham <idunham@lavabit.com>
To: musl@lists.openwall.com
Subject: Re: Fix strverscmp
Date: Wed, 5 Dec 2012 19:18:19 -0800 [thread overview]
Message-ID: <20121205191819.e83c8c3c.idunham@lavabit.com> (raw)
In-Reply-To: <20121206022640.GQ20323@brightrain.aerifal.cx>
[-- Attachment #1: Type: text/plain, Size: 1220 bytes --]
On Wed, 5 Dec 2012 21:26:40 -0500
Rich Felker <dalias@aerifal.cx> wrote:
> > I'm getting the idea that that may actually work...in which case my
> > last version is unneeded.
> > Except, it breaks here:
> > 00123
> > 001145 <-should be the lesser (the leading zeros)
>
> Yes, I was unaware of the leading-zero semantics when I wrote that.
> See my revised email with a proposed algorithm.
Which is almost exactly the same as the method below:
> > OTOH, it could be done by recording the zero while walking up the chain:
> > /*NOT tested*/
> > while (*l && *r && l[0]==r[0]){
> > if (l[0]='0'){
> > nozero=1;
>
> It can't set the flag unconditionally, only if the previous byte was
> not a digit. Otherwise, non-leading zeros would break handling of
> numeric differences.
Fortunately for us, that appears to be incorrect:
idunham@Caracal:~$ ./a.out jan012 jan0111
1
1
idunham@Caracal:~$ ./a.out jan0001 jan001
-1
-1
idunham@Caracal:~$ ./a.out 0001 001
-1
-1
idunham@Caracal:~$ ./a.out 001 0001
1
1
idunham@Caracal:~$ ./a.out 0012 00111
1
1
idunham@Caracal:~$ ./a.out 00012 00111
-1
-1
idunham@Caracal:~$ ./a.out 00120 00111
1
1
That's testing with the attached version.
--
Isaac Dunham <idunham@lavabit.com>
[-- Attachment #2: strvers.c --]
[-- Type: text/x-csrc, Size: 792 bytes --]
#define _GNU_SOURCE
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int str_vers_cmp(const char *l, const char *r){
int haszero=0;
while (*l && *r && l[0]==r[0]){
if (l[0]=='0'){
haszero=0;
} else if (!isdigit(l[0])) {
haszero=1;
}
l++; r++;
}
if ((isdigit(l[0]) && isdigit(r[0]) ) && haszero) {
//return the one with the longer substring of numbers
int lenl=0, lenr=0, firstl=l[0], firstr=r[0];
while (isdigit(l++[0]) ) {
lenl++;
}
while (isdigit(r++[0]) ) {
lenr++;
}
if (lenl==lenr) {
return (firstl - firstr);
} else {
return (lenl - lenr);
}
} else {
return (l[0] - r[0]);
}
}
int main(int argc, char **argv){
printf("%d\n%d\n",str_vers_cmp(argv[1], argv[2]), strverscmp(argv[1], argv[2]));
}
next prev parent reply other threads:[~2012-12-06 3:18 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-12-05 19:09 [PATCH] " Isaac Dunham
2012-12-05 19:35 ` Rich Felker
2012-12-06 0:43 ` Isaac Dunham
2012-12-06 1:00 ` Rich Felker
2012-12-06 2:14 ` Rich Felker
2012-12-06 2:21 ` Isaac Dunham
2012-12-06 2:26 ` Rich Felker
2012-12-06 3:18 ` Isaac Dunham [this message]
2012-12-06 4:14 ` Rich Felker
2012-12-07 0:36 ` Isaac Dunham
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20121205191819.e83c8c3c.idunham@lavabit.com \
--to=idunham@lavabit.com \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).