mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Isaac Dunham <idunham@lavabit.com>
To: musl@lists.openwall.com
Subject: Re: Fix strverscmp
Date: Thu, 6 Dec 2012 16:36:43 -0800	[thread overview]
Message-ID: <20121206163643.7d972f99.idunham@lavabit.com> (raw)
In-Reply-To: <20121206041456.GR20323@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 844 bytes --]

On Wed, 5 Dec 2012 23:14:56 -0500
Rich Felker <dalias@aerifal.cx> wrote:

> All that means is that you missed testing any case where it differs.
> 
> ./a.out s10212 s102102
> 2
> -1

I realized what you meant just after sending the last email...
Anyhow, after working out how the state should change and a little bit of testing, I arrived at the attached version.

Logic for the upper part:
Start, assuming a non-numeric char (1)
--if initialized to 0, it will break the case where the first 2 characters are differing digits!
while walking down to the difference, 
-record 0 if char was last
-record 2 for any other digit if char was last
-Reset to 1 if a non-digit is found.
This records the leading digit of a sequence.
When the difference is reached, record 0 if one branch contains a leading zero.


-- 
Isaac Dunham <idunham@lavabit.com>

[-- Attachment #2: strvers.c --]
[-- Type: text/x-csrc, Size: 933 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=1;
	while (*l && *r && l[0]==r[0]){
		if (l[0]=='0'){
			if (haszero==1) {
				haszero=0;
			}
		} else if (isdigit(l[0])) {
			if (haszero==1) {
				haszero=2;
			}
		} else {
			haszero=1;
		}
		l++; r++;
	}
	if (haszero==1 && (l[0]=='0' || r[0]=='0')) {
		haszero=0;
	}
	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]));
}

      reply	other threads:[~2012-12-07  0:36 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
2012-12-06  4:14             ` Rich Felker
2012-12-07  0:36               ` Isaac Dunham [this message]

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=20121206163643.7d972f99.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).