1 /*****************************************************************************/
5 /* Variable sized string buffers */
9 /* (C) 2001-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 /* An empty string buf */
55 const StrBuf EmptyStrBuf = STATIC_STRBUF_INITIALIZER;
59 /*****************************************************************************/
61 /*****************************************************************************/
65 #if !defined(HAVE_INLINE)
66 StrBuf* SB_Init (StrBuf* B)
67 /* Initialize a string buffer */
76 StrBuf* SB_InitFromString (StrBuf* B, const char* S)
77 /* Initialize a string buffer from a literal string. Beware: The buffer won't
78 ** store a copy but a pointer to the actual string.
90 void SB_Done (StrBuf* B)
91 /* Free the data of a string buffer (but not the struct itself) */
100 StrBuf* NewStrBuf (void)
101 /* Allocate, initialize and return a new StrBuf */
103 /* Allocate a new string buffer */
104 StrBuf* B = xmalloc (sizeof (StrBuf));
106 /* Initialize the struct... */
109 /* ...and return it */
115 void FreeStrBuf (StrBuf* B)
116 /* Free a string buffer */
118 /* Allow NULL pointers */
127 void SB_Realloc (StrBuf* B, unsigned NewSize)
128 /* Reallocate the string buffer space, make sure at least NewSize bytes are
132 /* Get the current size, use a minimum of 8 bytes */
133 unsigned NewAllocated = B->Allocated;
134 if (NewAllocated == 0) {
138 /* Round up to the next power of two */
139 while (NewAllocated < NewSize) {
143 /* Reallocate the buffer. Beware: The allocated size may be zero while the
144 ** length is not. This means that we have a buffer that wasn't allocated
148 /* Just reallocate the block */
149 B->Buf = xrealloc (B->Buf, NewAllocated);
151 /* Allocate a new block and copy */
152 B->Buf = memcpy (xmalloc (NewAllocated), B->Buf, B->Len);
155 /* Remember the new block size */
156 B->Allocated = NewAllocated;
161 static void SB_CheapRealloc (StrBuf* B, unsigned NewSize)
162 /* Reallocate the string buffer space, make sure at least NewSize bytes are
163 ** available. This function won't copy the old buffer contents over to the new
164 ** buffer and may be used if the old contents are overwritten later.
167 /* Get the current size, use a minimum of 8 bytes */
168 unsigned NewAllocated = B->Allocated;
169 if (NewAllocated == 0) {
173 /* Round up to the next power of two */
174 while (NewAllocated < NewSize) {
178 /* Free the old buffer if there is one */
183 /* Allocate a fresh block */
184 B->Buf = xmalloc (NewAllocated);
186 /* Remember the new block size */
187 B->Allocated = NewAllocated;
192 #if !defined(HAVE_INLINE)
193 char SB_At (const StrBuf* B, unsigned Index)
194 /* Get a character from the buffer */
196 PRECONDITION (Index < B->Len);
197 return B->Buf[Index];
203 void SB_Drop (StrBuf* B, unsigned Count)
204 /* Drop characters from the end of the string. */
206 PRECONDITION (Count <= B->Len);
208 if (B->Index > B->Len) {
215 void SB_Terminate (StrBuf* B)
216 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
217 ** accounted for in B->Len, if you want that, you have to use AppendChar!
220 unsigned NewLen = B->Len + 1;
221 if (NewLen > B->Allocated) {
222 SB_Realloc (B, NewLen);
224 B->Buf[B->Len] = '\0';
229 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size)
230 /* Copy Buf to Target, discarding the old contents of Target */
233 if (Target->Allocated < Size) {
234 SB_CheapRealloc (Target, Size);
236 memcpy (Target->Buf, Buf, Size);
243 #if !defined(HAVE_INLINE)
244 void SB_CopyStr (StrBuf* Target, const char* S)
245 /* Copy S to Target, discarding the old contents of Target */
247 SB_CopyBuf (Target, S, strlen (S));
253 #if !defined(HAVE_INLINE)
254 void SB_Copy (StrBuf* Target, const StrBuf* Source)
255 /* Copy Source to Target, discarding the old contents of Target */
257 SB_CopyBuf (Target, Source->Buf, Source->Len);
258 Target->Index = Source->Index;
264 void SB_AppendChar (StrBuf* B, int C)
265 /* Append a character to a string buffer */
267 unsigned NewLen = B->Len + 1;
268 if (NewLen > B->Allocated) {
269 SB_Realloc (B, NewLen);
271 B->Buf[B->Len] = (char) C;
277 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size)
278 /* Append a character buffer to the end of the string buffer */
280 unsigned NewLen = B->Len + Size;
281 if (NewLen > B->Allocated) {
282 SB_Realloc (B, NewLen);
284 memcpy (B->Buf + B->Len, S, Size);
290 #if !defined(HAVE_INLINE)
291 void SB_AppendStr (StrBuf* B, const char* S)
292 /* Append a string to the end of the string buffer */
294 SB_AppendBuf (B, S, strlen (S));
300 #if !defined(HAVE_INLINE)
301 void SB_Append (StrBuf* Target, const StrBuf* Source)
302 /* Append the contents of Source to Target */
304 SB_AppendBuf (Target, Source->Buf, Source->Len);
310 #if !defined(HAVE_INLINE)
311 void SB_Cut (StrBuf* B, unsigned Len)
312 /* Cut the contents of B at the given length. If the current length of the
313 ** buffer is smaller than Len, nothing will happen.
324 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len)
325 /* Copy a slice from Source into Target. The current contents of Target are
326 ** destroyed. If Start is greater than the length of Source, or if Len
327 ** characters aren't available, the result will be a buffer with less than Len
331 /* Calculate the length of the resulting buffer */
332 if (Start >= Source->Len) {
333 /* Target will be empty */
337 if (Start + Len > Source->Len) {
338 Len = Source->Len - Start;
341 /* Make sure we have enough room in the target string buffer */
342 if (Len > Target->Allocated) {
343 SB_Realloc (Target, Len);
347 memcpy (Target->Buf, Source->Buf + Start, Len);
353 void SB_Move (StrBuf* Target, StrBuf* Source)
354 /* Move the complete contents of Source to target. This will delete the old
355 ** contents of Target, and Source will be empty after the call.
358 /* Free the target string */
359 if (Target->Allocated) {
363 /* Move all data from Source to Target */
372 void SB_ToLower (StrBuf* S)
373 /* Convert all characters in S to lower case */
377 for (I = 0; I < S->Len; ++I, ++B) {
386 void SB_ToUpper (StrBuf* S)
387 /* Convert all characters in S to upper case */
391 for (I = 0; I < S->Len; ++I, ++B) {
400 int SB_Compare (const StrBuf* S1, const StrBuf* S2)
401 /* Do a lexical compare of S1 and S2. See strcmp for result codes. */
404 if (S1->Len < S2->Len) {
405 Result = memcmp (S1->Buf, S2->Buf, S1->Len);
407 /* S1 considered lesser because it's shorter */
410 } else if (S1->Len > S2->Len) {
411 Result = memcmp (S1->Buf, S2->Buf, S2->Len);
413 /* S2 considered lesser because it's shorter */
417 Result = memcmp (S1->Buf, S2->Buf, S1->Len);
424 int SB_CompareStr (const StrBuf* S1, const char* S2)
425 /* Do a lexical compare of S1 and S2. See strcmp for result codes. */
428 unsigned S2Len = strlen (S2);
429 if (S1->Len < S2Len) {
430 Result = memcmp (S1->Buf, S2, S1->Len);
432 /* S1 considered lesser because it's shorter */
435 } else if (S1->Len > S2Len) {
436 Result = memcmp (S1->Buf, S2, S2Len);
438 /* S2 considered lesser because it's shorter */
442 Result = memcmp (S1->Buf, S2, S1->Len);
449 void SB_VPrintf (StrBuf* S, const char* Format, va_list ap)
450 /* printf function with S as target. The function is safe, which means that
451 ** the current contents of S are discarded, and are allocated again with
452 ** a matching size for the output. The function will call FAIL when problems
453 ** are detected (anything that let xsnprintf return -1).
459 /* Since we must determine the space needed anyway, we will try with
460 ** the currently allocated memory. If the call succeeds, we've saved
461 ** an allocation. If not, we have to reallocate and try again.
464 SizeNeeded = xvsnprintf (S->Buf, S->Allocated, Format, tmp);
467 /* Check the result, the xvsnprintf function should not fail */
468 CHECK (SizeNeeded >= 0);
470 /* Check if we must reallocate */
471 if ((unsigned) SizeNeeded >= S->Allocated) {
472 /* Must retry. Use CheapRealloc to avoid copying */
473 SB_CheapRealloc (S, SizeNeeded + 1); /* Account for '\0' */
474 (void) xvsnprintf (S->Buf, S->Allocated, Format, ap);
477 /* Update string buffer variables */
484 void SB_Printf (StrBuf* S, const char* Format, ...)
485 /* vprintf function with S as target. The function is safe, which means that
486 ** the current contents of S are discarded, and are allocated again with
487 ** a matching size for the output. The function will call FAIL when problems
488 ** are detected (anything that let xsnprintf return -1).
492 va_start (ap, Format);
493 SB_VPrintf (S, Format, ap);