Skip to contentSkip to conversation

Variable "combinations" in tokenize.c and token.h

DISCUSSION Josh Triplett 2 participants 3 messages May 1, 2007 – May 2, 2007 Sparse checker

lore
Share & tools
lore
#0 lore raw

token.h contains this:

Quoted 16 lines

/* Combination tokens */

#define COMBINATION_STRINGS {   \
        "+=", "++",             \
        "-=", "--", "->",       \
        "*=",                   \
        "/=",                   \
        "%=",                   \
        "<=", ">=",             \
        "==", "!=",             \
        "&&", "&=",             \
        "||", "|=",             \
        "^=", "##",             \
        "<<", ">>", "..",       \
        "<<=", ">>=", "...",    \
        "",                     \
        "<", ">", "<=", ">="    \
Quoted 3 lines

}

extern unsigned char combinations[][3];

tokenize.c contains this:

Quoted 11 lines

const char *show_special(int val)

{
        static const char *combinations[] = COMBINATION_STRINGS;
        static char buffer[4];

        buffer[0] = val;
        buffer[1] = 0;
        if (val >= SPECIAL_BASE)
                strcpy(buffer, combinations[val - SPECIAL_BASE]);
        return buffer;
}

[...]

Quoted 1 line

unsigned char combinations[][3] = COMBINATION_STRINGS;

Apart from triggering a -Wshadow warning, this seems somewhat wasteful. The reason appears to relate to the presence or absence of a '\0' terminator at the end of each item. Does that matter? Could show_special change somehow to avoid the duplication? Alternatively, could the global version just include '\0' terminators?

- Josh Triplett

maintainer
#1 lore raw
Quoted 9 lines · Josh Triplett

Apart from triggering a -Wshadow warning, this seems somewhat wasteful. The reason appears to relate to the presence or absence of a '\0' terminator at the end of each item. Does that matter? Could show_special change somehow to avoid the duplication? Alternatively, could the global version just include '\0' terminators?

- Josh Triplett

You mean some thing like that?

Chris

Combinations string clean up

token.h +1 -1
Index: sparse/token.h
===================================================================
@@ -100,7 +100,7 @@ enum token_type {
 	"<", ">", "<=", ">="	\
 }
 
-extern unsigned char combinations[][3];
+extern unsigned char combinations[][4];
 
 enum special_token {
 	SPECIAL_BASE = 256,
tokenize.c +2 -3
Index: sparse/tokenize.c
===================================================================
@@ -59,13 +59,12 @@ static struct position stream_pos(stream
 
 const char *show_special(int val)
 {
-	static const char *combinations[] = COMBINATION_STRINGS;
 	static char buffer[4];
 
 	buffer[0] = val;
 	buffer[1] = 0;
 	if (val >= SPECIAL_BASE)
-		strcpy(buffer, combinations[val - SPECIAL_BASE]);
+		strcpy(buffer, (char *) combinations[val - SPECIAL_BASE]);
 	return buffer;
 }
 
@@ -611,7 +610,7 @@ static int drop_stream_comment(stream_t 
 	return nextchar(stream);
 }
 
-unsigned char combinations[][3] = COMBINATION_STRINGS;
+unsigned char combinations[][4] = COMBINATION_STRINGS;
 
 #define NR_COMBINATIONS (SPECIAL_ARG_SEPARATOR - SPECIAL_BASE)
 
#2 lore raw
Quoted 11 lines · Christopher Li
Quoted 8 lines · Josh Triplett

Apart from triggering a -Wshadow warning, this seems somewhat wasteful. The reason appears to relate to the presence or absence of a '\0' terminator at the end of each item. Does that matter? Could show_special change somehow to avoid the duplication? Alternatively, could the global version just include '\0' terminators?

- Josh Triplett

You mean some thing like that?

[snip patch]

Perfect, thanks. Applied.

- Josh Triplett