On Sat, 2007-03-03 at 11:23 -0500, Bruce Momjian wrote:
> Are there any places in our code where we could use it?

I manually grep'ed around and found a few places where resetStringInfo
can be used, but I probably didn't find them all: it's quite hard to
find all the places in which "StringInfo->len = 0" is assigned to, given
that the StringInfo might have any variable name, and variables like
"len" and "buf" are very common for other purposes.

Attached is a revised patch, I'll commit it later today.

-Neil

Index: src/backend/commands/copy.c
===================================================================
RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/commands/copy.c,v
retrieving revision 1.276
diff -c -p -r1.276 copy.c
*** src/backend/commands/copy.c	20 Feb 2007 17:32:13 -0000	1.276
--- src/backend/commands/copy.c	3 Mar 2007 17:10:56 -0000
*************** CopySendEndOfRow(CopyState cstate)
*** 467,474 ****
  	}
  
  	/* Reset fe_msgbuf to empty */
! 	fe_msgbuf->len = 0;
! 	fe_msgbuf->data[0] = '\0';
  }
  
  /*
--- 467,473 ----
  	}
  
  	/* Reset fe_msgbuf to empty */
! 	resetStringInfo(fe_msgbuf);
  }
  
  /*
*************** CopyReadLine(CopyState cstate)
*** 2194,2201 ****
  	bool		result;
  
  	/* Reset line_buf to empty */
! 	cstate->line_buf.len = 0;
! 	cstate->line_buf.data[0] = '\0';
  
  	/* Mark that encoding conversion hasn't occurred yet */
  	cstate->line_buf_converted = false;
--- 2193,2199 ----
  	bool		result;
  
  	/* Reset line_buf to empty */
! 	resetStringInfo(&cstate->line_buf);
  
  	/* Mark that encoding conversion hasn't occurred yet */
  	cstate->line_buf_converted = false;
*************** CopyReadAttributesText(CopyState cstate,
*** 2687,2694 ****
  	}
  
  	/* reset attribute_buf to empty */
! 	cstate->attribute_buf.len = 0;
! 	cstate->attribute_buf.data[0] = '\0';
  
  	/*
  	 * The de-escaped attributes will certainly not be longer than the input
--- 2685,2691 ----
  	}
  
  	/* reset attribute_buf to empty */
! 	resetStringInfo(&cstate->attribute_buf);
  
  	/*
  	 * The de-escaped attributes will certainly not be longer than the input
*************** CopyReadAttributesCSV(CopyState cstate, 
*** 2887,2894 ****
  	}
  
  	/* reset attribute_buf to empty */
! 	cstate->attribute_buf.len = 0;
! 	cstate->attribute_buf.data[0] = '\0';
  
  	/*
  	 * The de-escaped attributes will certainly not be longer than the input
--- 2884,2890 ----
  	}
  
  	/* reset attribute_buf to empty */
! 	resetStringInfo(&cstate->attribute_buf);
  
  	/*
  	 * The de-escaped attributes will certainly not be longer than the input
*************** CopyReadBinaryAttribute(CopyState cstate
*** 3040,3048 ****
  				 errmsg("invalid field size")));
  
  	/* reset attribute_buf to empty, and load raw data in it */
! 	cstate->attribute_buf.len = 0;
! 	cstate->attribute_buf.data[0] = '\0';
! 	cstate->attribute_buf.cursor = 0;
  
  	enlargeStringInfo(&cstate->attribute_buf, fld_size);
  
--- 3036,3042 ----
  				 errmsg("invalid field size")));
  
  	/* reset attribute_buf to empty, and load raw data in it */
! 	resetStringInfo(&cstate->attribute_buf);
  
  	enlargeStringInfo(&cstate->attribute_buf, fld_size);
  
Index: src/backend/lib/stringinfo.c
===================================================================
RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/lib/stringinfo.c,v
retrieving revision 1.44
diff -c -p -r1.44 stringinfo.c
*** src/backend/lib/stringinfo.c	5 Jan 2007 22:19:29 -0000	1.44
--- src/backend/lib/stringinfo.c	3 Mar 2007 17:01:46 -0000
*************** initStringInfo(StringInfo str)
*** 49,56 ****
  
  	str->data = (char *) palloc(size);
  	str->maxlen = size;
! 	str->len = 0;
  	str->data[0] = '\0';
  	str->cursor = 0;
  }
  
--- 49,68 ----
  
  	str->data = (char *) palloc(size);
  	str->maxlen = size;
