Adding functionality to StrBuf

git-svn-id: svn://svn.cc65.org/cc65/trunk@885 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz
2001-09-09 20:49:20 +00:00
parent cf4555f101
commit 23fbf3ff2a
11 changed files with 491 additions and 87 deletions

View File

@@ -42,31 +42,13 @@
/*****************************************************************************/
/* Helpers */
/* Data */
/*****************************************************************************/
void SB_Realloc (StrBuf* B, unsigned NewSize)
/* Reallocate the string buffer space, make sure at least NewSize bytes are
* available.
*/
{
/* Get the current size, use a minimum of 8 bytes */
unsigned NewAllocated = B->Allocated;
if (NewAllocated == 0) {
NewAllocated = 8;
}
/* Round up to the next power of two */
while (NewAllocated < NewSize) {
NewAllocated *= 2;
}
/* Reallocate the buffer */
B->Buf = xrealloc (B->Buf, NewAllocated);
B->Allocated = NewAllocated;
}
/* An empty string buf */
const StrBuf EmptyStrBuf = STATIC_STRBUF_INITIALIZER;
@@ -119,6 +101,29 @@ void FreeStrBuf (StrBuf* B)
void SB_Realloc (StrBuf* B, unsigned NewSize)
/* Reallocate the string buffer space, make sure at least NewSize bytes are
* available.
*/
{
/* Get the current size, use a minimum of 8 bytes */
unsigned NewAllocated = B->Allocated;
if (NewAllocated == 0) {
NewAllocated = 8;
}
/* Round up to the next power of two */
while (NewAllocated < NewSize) {
NewAllocated *= 2;
}
/* Reallocate the buffer */
B->Buf = xrealloc (B->Buf, NewAllocated);
B->Allocated = NewAllocated;
}
void SB_Terminate (StrBuf* B)
/* Zero terminate the given string buffer. NOTE: The terminating zero is not
* accounted for in B->Len, if you want that, you have to use AppendChar!
@@ -133,6 +138,18 @@ void SB_Terminate (StrBuf* B)
void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size)
/* Copy Buf to Target, discarding the old contents of Target */
{
if (Target->Allocated < Size) {
SB_Realloc (Target, Size);
}
memcpy (Target->Buf, Buf, Size);
Target->Len = Size;
}
void SB_AppendChar (StrBuf* B, char C)
/* Append a character to a string buffer */
{
@@ -159,18 +176,6 @@ void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size)
void SB_Copy (StrBuf* Target, const StrBuf* Source)
/* Copy Source to Target, discarding the old contents of Target */
{
if (Target->Allocated < Source->Allocated) {
SB_Realloc (Target, Source->Allocated);
}
memcpy (Target->Buf, Source->Buf, Source->Len);
Target->Len = Source->Len;
}
void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len)
/* Copy a slice from Source into Target. The current contents of Target are
* destroyed. If Start is greater than the length of Source, or if Len

View File

@@ -42,6 +42,7 @@
/* common */
#include "attrib.h"
#include "check.h"
#include "inline.h"
@@ -59,9 +60,15 @@ struct StrBuf {
char* Buf;
};
/* An empty string buf */
extern const StrBuf EmptyStrBuf;
/* Initializer for static string bufs */
#define STATIC_STRBUF_INITIALIZER { 0, 0, 0 }
/* Initializer for auto string bufs */
#define AUTO_STRBUF_INITIALIZER EmptyStrBuf
/*****************************************************************************/
@@ -117,6 +124,39 @@ INLINE char* SB_GetBuf (StrBuf* B)
# define SB_GetBuf(B) (B)->Buf
#endif
#if defined(HAVE_INLINE)
INLINE char SB_At (const StrBuf* B, unsigned Index)
/* Get a character from the buffer */
{
PRECONDITION (Index < B->Len);
return B->Buf[Index];
}
#else
# define SB_At(B, Index) \
(PRECONDITION ((Index) < (B)->Len), \
(B)->Buf[Index])
#endif
#if defined(HAVE_INLINE)
INLINE char SB_AtUnchecked (const StrBuf* B, unsigned Index)
/* Get a character from the buffer */
{
return B->Buf[Index];
}
#else
# define SB_AtUnchecked(B, Index) ((B)->Buf[Index])
#endif
#if defined(HAVE_INLINE)
INLINE int SB_IsEmpty (const StrBuf* B)
/* Return true if the string buffer is empty */
{
return (B->Len == 0);
}
#else
# define SB_IsEmpty(B) ((B)->Len == 0)
#endif
#if defined(HAVE_INLINE)
INLINE void SB_Clear (StrBuf* B)
/* Clear the string buffer (make it empty) */
@@ -132,6 +172,29 @@ void SB_Terminate (StrBuf* B);
* accounted for in B->Len, if you want that, you have to use AppendChar!
*/
void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size);
/* Copy Buf to Target, discarding the old contents of Target */
#if defined(HAVE_INLINE)
INLINE void SB_CopyStr (StrBuf* Target, const char* S)
/* Copy S to Target, discarding the old contents of Target */
{
SB_CopyBuf (Target, S, strlen (S));
}
#else
# define SB_CopyStr(Target, S) SB_CopyBuf (Target, S, strlen (S))
#endif
#if defined(HAVE_INLINE)
INLINE void SB_Copy (StrBuf* Target, const StrBuf* Source)
/* Copy Source to Target, discarding the old contents of Target */
{
SB_CopyBuf (Target, Source->Buf, Source->Len);
}
#else
# define SB_Copy(Target, Source) SB_CopyBuf (Target, (Source)->Buf, (Source)->Len)
#endif
void SB_AppendChar (StrBuf* B, char C);
/* Append a character to a string buffer */
@@ -148,9 +211,6 @@ INLINE void SB_AppendStr (StrBuf* B, const char* S)
# define SB_AppendStr(B, S) SB_AppendBuf (B, S, strlen (S))
#endif
void SB_Copy (StrBuf* Target, const StrBuf* Source);
/* Copy Source to Target, discarding the old contents of Target */
#if defined(HAVE_INLINE)
INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
/* Append the contents of Source to Target */