Subject: Re: P.S. [was: Re: CVS commit: pkgsrc]
To: None <hubert.feyrer@informatik.fh-regensburg.de>
From: David Brownlee <abs@netbsd.org>
List: tech-pkg
Date: 02/07/2000 11:30:44
On Thu, 3 Feb 2000, Hubert Feyrer wrote:
> 
> If you want someone for code review, let me know.
> (mmm, last minute changes...)

	(always fun)

	Thanks - updated deweycmp attached. It takes two passes through
	the versions, and can handle versions of the form 1.2.3nb5.6.7

	I tried a couple of versions which did not modify the version
	passed in, but they all ended up much more complex...

	Does this look OK to people?

/*
 * Compare two dewey decimal numbers
 */
static int
deweycmp(char *a, deweyop_t op, char *b)
{
	int     ad;
	int     bd;
	char	*a_nb;
	char	*b_nb;
	int	in_nb = 0;
	int     cmp;

	/* Null out 'n' in any "nb" suffixes for initial pass */
	if ((a_nb = strstr(a, "nb")))
	    *a_nb = 0;
	if ((b_nb = strstr(b, "nb")))
	    *b_nb = 0;

	for (;;) {
		if (*a == 0 && *b == 0) {
			if (!in_nb && (a_nb || b_nb)) {
				/*
				 * If exact match on first pass, test
				 * "nb<X>" suffixes in second pass
				 */
				in_nb = 1;
				if (a_nb)
				    a = a_nb + 2;	/* Skip "nb" suffix */
				if (b_nb)
				    b = b_nb + 2;	/* Skip "nb" suffix */
			} else {
				cmp = 0;
				break;
			}
		}

		ad = bd = 0;
		for (; *a && *a != '.'; a++) {
			ad = (ad * 10) + (*a - '0');
		}
		for (; *b && *b != '.'; b++) {
			bd = (bd * 10) + (*b - '0');
		}
		if ((cmp = ad - bd) != 0) {
			break;
		}
		if (*a == '.')
			++a;
		if (*b == '.')
			++b;
	}
	/* Replace any nulled 'n' */
	if (a_nb)
		*a_nb = 'n';
	if (b_nb)
		*b_nb = 'n';
	return (op == GE) ? cmp >= 0 : (op == GT) ? cmp > 0 : (op == LE) ? cmp <= 0 : cmp < 0;
}