! 	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->len = 0;
  	str->cursor = 0;
  }
  
Index: src/backend/storage/lmgr/deadlock.c
===================================================================
RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/storage/lmgr/deadlock.c,v
retrieving revision 1.44
diff -c -p -r1.44 deadlock.c
*** src/backend/storage/lmgr/deadlock.c	5 Jan 2007 22:19:38 -0000	1.44
--- src/backend/storage/lmgr/deadlock.c	3 Mar 2007 17:05:49 -0000
*************** DeadLockReport(void)
*** 923,930 ****
  			appendStringInfoChar(&buf, '\n');
  
  		/* reset buf2 to hold next object description */
! 		buf2.len = 0;
! 		buf2.data[0] = '\0';
  
  		DescribeLockTag(&buf2, &info->locktag);
  
--- 923,929 ----
  			appendStringInfoChar(&buf, '\n');
  
  		/* reset buf2 to hold next object description */
! 		resetStringInfo(&buf2);
  
  		DescribeLockTag(&buf2, &info->locktag);
  
Index: src/backend/tcop/fastpath.c
===================================================================
RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/tcop/fastpath.c,v
retrieving revision 1.95
diff -c -p -r1.95 fastpath.c
*** src/backend/tcop/fastpath.c	5 Jan 2007 22:19:39 -0000	1.95
--- src/backend/tcop/fastpath.c	3 Mar 2007 17:04:53 -0000
*************** parse_fcall_arguments(StringInfo msgBuf,
*** 480,489 ****
  						 argsize)));
  
  			/* Reset abuf to empty, and insert raw data into it */
! 			abuf.len = 0;
! 			abuf.data[0] = '\0';
! 			abuf.cursor = 0;
! 
  			appendBinaryStringInfo(&abuf,
  								   pq_getmsgbytes(msgBuf, argsize),
  								   argsize);
--- 480,486 ----
  						 argsize)));
  
  			/* Reset abuf to empty, and insert raw data into it */
! 			resetStringInfo(&abuf);
  			appendBinaryStringInfo(&abuf,
  								   pq_getmsgbytes(msgBuf, argsize),
  								   argsize);
*************** parse_fcall_arguments_20(StringInfo msgB
*** 613,622 ****
  						 argsize)));
  
  		/* Reset abuf to empty, and insert raw data into it */
! 		abuf.len = 0;
! 		abuf.data[0] = '\0';
! 		abuf.cursor = 0;
! 
  		appendBinaryStringInfo(&abuf,
  							   pq_getmsgbytes(msgBuf, argsize),
  							   argsize);
--- 610,616 ----
  						 argsize)));
  
  		/* Reset abuf to empty, and insert raw data into it */
! 		resetStringInfo(&abuf);
  		appendBinaryStringInfo(&abuf,
  							   pq_getmsgbytes(msgBuf, argsize),
  							   argsize);
Index: src/backend/utils/adt/rowtypes.c
===================================================================
RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/utils/adt/rowtypes.c,v
retrieving revision 1.18
diff -c -p -r1.18 rowtypes.c
*** src/backend/utils/adt/rowtypes.c	5 Jan 2007 22:19:42 -0000	1.18
--- src/backend/utils/adt/rowtypes.c	3 Mar 2007 17:01:06 -0000
*************** record_in(PG_FUNCTION_ARGS)
*** 168,175 ****
  			/* Extract string for this column */
  			bool		inquote = false;
  
! 			buf.len = 0;
! 			buf.data[0] = '\0';
  			while (inquote || !(*ptr == ',' || *ptr == ')'))
  			{
  				char		ch = *ptr++;
--- 168,174 ----
  			/* Extract string for this column */
  			bool		inquote = false;
  
! 			resetStringInfo(&buf);
  			while (inquote || !(*ptr == ',' || *ptr == ')'))
  			{
  				char		ch = *ptr++;
Index: src/include/lib/stringinfo.h
===================================================================
RCS file: /home/neilc/postgres/cvs_root/pgsql/src/include/lib/stringinfo.h,v
retrieving revision 1.33
diff -c -p -r1.33 stringinfo.h
*** src/include/lib/stringinfo.h	5 Jan 2007 22:19:55 -0000	1.33
--- src/include/lib/stringinfo.h	3 Mar 2007 17:01:46 -0000
*************** extern StringInfo makeStringInfo(void);
*** 79,84 ****
--- 79,91 ----
  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
   * Format text data under the control of fmt (an sprintf-style format string)
   * and append it to whatever is already in str.  More space is allocated
---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match

Reply via email to