bugfix: reserve returned wrong pointer
optimized implementation to append 1 char operator += aliases for append with 1 argument equals renamed to startswith
This commit is contained in:
@ -35,26 +35,30 @@ class StreamBuffer
|
|||||||
long offs;
|
long offs;
|
||||||
char* buffer;
|
char* buffer;
|
||||||
|
|
||||||
void init(const void*, long);
|
void init(const void* s, long minsize);
|
||||||
|
|
||||||
void check(long size)
|
void check(long size)
|
||||||
{if (len+offs+size >= cap) grow(len+size);}
|
{if (len+offs+size >= cap) grow(len+size);}
|
||||||
|
|
||||||
|
void grow(long minsize);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void grow(long);
|
|
||||||
// Hints:
|
// Hints:
|
||||||
// * Any index parameter (long) can be negative
|
// * Any index parameter (long) can be negative
|
||||||
// meaning "count from end" (-1 is the last byte)
|
// meaning "count from end" (-1 is the last byte)
|
||||||
|
// * Appending negative count deletes from end
|
||||||
// * Any returned char* pointer becomes invalid when
|
// * Any returned char* pointer becomes invalid when
|
||||||
// the StreamBuffer is modified.
|
// the StreamBuffer is modified.
|
||||||
|
// * End of StreamBuffer always contains 0x00 bytes
|
||||||
|
// * Deleting from start and clearing is fast
|
||||||
|
|
||||||
StreamBuffer()
|
StreamBuffer()
|
||||||
{init(NULL, 0);}
|
{init(NULL, 0);}
|
||||||
|
|
||||||
StreamBuffer(const void*s, long size)
|
StreamBuffer(const void* s, long size)
|
||||||
{init(s, size);}
|
{init(s, size);}
|
||||||
|
|
||||||
StreamBuffer(const char*s)
|
StreamBuffer(const char* s)
|
||||||
{init(s, s?strlen(s):0);}
|
{init(s, s?strlen(s):0);}
|
||||||
|
|
||||||
StreamBuffer(const StreamBuffer& s)
|
StreamBuffer(const StreamBuffer& s)
|
||||||
@ -68,10 +72,10 @@ public:
|
|||||||
|
|
||||||
// operator (): get char* pointing to index
|
// operator (): get char* pointing to index
|
||||||
const char* operator()(long index=0) const
|
const char* operator()(long index=0) const
|
||||||
{buffer[offs+len]=0; return buffer+offs+(index<0?index+len:index);}
|
{return buffer+offs+(index<0?index+len:index);}
|
||||||
|
|
||||||
char* operator()(long index=0)
|
char* operator()(long index=0)
|
||||||
{buffer[offs+len]=0; return buffer+offs+(index<0?index+len:index);}
|
{return buffer+offs+(index<0?index+len:index);}
|
||||||
|
|
||||||
// operator []: get byte at index
|
// operator []: get byte at index
|
||||||
char operator[](long index) const
|
char operator[](long index) const
|
||||||
@ -103,11 +107,16 @@ public:
|
|||||||
// reserve: reserve size bytes of memory and return
|
// reserve: reserve size bytes of memory and return
|
||||||
// pointer to that memory (for copying something to it)
|
// pointer to that memory (for copying something to it)
|
||||||
char* reserve(long size)
|
char* reserve(long size)
|
||||||
{grow(size); char* p=buffer+len; len+=size; return p;}
|
{check(size); char* p=buffer+offs+len; len+=size; return p;}
|
||||||
|
|
||||||
// append: append data at the end of the buffer
|
// append: append data at the end of the buffer
|
||||||
StreamBuffer& append(char c, long count=1)
|
StreamBuffer& append(char c)
|
||||||
{check(count); while(count-->0) buffer[offs+len++]=c; return *this;}
|
{check(1); buffer[offs+len++]=c; return *this;}
|
||||||
|
|
||||||
|
StreamBuffer& append(char c, long count)
|
||||||
|
{if (count < 0) truncate(count);
|
||||||
|
else {check(count); memset(buffer+offs+len, c, count); len+=count;}
|
||||||
|
return *this;}
|
||||||
|
|
||||||
StreamBuffer& append(const void* s, long size);
|
StreamBuffer& append(const void* s, long size);
|
||||||
|
|
||||||
@ -116,6 +125,16 @@ public:
|
|||||||
|
|
||||||
StreamBuffer& append(const StreamBuffer& s)
|
StreamBuffer& append(const StreamBuffer& s)
|
||||||
{return append(s.buffer+s.offs, s.len);}
|
{return append(s.buffer+s.offs, s.len);}
|
||||||
|
|
||||||
|
// operator += alias for set
|
||||||
|
StreamBuffer& operator+=(char c)
|
||||||
|
{return append(c);}
|
||||||
|
|
||||||
|
StreamBuffer& operator+=(const char* s)
|
||||||
|
{return append(s);}
|
||||||
|
|
||||||
|
StreamBuffer& operator+=(const StreamBuffer& s)
|
||||||
|
{return append(s);}
|
||||||
|
|
||||||
// set: clear buffer and fill with new data
|
// set: clear buffer and fill with new data
|
||||||
StreamBuffer& set(const void* s, long size)
|
StreamBuffer& set(const void* s, long size)
|
||||||
@ -127,7 +146,7 @@ public:
|
|||||||
StreamBuffer& set(const StreamBuffer& s)
|
StreamBuffer& set(const StreamBuffer& s)
|
||||||
{clear(); return append(s.buffer+s.offs, s.len);}
|
{clear(); return append(s.buffer+s.offs, s.len);}
|
||||||
|
|
||||||
// operator =: alias for set
|
// operator = alias for set
|
||||||
StreamBuffer& operator=(const char* s)
|
StreamBuffer& operator=(const char* s)
|
||||||
{return set(s);}
|
{return set(s);}
|
||||||
|
|
||||||
@ -148,6 +167,7 @@ public:
|
|||||||
StreamBuffer& remove(long pos, long length)
|
StreamBuffer& remove(long pos, long length)
|
||||||
{return replace(pos, length, NULL, 0);}
|
{return replace(pos, length, NULL, 0);}
|
||||||
|
|
||||||
|
// remove from start: no memset, no function call, fast!
|
||||||
StreamBuffer& remove(long length)
|
StreamBuffer& remove(long length)
|
||||||
{if (length>len) length=len;
|
{if (length>len) length=len;
|
||||||
offs+=length; len-=length; return *this;}
|
offs+=length; len-=length; return *this;}
|
||||||
@ -188,12 +208,12 @@ public:
|
|||||||
long int find(const StreamBuffer& s, long start=0) const
|
long int find(const StreamBuffer& s, long start=0) const
|
||||||
{return find(s.buffer+s.offs, s.len, start);}
|
{return find(s.buffer+s.offs, s.len, start);}
|
||||||
|
|
||||||
// equals: returns true if first size bytes are equal
|
// startswith: returns true if first size bytes are equal
|
||||||
bool equals(const void* s, long size) const
|
bool startswith(const void* s, long size) const
|
||||||
{return len>=size ? memcmp(buffer+offs, s, size) == 0 : false;}
|
{return len>=size ? memcmp(buffer+offs, s, size) == 0 : false;}
|
||||||
|
|
||||||
// equals: returns true if first string is equal (empty string match)
|
// startswith: returns true if first string is equal (empty string matches)
|
||||||
bool equals(const char* s) const
|
bool startswith(const char* s) const
|
||||||
{return len ? strcmp(buffer+offs, s) == 0 : !s || !*s;}
|
{return len ? strcmp(buffer+offs, s) == 0 : !s || !*s;}
|
||||||
|
|
||||||
// expand: create copy of StreamBuffer where all nonprintable characters
|
// expand: create copy of StreamBuffer where all nonprintable characters
|
||||||
|
Reference in New Issue
Block a user