9
9
*
10
10
*
11
11
* IDENTIFICATION
12
- * $Header: /cvsroot/pgsql/src/backend/lib/stringinfo.c,v 1.11 1998/09/01 03 :22:39 momjian Exp $
12
+ * $Header: /cvsroot/pgsql/src/backend/lib/stringinfo.c,v 1.12 1998/11/08 19 :22:24 tgl Exp $
13
13
*
14
14
*-------------------------------------------------------------------------
15
15
*/
@@ -31,27 +31,22 @@ StringInfo
31
31
makeStringInfo ()
32
32
{
33
33
StringInfo res ;
34
- long size ;
34
+ int size ;
35
35
36
36
res = (StringInfo ) palloc (sizeof (StringInfoData ));
37
37
if (res == NULL )
38
38
elog (ERROR , "makeStringInfo: Out of memory!" );
39
39
40
- size = 100 ;
40
+ size = 256 ; /* initial default size */
41
41
res -> data = palloc (size );
42
42
if (res -> data == NULL )
43
43
{
44
44
elog (ERROR ,
45
- "makeStringInfo: Out of memory! (%ld bytes requested)" , size );
45
+ "makeStringInfo: Out of memory! (%d bytes requested)" , size );
46
46
}
47
47
res -> maxlen = size ;
48
48
res -> len = 0 ;
49
-
50
- /*
51
- * NOTE: we must initialize `res->data' to the empty string because we
52
- * use 'strcat' in 'appendStringInfo', which of course it always
53
- * expects a null terminated string.
54
- */
49
+ /* Make sure the string is empty initially. */
55
50
res -> data [0 ] = '\0' ;
56
51
57
52
return res ;
71
66
appendStringInfo (StringInfo str , char * buffer )
72
67
{
73
68
int buflen ,
74
- newlen ;
69
+ newlen ,
70
+ needed ;
75
71
char * s ;
76
72
77
73
Assert (str != NULL );
@@ -84,15 +80,16 @@ appendStringInfo(StringInfo str, char *buffer)
84
80
* some more.
85
81
*/
86
82
buflen = strlen (buffer );
87
- if (buflen + str -> len >= str -> maxlen - 1 )
83
+ needed = str -> len + buflen + 1 ;
84
+ if (needed > str -> maxlen )
88
85
{
89
86
90
87
/*
91
88
* how much more space to allocate ? Let's say double the current
92
89
* space... However we must check if this is enough!
93
90
*/
94
- newlen = 2 * str -> len ;
95
- while (buflen + str -> len >= newlen - 1 )
91
+ newlen = 2 * str -> maxlen ;
92
+ while (needed > newlen )
96
93
newlen = 2 * newlen ;
97
94
98
95
/*
@@ -105,18 +102,22 @@ appendStringInfo(StringInfo str, char *buffer)
105
102
"appendStringInfo: Out of memory (%d bytes requested)" ,
106
103
newlen );
107
104
}
108
- memmove (s , str -> data , str -> len + 1 );
105
+ /*
106
+ * transfer the data. strcpy() would work, but is probably a tad
107
+ * slower than memcpy(), and since we know the string length...
108
+ */
109
+ memcpy (s , str -> data , str -> len + 1 );
109
110
pfree (str -> data );
110
111
str -> maxlen = newlen ;
111
112
str -> data = s ;
112
113
}
113
114
114
115
/*
115
116
* OK, we have enough space now, append 'buffer' at the end of the
116
- * string & update the string length. NOTE: this is a text string
117
- * (i.e. printable characters) so 'strcat' will do the job (no need to
118
- * use 'bcopy' et all...)
117
+ * string & update the string length. NOTE: strcat() would work,
118
+ * but is certainly slower than just memcpy'ing the data to the right
119
+ * place.
119
120
*/
120
- strcat (str -> data , buffer );
121
+ memcpy (str -> data + str -> len , buffer , buflen + 1 );
121
122
str -> len += buflen ;
122
123
}
0 commit comments