Clean up psql variable code a little: eliminate unnecessary tests in

GetVariable() and be consistent about treatment of the list header.
Motivated by noticing strspn() taking an unreasonable percentage of
runtime --- the call removed from GetVariable() was the only one that
could be in a high-usage path ...
This commit is contained in:
Tom Lane 2006-06-21 16:05:11 +00:00
parent 3f9aace723
commit 04c5b69603
1 changed files with 20 additions and 21 deletions

View File

@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2006, PostgreSQL Global Development Group * Copyright (c) 2000-2006, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/variables.c,v 1.24 2006/06/14 16:49:03 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/variables.c,v 1.25 2006/06/21 16:05:11 tgl Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
@ -29,15 +29,13 @@ GetVariable(VariableSpace space, const char *name)
if (!space) if (!space)
return NULL; return NULL;
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) for (current = space->next; current; current = current->next)
return NULL;
for (current = space; current; current = current->next)
{ {
psql_assert(current->name);
psql_assert(current->value);
if (strcmp(current->name, name) == 0) if (strcmp(current->name, name) == 0)
{
psql_assert(current->value);
return current->value; return current->value;
}
} }
return NULL; return NULL;
@ -126,6 +124,9 @@ PrintVariables(VariableSpace space)
{ {
struct _variable *ptr; struct _variable *ptr;
if (!space)
return;
for (ptr = space->next; ptr; ptr = ptr->next) for (ptr = space->next; ptr; ptr = ptr->next)
{ {
printf("%s = '%s'\n", ptr->name, ptr->value); printf("%s = '%s'\n", ptr->name, ptr->value);
@ -143,18 +144,19 @@ SetVariable(VariableSpace space, const char *name, const char *value)
if (!space) if (!space)
return false; return false;
if (!value)
return DeleteVariable(space, name);
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
return false; return false;
for (current = space, previous = NULL; current; previous = current, current = current->next) if (!value)
return DeleteVariable(space, name);
for (previous = space, current = space->next;
current;
previous = current, current = current->next)
{ {
psql_assert(current->name);
psql_assert(current->value);
if (strcmp(current->name, name) == 0) if (strcmp(current->name, name) == 0)
{ {
psql_assert(current->value);
free(current->value); free(current->value);
current->value = pg_strdup(value); current->value = pg_strdup(value);
return true; return true;
@ -182,19 +184,16 @@ DeleteVariable(VariableSpace space, const char *name)
if (!space) if (!space)
return false; return false;
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) for (previous = space, current = space->next;
return false; current;
previous = current, current = current->next)
for (current = space, previous = NULL; current; previous = current, current = current->next)
{ {
psql_assert(current->name);
psql_assert(current->value);
if (strcmp(current->name, name) == 0) if (strcmp(current->name, name) == 0)
{ {
psql_assert(current->value);
previous->next = current->next;
free(current->name); free(current->name);
free(current->value); free(current->value);
if (previous)
previous->next = current->next;
free(current); free(current);
return true; return true;
} }