threads / patch / 4561

patch, 7 partsInitialize FAMs using `FLEX_ARRAY'.

Subject: [PATCH 2/7] Initialize FAMs using `FLEX_ARRAY'.

## tl;dr

4 messages between Jun 18, 2006 and Jun 18, 2006. Diffs are folded; open one to read it.

replies: 3people: 3as markdown or json

Florian Forster· Jun 18, 2006, 15:18 UTC · lore

[PATCH 1/7] Remove ranges from switch statements.

Though very nice and readable, the "case 'a'...'z':" construct is not ANSI C99 compliant. This patch unfolds the range in `quote.c' and substitutes the switch-statement with an if-statement in `http-fetch.c' and `http-push.c'.

Signed-off-by: Florian Forster <octo@verplant.org>
---
 http-fetch.c |   13 +++++++------
 http-push.c  |   13 +++++++------
 quote.c      |    9 ++++++++-
 3 files changed, 22 insertions(+), 13 deletions(-)
d90149c5b4e91938329120bdde609e5f6d9b03e8
Show changes to 3 files +22 −13

http-fetch.c, http-push.c, quote.c

diff --git a/http-fetch.c b/http-fetch.c
index da1a7f5..3a2cb5e 100644
--- a/http-fetch.c
+++ b/http-fetch.c
@@ -1136,13 +1136,14 @@ int fetch(unsigned char *sha1)
 
 static inline int needs_quote(int ch)
 {
-	switch (ch) {
-	case '/': case '-': case '.':
-	case 'A'...'Z':	case 'a'...'z':	case '0'...'9':
+	if (((ch >= 'A') && (ch <= 'Z'))
+			|| ((ch >= 'a') && (ch <= 'z'))
+			|| ((ch >= '0') && (ch <= '9'))
+			|| (ch == '/')
+			|| (ch == '-')
+			|| (ch == '.'))
 		return 0;
-	default:
-		return 1;
-	}
+	return 1;
 }
 
 static inline int hex(int v)
diff --git a/http-push.c b/http-push.c
index 2d9441e..364ab76 100644
--- a/http-push.c
+++ b/http-push.c
@@ -1077,13 +1077,14 @@ static int fetch_indices(void)
 
 static inline int needs_quote(int ch)
 {
-	switch (ch) {
-	case '/': case '-': case '.':
-	case 'A'...'Z':	case 'a'...'z':	case '0'...'9':
+	if (((ch >= 'A') && (ch <= 'Z'))
+			|| ((ch >= 'a') && (ch <= 'z'))
+			|| ((ch >= '0') && (ch <= '9'))
+			|| (ch == '/')
+			|| (ch == '-')
+			|| (ch == '.'))
 		return 0;
-	default:
-		return 1;
-	}
+	return 1;
 }
 
 static inline int hex(int v)
diff --git a/quote.c b/quote.c
index 06792d4..dcc2326 100644
--- a/quote.c
+++ b/quote.c
@@ -206,7 +206,14 @@ #define EMIT(c) (outp ? (*outp++ = (c)) 
 				case '\\': case '"':
 					break; /* verbatim */
 
-				case '0'...'7':
+				case '0':
+				case '1':
+				case '2':
+				case '3':
+				case '4':
+				case '5':
+				case '6':
+				case '7':
 					/* octal */
 					ac = ((ch - '0') << 6);
 					if ((ch = *sp++) < '0' || '7' < ch)
-- 
1.3.3
Florian Forster· Jun 18, 2006, 15:18 UTC · re: Florian Forster · lore

When initializing a `flexible array member' the macro `FLEX_ARRAY' should be used. This was forgotten in `diff-delta.c'.

Signed-off-by: Florian Forster <octo@verplant.org>
---
 diff-delta.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
e587fd59510f2ed8326d280054d78cfb78d482dc
Show changes to diff-delta.c +2 −1
diff --git a/diff-delta.c b/diff-delta.c
index 25a798d..74486b1 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -22,6 +22,7 @@ #include <stdlib.h>
 #include <string.h>
 #include "delta.h"
 
+#include "git-compat-util.h"
 
 /* maximum hash entry list for the same hash bucket */
 #define HASH_LIMIT 64
@@ -131,7 +132,7 @@ struct delta_index {
 	const void *src_buf;
 	unsigned long src_size;
 	unsigned int hash_mask;
-	struct index_entry *hash[0];
+	struct index_entry *hash[FLEX_ARRAY];
 };
 
 struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
-- 
1.3.3
Junio C Hamano· Jun 18, 2006, 21:07 UTC · re: Florian Forster · lore

Re: [PATCH 1/7] Remove ranges from switch statements.

Florian Forster <octo@verplant.org> writes:
Show 7 quoted lines
> -	switch (ch) {
> -	case '/': case '-': case '.':
> -	case 'A'...'Z':	case 'a'...'z':	case '0'...'9':
> +	if (((ch >= 'A') && (ch <= 'Z'))
> +			|| ((ch >= 'a') && (ch <= 'z'))
> +			|| ((ch >= '0') && (ch <= '9'))
> + ...
Sorry for bringing up an old topic again, but wouldn't people
agree that this is easier to read if it were written this way ;-)?
 
	if (	   (('A' <= ch) && (ch <= 'Z'))
        	|| (('a' <= ch) && (ch <= 'z'))
		|| (('0' <= ch) && (ch <= '9'))
	...
Timo Hirvonen· Jun 18, 2006, 21:24 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/7] Remove ranges from switch statements.

Junio C Hamano <junkio@cox.net> wrote:
Show 7 quoted lines
> Sorry for bringing up an old topic again, but wouldn't people
> agree that this is easier to read if it were written this way ;-)?
>  
> 	if (	   (('A' <= ch) && (ch <= 'Z'))
>         	|| (('a' <= ch) && (ch <= 'z'))
> 		|| (('0' <= ch) && (ch <= '9'))
> 	...
Yes, but isalnum(ch) even better ;)
-- 
http://onion.dynserv.net/~timo/

← back to recent threads