git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: "H. Peter Anvin" <hpa@zytor.com>
Cc: Junio C Hamano <junkio@cox.net>, git@vger.kernel.org
Subject: Re: Pure renames/copies
Date: Mon, 21 Nov 2005 14:17:12 -0800	[thread overview]
Message-ID: <43824768.2070207@zytor.com> (raw)
In-Reply-To: <438245CF.9030501@zytor.com>

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

Better variant, which handles stuff like "4.5%" and rejects 
"192.168.0.1".  Additionally, make sure numbers are unsigned (I'm making 
them unsigned long just for the hell of it), to make sure that 
artificial wraparound scenarios don't cause harm.

	-hpa


[-- Attachment #2: diff --]
[-- Type: text/plain, Size: 1186 bytes --]

diff --git a/diff.c b/diff.c
index 0391e8c..ffe8a55 100644
--- a/diff.c
+++ b/diff.c
@@ -838,16 +838,29 @@ int diff_opt_parse(struct diff_options *
 
 static int parse_num(const char **cp_p)
 {
-	int num, scale, ch, cnt;
+	unsigned long num, scale;
+	int ch, dot;
 	const char *cp = *cp_p;
 
-	cnt = num = 0;
+	num = 0;
 	scale = 1;
-	while ('0' <= (ch = *cp) && ch <= '9') {
-		if (cnt++ < 5) {
-			/* We simply ignore more than 5 digits precision. */
-			scale *= 10;
-			num = num * 10 + ch - '0';
+	dot = 0;
+	for(;;) {
+		ch = *cp;
+		if ( !dot && ch == '.' ) {
+			scale = 1;
+			dot = 1;
+		} else if ( ch == '%' ) {
+			scale = dot ? scale*100 : 100;
+			cp++;	/* % is always at the end */
+			break;
+		} else if ( ch >= '0' && ch <= '9' ) {
+			if ( scale < 100000 ) {
+				scale *= 10;
+				num = (num*10) + (ch-'0');
+			}
+		} else {
+			break;
 		}
 		cp++;
 	}
@@ -856,7 +869,7 @@ static int parse_num(const char **cp_p)
 	/* user says num divided by scale and we say internally that
 	 * is MAX_SCORE * num / scale.
 	 */
-	return (MAX_SCORE * num / scale);
+	return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
 }
 
 int diff_scoreopt_parse(const char *opt)

  reply	other threads:[~2005-11-21 22:17 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-21 12:01 Pure renames/copies Santi Béjar
2005-11-21 18:37 ` Linus Torvalds
2005-11-21 19:31   ` Junio C Hamano
2005-11-21 19:50   ` Junio C Hamano
2005-11-21 21:01     ` H. Peter Anvin
2005-11-21 21:33       ` Junio C Hamano
2005-11-21 21:37         ` H. Peter Anvin
2005-11-21 22:00           ` Junio C Hamano
2005-11-21 22:10             ` H. Peter Anvin
2005-11-21 22:17               ` H. Peter Anvin [this message]
2005-11-22  9:03     ` Santi Bejar

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=43824768.2070207@zytor.com \
    --to=hpa@zytor.com \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    /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.
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).