Commit 90d76525 authored by Neil Conway's avatar Neil Conway

Add resetStringInfo(), which clears the content of a StringInfo, and

fixup various places in the tree that were clearing a StringInfo by hand.
Making this function a part of the API simplifies client code slightly,
and avoids needlessly peeking inside the StringInfo interface.
parent 053981f4
...@@ -1378,15 +1378,12 @@ build_tuplestore_recursively(char *key_fld, ...@@ -1378,15 +1378,12 @@ build_tuplestore_recursively(char *key_fld,
"incompatible."))); "incompatible.")));
} }
for (i = 0; i < proc; i++)
{
/* start a new branch */
initStringInfo(&branchstr); initStringInfo(&branchstr);
/* need these to check for recursion */
initStringInfo(&chk_branchstr); initStringInfo(&chk_branchstr);
initStringInfo(&chk_current_key); initStringInfo(&chk_current_key);
for (i = 0; i < proc; i++)
{
/* initialize branch for this pass */ /* initialize branch for this pass */
appendStringInfo(&branchstr, "%s", branch); appendStringInfo(&branchstr, "%s", branch);
appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim); appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim);
...@@ -1459,11 +1456,15 @@ build_tuplestore_recursively(char *key_fld, ...@@ -1459,11 +1456,15 @@ build_tuplestore_recursively(char *key_fld,
tupstore); tupstore);
/* reset branch for next pass */ /* reset branch for next pass */
resetStringInfo(&branchstr);
resetStringInfo(&chk_branchstr);
resetStringInfo(&chk_current_key);
}
xpfree(branchstr.data); xpfree(branchstr.data);
xpfree(chk_branchstr.data); xpfree(chk_branchstr.data);
xpfree(chk_current_key.data); xpfree(chk_current_key.data);
} }
}
return tupstore; return tupstore;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/catalog/pg_shdepend.c,v 1.16 2007/01/05 22:19:25 momjian Exp $ * $PostgreSQL: pgsql/src/backend/catalog/pg_shdepend.c,v 1.17 2007/03/03 19:32:54 neilc Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -588,8 +588,7 @@ checkSharedDependencies(Oid classId, Oid objectId) ...@@ -588,8 +588,7 @@ checkSharedDependencies(Oid classId, Oid objectId)
* Note: we don't ever suppress per-database totals, which should be * Note: we don't ever suppress per-database totals, which should be
* OK as long as there aren't too many databases ... * OK as long as there aren't too many databases ...
*/ */
descs.len = 0; /* reset to empty */ resetStringInfo(&descs);
descs.data[0] = '\0';
if (numLocalDeps > 0) if (numLocalDeps > 0)
{ {
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.276 2007/02/20 17:32:13 tgl Exp $ * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.277 2007/03/03 19:32:54 neilc Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -466,9 +466,7 @@ CopySendEndOfRow(CopyState cstate) ...@@ -466,9 +466,7 @@ CopySendEndOfRow(CopyState cstate)
break; break;
} }
/* Reset fe_msgbuf to empty */ resetStringInfo(fe_msgbuf);
fe_msgbuf->len = 0;
fe_msgbuf->data[0] = '\0';
} }
/* /*
...@@ -2193,9 +2191,7 @@ CopyReadLine(CopyState cstate) ...@@ -2193,9 +2191,7 @@ CopyReadLine(CopyState cstate)
{ {
bool result; bool result;
/* Reset line_buf to empty */ resetStringInfo(&cstate->line_buf);
cstate->line_buf.len = 0;
cstate->line_buf.data[0] = '\0';
/* Mark that encoding conversion hasn't occurred yet */ /* Mark that encoding conversion hasn't occurred yet */
cstate->line_buf_converted = false; cstate->line_buf_converted = false;
...@@ -2262,8 +2258,7 @@ CopyReadLine(CopyState cstate) ...@@ -2262,8 +2258,7 @@ CopyReadLine(CopyState cstate)
if (cvt != cstate->line_buf.data) if (cvt != cstate->line_buf.data)
{ {
/* transfer converted data back to line_buf */ /* transfer converted data back to line_buf */
cstate->line_buf.len = 0; resetStringInfo(&cstate->line_buf);
cstate->line_buf.data[0] = '\0';
appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt)); appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt));
pfree(cvt); pfree(cvt);
} }
...@@ -2686,9 +2681,7 @@ CopyReadAttributesText(CopyState cstate, int maxfields, char **fieldvals) ...@@ -2686,9 +2681,7 @@ CopyReadAttributesText(CopyState cstate, int maxfields, char **fieldvals)
return 0; return 0;
} }
/* reset attribute_buf to empty */ resetStringInfo(&cstate->attribute_buf);
cstate->attribute_buf.len = 0;
cstate->attribute_buf.data[0] = '\0';
/* /*
* The de-escaped attributes will certainly not be longer than the input * The de-escaped attributes will certainly not be longer than the input
...@@ -2886,9 +2879,7 @@ CopyReadAttributesCSV(CopyState cstate, int maxfields, char **fieldvals) ...@@ -2886,9 +2879,7 @@ CopyReadAttributesCSV(CopyState cstate, int maxfields, char **fieldvals)
return 0; return 0;
} }
/* reset attribute_buf to empty */ resetStringInfo(&cstate->attribute_buf);
cstate->attribute_buf.len = 0;
cstate->attribute_buf.data[0] = '\0';
/* /*
* The de-escaped attributes will certainly not be longer than the input * The de-escaped attributes will certainly not be longer than the input
...@@ -3040,12 +3031,9 @@ CopyReadBinaryAttribute(CopyState cstate, ...@@ -3040,12 +3031,9 @@ CopyReadBinaryAttribute(CopyState cstate,
errmsg("invalid field size"))); errmsg("invalid field size")));
/* reset attribute_buf to empty, and load raw data in it */ /* reset attribute_buf to empty, and load raw data in it */
cstate->attribute_buf.len = 0; resetStringInfo(&cstate->attribute_buf);
cstate->attribute_buf.data[0] = '\0';
cstate->attribute_buf.cursor = 0;
enlargeStringInfo(&cstate->attribute_buf, fld_size); enlargeStringInfo(&cstate->attribute_buf, fld_size);
if (CopyGetData(cstate, cstate->attribute_buf.data, if (CopyGetData(cstate, cstate->attribute_buf.data,
fld_size, fld_size) != fld_size) fld_size, fld_size) != fld_size)
ereport(ERROR, ereport(ERROR,
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.44 2007/01/05 22:19:29 momjian Exp $ * $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.45 2007/03/03 19:32:54 neilc Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -49,8 +49,20 @@ initStringInfo(StringInfo str) ...@@ -49,8 +49,20 @@ initStringInfo(StringInfo str)
str->data = (char *) palloc(size); str->data = (char *) palloc(size);
str->maxlen = size; str->maxlen = size;
str->len = 0; resetStringInfo(str);
}
/*
* resetStringInfo
*
* Reset the StringInfo: the data buffer remains valid, but its
* previous content, if any, is cleared.
*/
void
resetStringInfo(StringInfo str)
{
str->data[0] = '\0'; str->data[0] = '\0';
str->len = 0;
str->cursor = 0; str->cursor = 0;
} }
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.190 2007/02/13 19:18:53 tgl Exp $ * $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.191 2007/03/03 19:32:54 neilc Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -860,10 +860,7 @@ pq_getstring(StringInfo s) ...@@ -860,10 +860,7 @@ pq_getstring(StringInfo s)
{ {
int i; int i;
/* Reset string to empty */ resetStringInfo(s);
s->len = 0;
s->data[0] = '\0';
s->cursor = 0;
/* Read until we get the terminating '\0' */ /* Read until we get the terminating '\0' */
for (;;) for (;;)
...@@ -915,10 +912,7 @@ pq_getmessage(StringInfo s, int maxlen) ...@@ -915,10 +912,7 @@ pq_getmessage(StringInfo s, int maxlen)
{ {
int32 len; int32 len;
/* Reset message buffer to empty */ resetStringInfo(s);
s->len = 0;
s->data[0] = '\0';
s->cursor = 0;
/* Read message length word */ /* Read message length word */
if (pq_getbytes((char *) &len, 4) == EOF) if (pq_getbytes((char *) &len, 4) == EOF)
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.45 2007/03/03 18:46:40 momjian Exp $ * $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.46 2007/03/03 19:32:54 neilc Exp $
* *
* Interface: * Interface:
* *
...@@ -933,8 +933,7 @@ DeadLockReport(void) ...@@ -933,8 +933,7 @@ DeadLockReport(void)
appendStringInfoChar(&buf, '\n'); appendStringInfoChar(&buf, '\n');
/* reset buf2 to hold next object description */ /* reset buf2 to hold next object description */
buf2.len = 0; resetStringInfo(&buf2);
buf2.data[0] = '\0';
DescribeLockTag(&buf2, &info->locktag); DescribeLockTag(&buf2, &info->locktag);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.95 2007/01/05 22:19:39 momjian Exp $ * $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.96 2007/03/03 19:32:54 neilc Exp $
* *
* NOTES * NOTES
* This cruft is the server side of PQfn. * This cruft is the server side of PQfn.
...@@ -480,10 +480,7 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip, ...@@ -480,10 +480,7 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip,
argsize))); argsize)));
/* Reset abuf to empty, and insert raw data into it */ /* Reset abuf to empty, and insert raw data into it */
abuf.len = 0; resetStringInfo(&abuf);
abuf.data[0] = '\0';
abuf.cursor = 0;
appendBinaryStringInfo(&abuf, appendBinaryStringInfo(&abuf,
pq_getmsgbytes(msgBuf, argsize), pq_getmsgbytes(msgBuf, argsize),
argsize); argsize);
...@@ -613,10 +610,7 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip, ...@@ -613,10 +610,7 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip,
argsize))); argsize)));
/* Reset abuf to empty, and insert raw data into it */ /* Reset abuf to empty, and insert raw data into it */
abuf.len = 0; resetStringInfo(&abuf);
abuf.data[0] = '\0';
abuf.cursor = 0;
appendBinaryStringInfo(&abuf, appendBinaryStringInfo(&abuf,
pq_getmsgbytes(msgBuf, argsize), pq_getmsgbytes(msgBuf, argsize),
argsize); argsize);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.526 2007/03/02 23:37:22 tgl Exp $ * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.527 2007/03/03 19:32:54 neilc Exp $
* *
* NOTES * NOTES
* this is the "main" module of the postgres backend and * this is the "main" module of the postgres backend and
...@@ -205,10 +205,7 @@ InteractiveBackend(StringInfo inBuf) ...@@ -205,10 +205,7 @@ InteractiveBackend(StringInfo inBuf)
printf("backend> "); printf("backend> ");
fflush(stdout); fflush(stdout);
/* Reset inBuf to empty */ resetStringInfo(inBuf);
inBuf->len = 0;
inBuf->data[0] = '\0';
inBuf->cursor = 0;
for (;;) for (;;)
{ {
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/rowtypes.c,v 1.18 2007/01/05 22:19:42 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/rowtypes.c,v 1.19 2007/03/03 19:32:55 neilc Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -168,8 +168,7 @@ record_in(PG_FUNCTION_ARGS) ...@@ -168,8 +168,7 @@ record_in(PG_FUNCTION_ARGS)
/* Extract string for this column */ /* Extract string for this column */
bool inquote = false; bool inquote = false;
buf.len = 0; resetStringInfo(&buf);
buf.data[0] = '\0';
while (inquote || !(*ptr == ',' || *ptr == ')')) while (inquote || !(*ptr == ',' || *ptr == ')'))
{ {
char ch = *ptr++; char ch = *ptr++;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.33 2007/03/01 14:52:04 petere Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.34 2007/03/03 19:32:55 neilc Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -835,8 +835,7 @@ xml_init(void) ...@@ -835,8 +835,7 @@ xml_init(void)
else else
{ {
/* Reset pre-existing buffer to empty */ /* Reset pre-existing buffer to empty */
xml_err_buf->data[0] = '\0'; resetStringInfo(xml_err_buf);
xml_err_buf->len = 0;
} }
/* Now that xml_err_buf exists, safe to call xml_errorHandler */ /* Now that xml_err_buf exists, safe to call xml_errorHandler */
xmlSetGenericErrorFunc(NULL, xml_errorHandler); xmlSetGenericErrorFunc(NULL, xml_errorHandler);
...@@ -1197,8 +1196,7 @@ xml_ereport(int level, int sqlcode, ...@@ -1197,8 +1196,7 @@ xml_ereport(int level, int sqlcode,
if (xml_err_buf->len > 0) if (xml_err_buf->len > 0)
{ {
detail = pstrdup(xml_err_buf->data); detail = pstrdup(xml_err_buf->data);
xml_err_buf->data[0] = '\0'; resetStringInfo(xml_err_buf);
xml_err_buf->len = 0;
} }
else else
detail = NULL; detail = NULL;
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/lib/stringinfo.h,v 1.33 2007/01/05 22:19:55 momjian Exp $ * $PostgreSQL: pgsql/src/include/lib/stringinfo.h,v 1.34 2007/03/03 19:32:55 neilc Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -78,6 +78,13 @@ extern StringInfo makeStringInfo(void); ...@@ -78,6 +78,13 @@ extern StringInfo makeStringInfo(void);
*/ */
extern void initStringInfo(StringInfo str); extern void initStringInfo(StringInfo str);
/*------------------------
* resetStringInfo
* Clears the current content of the StringInfo, if any. The
* StringInfo remains valid.
*/
extern void resetStringInfo(StringInfo str);
/*------------------------ /*------------------------
* appendStringInfo * appendStringInfo
* Format text data under the control of fmt (an sprintf-style format string) * Format text data under the control of fmt (an sprintf-style format string)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment