git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Pure renames/copies

From
HAH. Peter Anvin <hpa@zytor.com>
Date
Nov 21, 2005, 22:17 UTC
Message-ID
<43824768.2070207@zytor.com>
In-Reply-To
<438245CF.9030501@zytor.com>

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
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)
Previous: H. Peter AnvinNext: Santi Bejar
Message 10 of 11 in “Pure renames/copies”
  1. Santi BéjarNov 21, 2005
  2. Linus TorvaldsNov 21, 2005
  3. Junio C HamanoNov 21, 2005
  4. Junio C HamanoNov 21, 2005
  5. H. Peter AnvinNov 21, 2005
  6. Junio C HamanoNov 21, 2005
  7. H. Peter AnvinNov 21, 2005
  8. Junio C HamanoNov 21, 2005
  9. H. Peter AnvinNov 21, 2005
  10. H. Peter AnvinNov 21, 2005
  11. Santi BejarNov 22, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.