Skip to contentSkip to conversation

clean up cclass

RFC PATCH In review Alecs King 2 participants 2 messages Dec 31, 2005 Sparse checker

lore
Share & tools
lore

Alecs King

author
#0 lore raw

RFC PATCH 1/1 clean up cclass

Is there a good reason to always add that '+ 1' in both initialization and dereference of cclass?

This patch just remove that unnecessary '+ 1'.

tokenize.c +28 -28
index 497da13..7464b5a 100644
@@ -347,30 +347,30 @@ enum {
 	ValidSecond = 32,
 };
 
-static const long cclass[257] = {
-	['0' + 1 ... '9' + 1] = Digit | Hex,
-	['A' + 1 ... 'D' + 1] = Letter | Hex,
-	['E' + 1] = Letter | Hex | Exp,
-	['F' + 1] = Letter | Hex,
-	['G' + 1 ... 'O' + 1] = Letter,
-	['P' + 1] = Letter | Exp,
-	['Q' + 1 ... 'Z' + 1] = Letter,
-	['a' + 1 ... 'd' + 1] = Letter | Hex,
-	['e' + 1] = Letter | Hex | Exp,
-	['f' + 1] = Letter | Hex,
-	['g' + 1 ... 'o' + 1] = Letter,
-	['p' + 1] = Letter | Exp,
-	['q' + 1 ... 'z' + 1] = Letter,
-	['_' + 1] = Letter,
-	['.' + 1] = Dot | ValidSecond,
-	['=' + 1] = ValidSecond,
-	['+' + 1] = ValidSecond,
-	['-' + 1] = ValidSecond,
-	['>' + 1] = ValidSecond,
-	['<' + 1] = ValidSecond,
-	['&' + 1] = ValidSecond,
-	['|' + 1] = ValidSecond,
-	['#' + 1] = ValidSecond,
+static const long cclass[256] = {
+	['0' ... '9'] = Digit | Hex,
+	['A' ... 'D'] = Letter | Hex,
+	['E'] = Letter | Hex | Exp,
+	['F'] = Letter | Hex,
+	['G' ... 'O'] = Letter,
+	['P'] = Letter | Exp,
+	['Q' ... 'Z'] = Letter,
+	['a' ... 'd'] = Letter | Hex,
+	['e'] = Letter | Hex | Exp,
+	['f'] = Letter | Hex,
+	['g' ... 'o'] = Letter,
+	['p'] = Letter | Exp,
+	['q' ... 'z'] = Letter,
+	['_'] = Letter,
+	['.'] = Dot | ValidSecond,
+	['='] = ValidSecond,
+	['+'] = ValidSecond,
+	['-'] = ValidSecond,
+	['>'] = ValidSecond,
+	['<'] = ValidSecond,
+	['&'] = ValidSecond,
+	['|'] = ValidSecond,
+	['#'] = ValidSecond,
 };
 
 /*
@@ -394,7 +394,7 @@ static int get_one_number(int c, int nex
 
 	*p++ = c;
 	for (;;) {
-		long class =  cclass[next + 1];
+		long class =  cclass[next];
 		if (!(class & (Dot | Digit | Letter)))
 			break;
 		if (p != buffer_end)
@@ -647,7 +647,7 @@ static int get_one_special(int c, stream
 	 * Check for combinations
 	 */
 	value = c;
-	if (cclass[next + 1] & ValidSecond) {
+	if (cclass[next] & ValidSecond) {
 		comb = combinations[0];
 		c1 = c; c2 = next; c3 = 0;
 		for (i = 0; i < NR_COMBINATIONS; i++) {
@@ -810,7 +810,7 @@ static int get_one_identifier(int c, str
 	buf[0] = c;
 	for (;;) {
 		next = nextchar(stream);
-		if (!(cclass[next + 1] & (Letter | Digit)))
+		if (!(cclass[next] & (Letter | Digit)))
 			break;
 		if (len >= sizeof(buf))
 			break;
@@ -832,7 +832,7 @@ static int get_one_identifier(int c, str
 
 static int get_one_token(int c, stream_t *stream)
 {
-	long class = cclass[c + 1];
+	long class = cclass[c];
 	if (class & Digit)
 		return get_one_number(c, nextchar(stream), stream);
 	if (class & Letter)
Signature · 1 line
Alecs King
#1 lore raw
Quoted 3 lines · Alecs King

Is there a good reason to always add that '+ 1' in both initialization and dereference of cclass?

EOF, aka -1.

Now, whether that's a good enough reason for ugly code, I don't know. You could do the same by doing something like

	static const struct {
		long eof;
		long c[256];
	} cclass = { 0 , { ..sane initializer.. } };

and then using

cclass.c[next]

without the "+1". It might noe be strictly conforming, but it sure as hell should work in practice.

Another (perhaps better) alternative is to change the #define of EOF from -1 to 256, and then make sure that we don't rely on "next < 0" anywhere (which we might do by changing the type to "unsigned int" and expecting the compiler to complain.

Linus