diff mbox

[U-Boot,V3,04/12] cmd_test: use table lookup for parsing

Message ID 1390855802-17504-4-git-send-email-swarren@wwwdotorg.org
State Superseded
Delegated to: Tom Rini
Headers show

Commit Message

Stephen Warren Jan. 27, 2014, 8:49 p.m. UTC
From: Stephen Warren <swarren@nvidia.com>

do_test() currently uses strcmp() twice to determine which operator is
present; once to determine how many arguments the operator needs, then
a second time to actually decode the operator and implement it.

Rewrite the code so that a table lookup is used to translate the operator
string to an integer, and use a more efficient switch statement to decode
and execute the operator.

This approach also acts as enablement for the following patches.

This patch should introduce no behavioural change.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
---
v3: New patch.
---
 common/cmd_test.c | 177 +++++++++++++++++++++++++++++++++---------------------
 1 file changed, 110 insertions(+), 67 deletions(-)

Comments

Simon Glass Feb. 1, 2014, 12:03 a.m. UTC | #1
Hi Stephen,

On 27 January 2014 13:49, Stephen Warren <swarren@wwwdotorg.org> wrote:
> From: Stephen Warren <swarren@nvidia.com>
>
> do_test() currently uses strcmp() twice to determine which operator is
> present; once to determine how many arguments the operator needs, then
> a second time to actually decode the operator and implement it.
>
> Rewrite the code so that a table lookup is used to translate the operator
> string to an integer, and use a more efficient switch statement to decode
> and execute the operator.
>
> This approach also acts as enablement for the following patches.
>
> This patch should introduce no behavioural change.
>
> Signed-off-by: Stephen Warren <swarren@nvidia.com>
> ---
> v3: New patch.

Wow that's some interesting code...it took me a while to understand
both the old and the new code. It looks correct to me but I wonder if
it is deserving of some tests? Something like test/command_ut.c might
show a simple way to run some tests.

Regards,
Simon
Stephen Warren Feb. 3, 2014, 8:19 p.m. UTC | #2
On 01/31/2014 05:03 PM, Simon Glass wrote:
> Hi Stephen,
> 
> On 27 January 2014 13:49, Stephen Warren <swarren@wwwdotorg.org> wrote:
>> From: Stephen Warren <swarren@nvidia.com>
>>
>> do_test() currently uses strcmp() twice to determine which operator is
>> present; once to determine how many arguments the operator needs, then
>> a second time to actually decode the operator and implement it.
>>
>> Rewrite the code so that a table lookup is used to translate the operator
>> string to an integer, and use a more efficient switch statement to decode
>> and execute the operator.
>>
>> This approach also acts as enablement for the following patches.
>>
>> This patch should introduce no behavioural change.
>>
>> Signed-off-by: Stephen Warren <swarren@nvidia.com>
>> ---
>> v3: New patch.
> 
> Wow that's some interesting code...it took me a while to understand
> both the old and the new code. It looks correct to me but I wonder if
> it is deserving of some tests? Something like test/command_ut.c might
> show a simple way to run some tests.

OK, I'll send V4 of this series with your minor issues addressed, and
I'll send a separate follow-on series which adds the unit tests, just so
it doesn't delay or cause revisions to the main series.
Simon Glass Feb. 3, 2014, 8:37 p.m. UTC | #3
Hi Stephen,

