1 /*****************************************************************************/
5 /* Variable sized string buffers */
9 /* (C) 2001-2004 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
44 /*****************************************************************************/
46 /*****************************************************************************/
50 /* An empty string buf */
51 const StrBuf EmptyStrBuf = STATIC_STRBUF_INITIALIZER;
55 /*****************************************************************************/
57 /*****************************************************************************/
61 StrBuf* InitStrBuf (StrBuf* B)
62 /* Initialize a string buffer */
73 void DoneStrBuf (StrBuf* B)
74 /* Free the data of a string buffer (but not the struct itself) */
81 StrBuf* NewStrBuf (void)
82 /* Allocate, initialize and return a new StrBuf */
84 /* Allocate a new string buffer */
85 StrBuf* B = xmalloc (sizeof (StrBuf));
87 /* Initialize the struct... */
90 /* ...and return it */
96 void FreeStrBuf (StrBuf* B)
97 /* Free a string buffer */
105 static void SB_Realloc (StrBuf* B, unsigned NewSize)
106 /* Reallocate the string buffer space, make sure at least NewSize bytes are
110 /* Get the current size, use a minimum of 8 bytes */
111 unsigned NewAllocated = B->Allocated;
112 if (NewAllocated == 0) {
116 /* Round up to the next power of two */
117 while (NewAllocated < NewSize) {
121 /* Reallocate the buffer */
122 B->Buf = xrealloc (B->Buf, NewAllocated);
123 B->Allocated = NewAllocated;
128 #if !defined(HAVE_INLINE)
129 char SB_At (const StrBuf* B, unsigned Index)
130 /* Get a character from the buffer */
132 PRECONDITION (Index < B->Len);
133 return B->Buf[Index];
139 void SB_Drop (StrBuf* B, unsigned Count)
140 /* Drop characters from the end of the string. */
142 PRECONDITION (Count <= B->Len);
144 if (B->Index > B->Len) {
151 void SB_Terminate (StrBuf* B)
152 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
153 * accounted for in B->Len, if you want that, you have to use AppendChar!
156 unsigned NewLen = B->Len + 1;
157 if (NewLen > B->Allocated) {
158 SB_Realloc (B, NewLen);
160 B->Buf[B->Len] = '\0';
165 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size)
166 /* Copy Buf to Target, discarding the old contents of Target */
168 if (Target->Allocated < Size) {
169 SB_Realloc (Target, Size);
171 memcpy (Target->Buf, Buf, Size);
177 #if !defined(HAVE_INLINE)
178 void SB_CopyStr (StrBuf* Target, const char* S)
179 /* Copy S to Target, discarding the old contents of Target */
181 SB_CopyBuf (Target, S, strlen (S));
187 #if !defined(HAVE_INLINE)
188 void SB_Copy (StrBuf* Target, const StrBuf* Source)
189 /* Copy Source to Target, discarding the old contents of Target */
191 SB_CopyBuf (Target, Source->Buf, Source->Len);
192 Target->Index = Source->Index;
198 void SB_AppendChar (StrBuf* B, int C)
199 /* Append a character to a string buffer */
201 unsigned NewLen = B->Len + 1;
202 if (NewLen > B->Allocated) {
203 SB_Realloc (B, NewLen);
205 B->Buf[B->Len] = (char) C;
211 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size)
212 /* Append a character buffer to the end of the string buffer */
214 unsigned NewLen = B->Len + Size;
215 if (NewLen > B->Allocated) {
216 SB_Realloc (B, NewLen);
218 memcpy (B->Buf + B->Len, S, Size);
224 #if !defined(HAVE_INLINE)
225 void SB_AppendStr (StrBuf* B, const char* S)
226 /* Append a string to the end of the string buffer */
228 SB_AppendBuf (B, S, strlen (S));
234 #if !defined(HAVE_INLINE)
235 void SB_Append (StrBuf* Target, const StrBuf* Source)
236 /* Append the contents of Source to Target */
238 SB_AppendBuf (Target, Source->Buf, Source->Len);
244 #if !defined(HAVE_INLINE)
245 void SB_Cut (StrBuf* B, unsigned Len)
246 /* Cut the contents of B at the given length. If the current length of the
247 * buffer is smaller than Len, nothing will happen.
258 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len)
259 /* Copy a slice from Source into Target. The current contents of Target are
260 * destroyed. If Start is greater than the length of Source, or if Len
261 * characters aren't available, the result will be a buffer with less than Len
265 /* Calculate the length of the resulting buffer */
266 if (Start >= Source->Len) {
267 /* Target will be empty */
270 } else if (Start + Len > Source->Len) {
271 Len = (Start + Len) - Source->Len;
274 /* Make sure we have enough room in the target string buffer */
275 if (Len > Target->Allocated) {
276 SB_Realloc (Target, Len);
280 memcpy (Target->Buf, Source->Buf + Start, Len);
286 void SB_Move (StrBuf* Target, StrBuf* Source)
287 /* Move the complete contents of Source to target. This will delete the old
288 * contents of Target, and Source will be empty after the call.
291 /* Free the target string */
296 /* Move all data from Source to Target */
305 int SB_Compare (const StrBuf* S1, const StrBuf* S2)
306 /* Do a lexical compare of S1 and S2. See strcmp for result codes. */
309 if (S1->Len < S2->Len) {
310 Result = memcmp (S1->Buf, S2->Buf, S1->Len);
312 /* S1 considered lesser because it's shorter */
315 } else if (S1->Len > S2->Len) {
316 Result = memcmp (S1->Buf, S2->Buf, S2->Len);
318 /* S2 considered lesser because it's shorter */
322 Result = memcmp (S1->Buf, S2->Buf, S1->Len);