From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/2422 Path: news.gmane.org!not-for-mail From: Isaac Dunham Newsgroups: gmane.linux.lib.musl.general Subject: Re: Fix strverscmp Date: Thu, 6 Dec 2012 16:36:43 -0800 Message-ID: <20121206163643.7d972f99.idunham@lavabit.com> References: <20121205110959.87b6111a.idunham@lavabit.com> <20121205193520.GN20323@brightrain.aerifal.cx> <20121205164329.c5cd3a20.idunham@lavabit.com> <20121206010000.GO20323@brightrain.aerifal.cx> <20121205182101.d997eb6e.idunham@lavabit.com> <20121206022640.GQ20323@brightrain.aerifal.cx> <20121205191819.e83c8c3c.idunham@lavabit.com> <20121206041456.GR20323@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="Multipart=_Thu__6_Dec_2012_16_36_43_-0800_5CnRu781u085Pz1E" X-Trace: ger.gmane.org 1354840618 4720 80.91.229.3 (7 Dec 2012 00:36:58 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 7 Dec 2012 00:36:58 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-2423-gllmg-musl=m.gmane.org@lists.openwall.com Fri Dec 07 01:37:11 2012 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1TglwE-0004il-Up for gllmg-musl@plane.gmane.org; Fri, 07 Dec 2012 01:37:11 +0100 Original-Received: (qmail 27997 invoked by uid 550); 7 Dec 2012 00:36:57 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 27985 invoked from network); 7 Dec 2012 00:36:57 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=lavabit; d=lavabit.com; b=B2bU1TzErUVBMYY1nI1C/MRPIM2flgvRcZe/zeon2o2BvrlWzS9aQu6hKqwcMRGvxqJ1arzyCejZH9epczxusEaQK2qc2jYPiyLzBovSrzCyTOydkfr3PpKwq9RQGvBrVPMU4pnWDAnCAluuo5BKiLddyb5q4DqsHAoyFuE6t/0=; h=Date:From:To:Subject:Message-Id:In-Reply-To:References:X-Mailer:Mime-Version:Content-Type; In-Reply-To: <20121206041456.GR20323@brightrain.aerifal.cx> X-Mailer: Sylpheed 3.0.2 (GTK+ 2.20.1; i486-pc-linux-gnu) Xref: news.gmane.org gmane.linux.lib.musl.general:2422 Archived-At: This is a multi-part message in MIME format. --Multipart=_Thu__6_Dec_2012_16_36_43_-0800_5CnRu781u085Pz1E Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 5 Dec 2012 23:14:56 -0500 Rich Felker 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 --Multipart=_Thu__6_Dec_2012_16_36_43_-0800_5CnRu781u085Pz1E Content-Type: text/x-csrc; name="strvers.c" Content-Disposition: attachment; filename="strvers.c" Content-Transfer-Encoding: 7bit #define _GNU_SOURCE #define _XOPEN_SOURCE 700 #include #include #include 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])); } --Multipart=_Thu__6_Dec_2012_16_36_43_-0800_5CnRu781u085Pz1E--