On 3 February 2014 13:19, Stephen Warren <swarren@wwwdotorg.org> wrote:
>
> On 01/31/2014 05:03 PM, Simon Glass wrote:
> > Hi Stephen,
> >
> > On 27 January 2014 13:49, Stephen Warren <swarren@wwwdotorg.org> wrote:
> >> From: Stephen Warren <swarren@nvidia.com>
> >>
> >> do_test() currently uses strcmp() twice to determine which operator is
> >> present; once to determine how many arguments the operator needs, then
> >> a second time to actually decode the operator and implement it.
> >>
> >> Rewrite the code so that a table lookup is used to translate the operator
> >> string to an integer, and use a more efficient switch statement to decode
> >> and execute the operator.
> >>
> >> This approach also acts as enablement for the following patches.
> >>
> >> This patch should introduce no behavioural change.
> >>
> >> Signed-off-by: Stephen Warren <swarren@nvidia.com>
> >> ---
> >> v3: New patch.
> >
> > Wow that's some interesting code...it took me a while to understand
> > both the old and the new code. It looks correct to me but I wonder if
> > it is deserving of some tests? Something like test/command_ut.c might
> > show a simple way to run some tests.
>
> OK, I'll send V4 of this series with your minor issues addressed, and
> I'll send a separate follow-on series which adds the unit tests, just so
> it doesn't delay or cause revisions to the main series.


Sounds good, thanks.

Regards,
Simon
diff mbox

Patch

diff --git a/common/cmd_test.c b/common/cmd_test.c
index bacc3684069b..69b1b4cee69a 100644
--- a/common/cmd_test.c
+++ b/common/cmd_test.c
@@ -17,10 +17,48 @@ 
 #include <common.h>
 #include <command.h>
 
+#define OP_INVALID	0
+#define OP_OR		2
+#define OP_AND		3
+#define OP_STR_EMPTY	4
+#define OP_STR_NEMPTY	5
+#define OP_STR_EQ	6
+#define OP_STR_NEQ	7
+#define OP_STR_LT	8
+#define OP_STR_GT	9
+#define OP_INT_EQ	10
+#define OP_INT_NEQ	11
+#define OP_INT_LT	12
+#define OP_INT_LE	13
+#define OP_INT_GT	14
+#define OP_INT_GE	15
+
+const struct {
+	int arg;
+	const char *str;
+	int op;
+	int adv;
+} op_adv[] = {
+	{0, "-o", OP_OR, 1},
+	{0, "-a", OP_AND, 1},
+	{0, "-z", OP_STR_EMPTY, 2},
+	{0, "-n", OP_STR_NEMPTY, 2},
+	{1, "=", OP_STR_EQ, 3},
+	{1, "!=", OP_STR_NEQ, 3},
+	{1, "<", OP_STR_LT, 3},
+	{1, ">", OP_STR_GT, 3},
+	{1, "-eq", OP_INT_EQ, 3},
+	{1, "-ne", OP_INT_NEQ, 3},
+	{1, "-lt", OP_INT_LT, 3},
+	{1, "-le", OP_INT_LE, 3},
+	{1, "-gt", OP_INT_GT, 3},
+	{1, "-ge", OP_INT_GE, 3},
+};
+
 static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
 	char * const *ap;
-	int left, adv, expr, last_expr, neg, last_cmp;
+	int i, op, left, adv, expr, last_expr, neg, last_cmp;
 
 	/* args? */
 	if (argc < 3)
@@ -45,83 +83,88 @@  static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		neg = 0;
 
 	expr = -1;
-	last_cmp = -1;
+	last_cmp = OP_INVALID;
 	last_expr = -1;
 	while (left > 0) {
-
-		if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0)
-			adv = 1;
-		else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0)
-			adv = 2;
-		else
-			adv = 3;
-
-		if (left < adv) {
+		for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
+			if (left <= op_adv[i].arg)
+				continue;
+			if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
+				op = op_adv[i].op;
+				adv = op_adv[i].adv;
+				break;
+			}
+		}
+		if (i == ARRAY_SIZE(op_adv)) {
 			expr = 1;
 			break;
 		}
