3 * Copyright (c) 1998-2002, Darren Hiebert
5 * This source code is released for free distribution under the terms of the
6 * GNU General Public License.
8 * This module contains functions supporting resizeable strings.
14 #include "general.h" /* must always come first */
16 #include <limits.h> /* to define INT_MAX */
26 static const size_t vStringInitialSize
= 32;
29 * FUNCTION DEFINITIONS
32 static void vStringResize (vString
*const string
, const size_t newSize
)
34 char *const newBuffer
= xRealloc (string
->buffer
, newSize
, char);
36 string
->size
= newSize
;
37 string
->buffer
= newBuffer
;
44 extern boolean
vStringAutoResize (vString
*const string
)
48 if (string
->size
<= INT_MAX
/ 2)
50 const size_t newSize
= string
->size
* 2;
52 vStringResize (string
, newSize
);
57 extern void vStringClear (vString
*const string
)
60 string
->buffer
[0] = '\0';
61 DebugStatement ( memset (string
->buffer
, 0, string
->size
); )
64 extern void vStringDelete (vString
*const string
)
68 if (string
->buffer
!= NULL
)
69 eFree (string
->buffer
);
74 extern vString
*vStringNew (void)
76 vString
*const string
= xMalloc (1, vString
);
79 string
->size
= vStringInitialSize
;
80 string
->buffer
= xMalloc (string
->size
, char);
82 vStringClear (string
);
87 #ifndef VSTRING_PUTC_MACRO
88 extern void vStringPut (vString
*const string
, const int c
)
90 if (string
->length
+ 1 == string
->size
) /* check for buffer overflow */
91 vStringAutoResize (string
);
93 string
->buffer
[string
->length
] = c
;
95 string
->buffer
[++string
->length
] = '\0';
99 extern void vStringCatS (vString
*const string
, const char *const s
)
102 const size_t len
= strlen (s
);
103 while (string
->length
+ len
+ 1 >= string
->size
)/* check for buffer overflow */
104 vStringAutoResize (string
);
105 strcpy (string
->buffer
+ string
->length
, s
);
106 string
->length
+= len
;
110 vStringPut (string
, *p
);
111 while (*p
++ != '\0');
115 extern vString
*vStringNewCopy (const vString
*const string
)
117 vString
*vs
= vStringNew ();
118 vStringCatS (vs
, string
->buffer
);
122 extern vString
*vStringNewInit (const char *const s
)
124 vString
*vs
= vStringNew ();
129 extern void vStringNCatS (
130 vString
*const string
, const char *const s
, const size_t length
)
133 size_t remain
= length
;
135 while (*p
!= '\0' && remain
> 0)
137 vStringPut (string
, *p
);
141 vStringTerminate (string
);
144 /* Strip trailing newline from string.
146 extern void vStringStripNewline (vString
*const string
)
148 const size_t final
= string
->length
- 1;
149 if (string
->buffer
[final
] == '\n')
151 string
->buffer
[final
] = '\0';
156 /* Strip leading white space from string.
158 extern void vStringStripLeading (vString
*const string
)
160 while (isspace ((int) string
->buffer
[0]) && string
->length
> 0)
163 for (i
= 1 ; i
< string
->length
; ++i
)
164 string
->buffer
[i
- 1] = string
->buffer
[i
];
166 string
->buffer
[string
->length
] = '\0';
170 /* Strip trailing white space from string.
172 extern void vStringStripTrailing (vString
*const string
)
174 while (string
->length
> 0 &&
175 isspace ((int) string
->buffer
[string
->length
- 1]))
178 string
->buffer
[string
->length
] = '\0';
182 /* Chop last character from string.
184 extern void vStringChop (vString
*const string
)
186 if (string
->length
> 0)
189 string
->buffer
[string
->length
] = '\0';
193 extern void vStringCopyS (vString
*const string
, const char *const s
)
195 vStringClear (string
);
196 vStringCatS (string
, s
);
199 extern void vStringNCopyS (
200 vString
*const string
, const char *const s
, const size_t length
)
202 vStringClear (string
);
203 vStringNCatS (string
, s
, length
);
206 extern void vStringCopyToLower (vString
*const dest
, const vString
*const src
)
208 const size_t length
= src
->length
;
209 const char *s
= src
->buffer
;
213 if (dest
->size
< src
->size
)
214 vStringResize (dest
, src
->size
);
216 for (i
= 0 ; i
< length
; ++i
)
225 extern void vStringSetLength (vString
*const string
)
227 string
->length
= strlen (string
->buffer
);
230 /* vi:set tabstop=4 shiftwidth=4: */