-
-		if (adv == 1) {
-			if (strcmp(ap[0], "-o") == 0) {
-				last_expr = expr;
-				last_cmp = 0;
-			} else if (strcmp(ap[0], "-a") == 0) {
-				last_expr = expr;
-				last_cmp = 1;
-			} else {
-				expr = 1;
-				break;
-			}
+		if (left < adv) {
+			expr = 1;
+			break;
 		}
 
-		if (adv == 2) {
-			if (strcmp(ap[0], "-z") == 0)
-				expr = strlen(ap[1]) == 0 ? 1 : 0;
-			else if (strcmp(ap[0], "-n") == 0)
-				expr = strlen(ap[1]) == 0 ? 0 : 1;
-			else {
-				expr = 1;
-				break;
-			}
-
-			if (last_cmp == 0)
-				expr = last_expr || expr;
-			else if (last_cmp == 1)
-				expr = last_expr && expr;
-			last_cmp = -1;
+		switch (op) {
+		case OP_STR_EMPTY:
+			expr = strlen(ap[1]) == 0 ? 1 : 0;
+			break;
+		case OP_STR_NEMPTY:
+			expr = strlen(ap[1]) == 0 ? 0 : 1;
+			break;
+		case OP_STR_EQ:
+			expr = strcmp(ap[0], ap[2]) == 0;
+			break;
+		case OP_STR_NEQ:
+			expr = strcmp(ap[0], ap[2]) != 0;
+			break;
+		case OP_STR_LT:
+			expr = strcmp(ap[0], ap[2]) < 0;
+			break;
+		case OP_STR_GT:
+			expr = strcmp(ap[0], ap[2]) > 0;
+			break;
+		case OP_INT_EQ:
+			expr = simple_strtol(ap[0], NULL, 10) ==
+					simple_strtol(ap[2], NULL, 10);
+			break;
+		case OP_INT_NEQ:
+			expr = simple_strtol(ap[0], NULL, 10) !=
+					simple_strtol(ap[2], NULL, 10);
+			break;
+		case OP_INT_LT:
+			expr = simple_strtol(ap[0], NULL, 10) <
+					simple_strtol(ap[2], NULL, 10);
+			break;
+		case OP_INT_LE:
+			expr = simple_strtol(ap[0], NULL, 10) <=
+					simple_strtol(ap[2], NULL, 10);
+			break;
+		case OP_INT_GT:
+			expr = simple_strtol(ap[0], NULL, 10) >
+					simple_strtol(ap[2], NULL, 10);
+			break;
+		case OP_INT_GE:
+			expr = simple_strtol(ap[0], NULL, 10) >=
+					simple_strtol(ap[2], NULL, 10);
+			break;
 		}
 
-		if (adv == 3) {
-			if (strcmp(ap[1], "=") == 0)
-				expr = strcmp(ap[0], ap[2]) == 0;
-			else if (strcmp(ap[1], "!=") == 0)
-				expr = strcmp(ap[0], ap[2]) != 0;
-			else if (strcmp(ap[1], ">") == 0)
-				expr = strcmp(ap[0], ap[2]) > 0;
-			else if (strcmp(ap[1], "<") == 0)
-				expr = strcmp(ap[0], ap[2]) < 0;
-			else if (strcmp(ap[1], "-eq") == 0)
-				expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10);
-			else if (strcmp(ap[1], "-ne") == 0)
-				expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10);
-			else if (strcmp(ap[1], "-lt") == 0)
-				expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10);
-			else if (strcmp(ap[1], "-le") == 0)
-				expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10);
-			else if (strcmp(ap[1], "-gt") == 0)
-				expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10);
-			else if (strcmp(ap[1], "-ge") == 0)
-				expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10);
-			else {
-				expr = 1;
-				break;
-			}
-
-			if (last_cmp == 0)
+		switch (op) {
+		case OP_OR:
+			last_expr = expr;
+			last_cmp = OP_OR;
+			break;
+		case OP_AND:
+			last_expr = expr;
+			last_cmp = OP_AND;
+			break;
+		default:
+			if (last_cmp == OP_OR)
 				expr = last_expr || expr;
-			else if (last_cmp == 1)
+			else if (last_cmp == OP_AND)
 				expr = last_expr && expr;
-			last_cmp = -1;
+			last_cmp = OP_INVALID;
+			break;
 		}
 
 		ap += adv; left -= adv;