The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SWBuf Class Reference

#include <swbuf.h>

+ Collaboration diagram for SWBuf:

Public Member Functions

SWBufappend (const char *str, long max=-1)
 
SWBufappend (const SWBuf &str, long max=-1)
 
SWBufappend (char ch)
 
SWBufappend (const unsigned char ch)
 
SWBufappend (wchar_t wch)
 
SWBufappendFormatted (const char *format,...)
 
const char * c_str () const
 
char & charAt (unsigned long pos)
 
const char & charAt (unsigned long pos) const
 
char & charAtGuarded (unsigned long pos)
 
int compare (const SWBuf &other) const
 
int compare (const char *other) const
 
bool endsWith (const SWBuf &postfix) const
 
bool endsWith (const char *postfix) const
 
char getFillByte ()
 
char * getRawData ()
 
long indexOf (const SWBuf &needle) const
 
void insert (unsigned long pos, const char *str, unsigned long start=0, signed long max=-1)
 
void insert (unsigned long pos, const SWBuf &str, unsigned long start=0, signed long max=-1)
 
void insert (unsigned long pos, char c)
 
unsigned long length () const
 
 operator const char * () const
 
bool operator!= (const SWBuf &other) const
 
bool operator!= (const char *other) const
 
SWBuf operator+ (const SWBuf &other) const
 
SWBuf operator+ (char ch) const
 
SWBufoperator+= (const char *str)
 
SWBufoperator+= (char ch)
 
SWBufoperator-- (int)
 
SWBufoperator-= (unsigned long len)
 
bool operator< (const SWBuf &other) const
 
bool operator< (const char *other) const
 
SWBufoperator<< (unsigned long n)
 
bool operator<= (const SWBuf &other) const
 
bool operator<= (const char *other) const
 
SWBufoperator= (const char *newVal)
 
SWBufoperator= (const SWBuf &other)
 
bool operator== (const SWBuf &other) const
 
bool operator== (const char *other) const
 
bool operator> (const SWBuf &other) const
 
bool operator> (const char *other) const
 
bool operator>= (const SWBuf &other) const
 
bool operator>= (const char *other) const
 
SWBufoperator>> (unsigned long n)
 
char & operator[] (unsigned long pos)
 
char & operator[] (long pos)
 
char & operator[] (unsigned int pos)
 
char & operator[] (int pos)
 
const char & operator[] (unsigned long pos) const
 
const char & operator[] (long pos) const
 
const char & operator[] (unsigned int pos) const
 
const char & operator[] (int pos) const
 
SWBufreplaceBytes (const char *targets, char newByte)
 
void resize (unsigned long len)
 
void set (const SWBuf &newVal)
 
void set (const char *newVal, unsigned long maxSize=0)
 
void setFillByte (char ch)
 
SWBufsetFormatted (const char *format,...)
 
SWBufsetFormattedVA (const char *format, va_list argptr)
 
void setSize (unsigned long len)
 
unsigned long size () const
 
void size (unsigned long newSize)
 
bool startsWith (const SWBuf &prefix) const
 
bool startsWith (const char *prefix) const
 
const char * stripPrefix (char separator, bool endOfStringAsSeparator=false)
 
 SWBuf ()
 
 SWBuf (const char *initVal, unsigned long initSize=0)
 
 SWBuf (const SWBuf &other, unsigned long initSize=0)
 
 SWBuf (char initVal, unsigned long initSize=0)
 
SWBuftoLower ()
 
SWBuftoUpper ()
 
SWBuftrim ()
 
SWBuftrimEnd ()
 
SWBuftrimStart ()
 
 ~SWBuf ()
 

Static Public Attributes

static char * nullStr = (char *)""
 

Private Member Functions

void assureMore (size_t pastEnd)
 
void assureSize (size_t checkSize)
 
void init (size_t initSize)
 

Private Attributes

unsigned long allocSize
 
char * buf
 
char * end
 
char * endAlloc
 
char fillByte
 

Detailed Description

This class is used as a transport and utility for data buffers.

Warning
This class does not perform pointer validity checks (for speed reasons). Therefore, never try to pass an invalid string (const char* 0) as an argument- it will crash your program. You need to perform the checks yourself!

Definition at line 47 of file swbuf.h.

Constructor & Destructor Documentation

SWBuf::SWBuf ( )
inline

Definition at line 93 of file swbuf.h.

93  {
94  init(0);
95  }
void init(size_t initSize)
Definition: swbuf.h:74
SWBuf::SWBuf ( const char *  initVal,
unsigned long  initSize = 0 
)
inline

Definition at line 102 of file swbuf.h.

102  {
103  init(initSize);
104  if (initVal)
105  set(initVal, initSize);
106  }
void init(size_t initSize)
Definition: swbuf.h:74
void set(const SWBuf &newVal)
Definition: swbuf.h:204
SWBuf::SWBuf ( const SWBuf other,
unsigned long  initSize = 0 
)
inline

Definition at line 113 of file swbuf.h.

113  {
114  init(initSize);
115  set(other);
116  }
void init(size_t initSize)
Definition: swbuf.h:74
void set(const SWBuf &newVal)
Definition: swbuf.h:204
SWBuf::SWBuf ( char  initVal,
unsigned long  initSize = 0 
)
inline

Definition at line 123 of file swbuf.h.

123  {
124  init(initSize+1);
125  *buf = initVal;
126  end = buf+1;
127  *end = 0;
128  }
char * buf
Definition: swbuf.h:50
char * end
Definition: swbuf.h:51
void init(size_t initSize)
Definition: swbuf.h:74
SWBuf::~SWBuf ( )
inline

Definition at line 135 of file swbuf.h.

135  {
136  if ((buf) && (buf != nullStr))
137  free(buf);
138  }
char * buf
Definition: swbuf.h:50
free(preg->fastmap)
static char * nullStr
Definition: swbuf.h:87

Member Function Documentation

SWBuf& SWBuf::append ( const char *  str,
long  max = -1 
)
inline

SWBuf::append - appends a value to the current value of this SWBuf. If the allocated memory is not enough, it will be resized accordingly.

Parameters
strAppend this.
maxAppend only max chars.

Definition at line 274 of file swbuf.h.

274  {
275  // if (!str) //A null string was passed
276  // return;
277  if (max < 0)
278  max = strlen(str);
279  assureMore(max+1);
280  for (;((max)&&(*str));max--)
281  *end++ = *str++;
282  *end = 0;
283  return *this;
284  }
char * end
Definition: swbuf.h:51
void assureMore(size_t pastEnd)
Definition: swbuf.h:56
SWBuf& SWBuf::append ( const SWBuf str,
long  max = -1 
)
inline

SWBuf::append - appends a value to the current value of this SWBuf If the allocated memory is not enough, it will be resized accordingly.

Parameters
strAppend this.
maxAppend only max chars.

Definition at line 292 of file swbuf.h.

292 { return append(str.c_str(), max); }
const char * c_str() const
Definition: swbuf.h:158
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
SWBuf& SWBuf::append ( char  ch)
inline

SWBuf::append - appends a value to the current value of this SWBuf If the allocated memory is not enough, it will be resized accordingly.

Parameters
chAppend this.

Definition at line 299 of file swbuf.h.

299  {
300  assureMore(1);
301  *end++ = ch;
302  *end = 0;
303  return *this;
304  }
char * end
Definition: swbuf.h:51
void assureMore(size_t pastEnd)
Definition: swbuf.h:56
SWBuf& SWBuf::append ( const unsigned char  ch)
inline

Definition at line 305 of file swbuf.h.

305  {
306  assureMore(1);
307  *end++ = ch;
308  *end = 0;
309  return *this;
310  }
char * end
Definition: swbuf.h:51
void assureMore(size_t pastEnd)
Definition: swbuf.h:56
SWBuf& SWBuf::append ( wchar_t  wch)
inline

SWBuf::append - appends a wide character value to the current value of this SWBuf If the allocated memory is not enough, it will be resized accordingly. NOTE: This is dangerous, as wchar_t is currently different sizes on different platforms (stupid windoze; stupid c++ spec for not mandating 4byte).

Parameters
wchAppend this.

Definition at line 319 of file swbuf.h.

319  {
320  assureMore(sizeof(wchar_t)*2);
321  for (unsigned int i = 0; i < sizeof(wchar_t); i++) *end++ = ((char *)&wch)[i];
322  for (unsigned int i = 0; i < sizeof(wchar_t); i++) end[i] = 0;
323  return *this;
324  }
char * end
Definition: swbuf.h:51
void assureMore(size_t pastEnd)
Definition: swbuf.h:56
SWBuf & SWBuf::appendFormatted ( const char *  format,
  ... 
)

SWBuf::appendFormatted - appends formatted strings to the current value of this SWBuf.

Warning
This function can only write at most JUNKBUFSIZE to the string per call.
This function is not very fast. For loops with many iterations you might consider replacing it by other calls. Example:
SWBuf buf.appendFormatted("<%s>", stringVal);
should be replaced by:
buf.append("<"); buf.append(stringVal); buf.append(">");
This will produce much faster results.
Parameters
formatThe format string. Same syntax as printf, for example.
...Add all arguments here.

Definition at line 81 of file swbuf.cpp.

81  {
82  va_list argptr;
83 
84  va_start(argptr, format);
85 #ifdef NO_VSNPRINTF
86  static char junkBuf[JUNKBUFSIZE];
87  int len = vsprintf(junkBuf, format, argptr)+1;
88 #else
89  int len = vsnprintf(0, 0, format, argptr)+1;
90 #endif
91  va_end(argptr);
92  assureMore(len);
93  va_start(argptr, format);
94  end += vsprintf(end, format, argptr);
95  va_end(argptr);
96  return *this;
97 }
char * end
Definition: swbuf.h:51
void assureMore(size_t pastEnd)
Definition: swbuf.h:56
#define JUNKBUFSIZE
Definition: swbuf.h:38
void SWBuf::assureMore ( size_t  pastEnd)
inlineprivate

Definition at line 56 of file swbuf.h.

56  {
57  if (size_t(endAlloc-end) < pastEnd) {
58  assureSize(allocSize + pastEnd);
59  }
60  }
char * end
Definition: swbuf.h:51
void assureSize(size_t checkSize)
Definition: swbuf.h:62
unsigned long allocSize
Definition: swbuf.h:54
char * endAlloc
Definition: swbuf.h:52
void SWBuf::assureSize ( size_t  checkSize)
inlineprivate

Definition at line 62 of file swbuf.h.

62  {
63  if (checkSize > allocSize) {
64  long size = (end - buf);
65  checkSize += 128;
66  buf = (char *)((allocSize) ? realloc(buf, checkSize) : malloc(checkSize));
67  allocSize = checkSize;
68  end = (buf + size);
69  *end = 0;
70  endAlloc = buf + allocSize - 1;
71  }
72  }
char * buf
Definition: swbuf.h:50
char * end
Definition: swbuf.h:51
char * malloc()
char * realloc()
unsigned long size() const
Definition: swbuf.h:185
unsigned long allocSize
Definition: swbuf.h:54
char * endAlloc
Definition: swbuf.h:52
const char* SWBuf::c_str ( ) const
inline
Returns
a pointer to the buffer content (null-terminated string)

Definition at line 158 of file swbuf.h.

158 { return buf; }
char * buf
Definition: swbuf.h:50
char& SWBuf::charAt ( unsigned long  pos)
inline

Definition at line 168 of file swbuf.h.

168 { return *(buf + pos); }
char * buf
Definition: swbuf.h:50
int pos
Definition: regex.c:5534
const char& SWBuf::charAt ( unsigned long  pos) const
inline

Definition at line 169 of file swbuf.h.

169 { return *(buf + pos); }
char * buf
Definition: swbuf.h:50
int pos
Definition: regex.c:5534
char& SWBuf::charAtGuarded ( unsigned long  pos)
inline
Parameters
posThe position of the requested character.
Returns
The character at the specified position

Definition at line 165 of file swbuf.h.

165 { return ((pos <= (unsigned long)(endAlloc - buf)) ? buf[pos] : (*endAlloc)); }
char * buf
Definition: swbuf.h:50
int pos
Definition: regex.c:5534
char * endAlloc
Definition: swbuf.h:52
int SWBuf::compare ( const SWBuf other) const
inline

Definition at line 508 of file swbuf.h.

508 { return strcmp(c_str(), other.c_str()); }
const char * c_str() const
Definition: swbuf.h:158
int SWBuf::compare ( const char *  other) const
inline

Definition at line 527 of file swbuf.h.

527 { return (other?strcmp(c_str(), other):-1); }
const char * c_str() const
Definition: swbuf.h:158
bool SWBuf::endsWith ( const SWBuf postfix) const
inline
Returns
returns true if this buffer ends with the specified postfix

Definition at line 501 of file swbuf.h.

501 { return (size() >= postfix.size())?!strncmp(end-postfix.size(), postfix.c_str(), postfix.size()):false; }
char * end
Definition: swbuf.h:51
const char * c_str() const
Definition: swbuf.h:158
unsigned long size() const
Definition: swbuf.h:185
bool SWBuf::endsWith ( const char *  postfix) const
inline
Returns
returns true if this buffer ends with the specified postfix

Definition at line 524 of file swbuf.h.

524 { unsigned int psize = (unsigned int)strlen(postfix); return (size() >= psize)?!strncmp(end-psize, postfix, psize):false; }
char * end
Definition: swbuf.h:51
unsigned long size() const
Definition: swbuf.h:185
char SWBuf::getFillByte ( )
inline

SWBuf::getFillByte - Get the fillByte character

Returns
The character used for filling memory.
See Also
setFillByte.

Definition at line 153 of file swbuf.h.

153 { return fillByte; }
char fillByte
Definition: swbuf.h:53
char* SWBuf::getRawData ( )
inline

SWBuf::getRawData

Warning
be careful! Probably setSize needs to be called in conjunction before and maybe after
Returns
Pointer to the allocated memory of the SWBuf.

Definition at line 379 of file swbuf.h.

379 { return buf; }
char * buf
Definition: swbuf.h:50
long SWBuf::indexOf ( const SWBuf needle) const
inline
Returns
returns the index of a substring if it is found in this buffer; otherwise, returns < 0

Definition at line 506 of file swbuf.h.

506 { const char *ch = strstr(buf, needle.c_str()); return (ch) ? ch - buf : -1; }
char * buf
Definition: swbuf.h:50
const char * c_str() const
Definition: swbuf.h:158
void SWBuf::init ( size_t  initSize)
inlineprivate

Definition at line 74 of file swbuf.h.

74  {
75  fillByte = ' ';
76  allocSize = 0;
77  buf = nullStr;
78  end = buf;
79  endAlloc = buf;
80  if (initSize)
81  assureSize(initSize);
82  }
char * buf
Definition: swbuf.h:50
char * end
Definition: swbuf.h:51
void assureSize(size_t checkSize)
Definition: swbuf.h:62
char fillByte
Definition: swbuf.h:53
static char * nullStr
Definition: swbuf.h:87
unsigned long allocSize
Definition: swbuf.h:54
char * endAlloc
Definition: swbuf.h:52
void SWBuf::insert ( unsigned long  pos,
const char *  str,
unsigned long  start = 0,
signed long  max = -1 
)

SWBuf::insert - inserts the given string at position into this string

Parameters
posThe position where to insert. pos=0 inserts at the beginning, pos=1 after the first char, etc. Using pos=length() is the same as calling append(s)
strstring to be inserted
startstart from this position in the string to be inserted
maxInsert only max chars.

Definition at line 99 of file swbuf.cpp.

99  {
100 // if (!str) //A null string was passed
101 // return;
102 
103  str += start;
104  int len = (int)((max > -1) ? max : strlen(str));
105 
106  if (!len || (pos > length())) //nothing to do, return
107  return;
108 
109  // pos==length(), so we can call append in this case
110  if (pos == length()) { //append is more efficient
111  append(str, max);
112  return;
113  }
114 
115  assureMore( len );
116 
117  memmove(buf + pos + len, buf + pos, (end - buf) - pos); //make a gap of "len" bytes
118  memcpy(buf+pos, str, len);
119 
120  end += len;
121  *end = 0;
122 }
char * buf
Definition: swbuf.h:50
unsigned long length() const
Definition: swbuf.h:197
int pos
Definition: regex.c:5534
char * end
Definition: swbuf.h:51
void assureMore(size_t pastEnd)
Definition: swbuf.h:56
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
void SWBuf::insert ( unsigned long  pos,
const SWBuf str,
unsigned long  start = 0,
signed long  max = -1 
)
inline

SWBuf::insert - inserts the given string at position into this string

Parameters
posThe position where to insert. pos=0 inserts at the beginning, pos=1 after the first char, etc. Using pos=length() is the same as calling append(s)
strstring to be inserted
startstart from this position in the string to be inserted
maxInsert only max chars.

Definition at line 360 of file swbuf.h.

360  {
361  insert(pos, str.c_str(), start, max);
362  };
int pos
Definition: regex.c:5534
void insert(unsigned long pos, const char *str, unsigned long start=0, signed long max=-1)
Definition: swbuf.cpp:99
const char * c_str() const
Definition: swbuf.h:158
void SWBuf::insert ( unsigned long  pos,
char  c 
)
inline

SWBuf::insert - inserts the given character at position into this string

Parameters
posThe position where to insert. pos=0 inserts at the beginning, pos=1 after the first char, etc. Using pos=length() is the same as calling append(s)
cInsert this.

Definition at line 369 of file swbuf.h.

369  {
370  insert(pos, SWBuf(c));
371  }
int pos
Definition: regex.c:5534
void insert(unsigned long pos, const char *str, unsigned long start=0, signed long max=-1)
Definition: swbuf.cpp:99
SWBuf()
Definition: swbuf.h:93
unsigned long SWBuf::length ( ) const
inline
Returns
size() and length() return only the number of characters of the string.

Definition at line 197 of file swbuf.h.

197 { return end - buf; }
char * buf
Definition: swbuf.h:50
char * end
Definition: swbuf.h:51
SWBuf::operator const char * ( ) const
inline

Definition at line 381 of file swbuf.h.

381 { return c_str(); }
const char * c_str() const
Definition: swbuf.h:158
bool SWBuf::operator!= ( const SWBuf other) const
inline

Definition at line 510 of file swbuf.h.

510 { return compare(other) != 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
bool SWBuf::operator!= ( const char *  other) const
inline

Definition at line 529 of file swbuf.h.

529 { return compare(other) != 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
SWBuf SWBuf::operator+ ( const SWBuf other) const
inline

Concatenate another buffer to the end of this buffer

Definition at line 419 of file swbuf.h.

419  {
420  SWBuf retVal = buf;
421  retVal += other;
422  return retVal;
423  }
char * buf
Definition: swbuf.h:50
Definition: swbuf.h:47
SWBuf SWBuf::operator+ ( char  ch) const
inline

Concatenate a byte to the end of this buffer

Definition at line 428 of file swbuf.h.

428 { return (*this) + SWBuf(ch); }
SWBuf()
Definition: swbuf.h:93
SWBuf& SWBuf::operator+= ( const char *  str)
inline

Definition at line 392 of file swbuf.h.

392 { return append(str); }
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
SWBuf& SWBuf::operator+= ( char  ch)
inline

Definition at line 393 of file swbuf.h.

393 { return append(ch); }
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
SWBuf& SWBuf::operator-- ( int  )
inline

Decrease the buffer size, discarding the last character

Definition at line 404 of file swbuf.h.

404 { operator -=(1); return *this; }
SWBuf & operator-=(unsigned long len)
Definition: swbuf.h:399
SWBuf& SWBuf::operator-= ( unsigned long  len)
inline

Decrease the buffer size, discarding the last characters

Parameters
lenhow many bytes to decrease the buffer size

Definition at line 399 of file swbuf.h.

399 { setSize(length()-len); return *this; }
unsigned long length() const
Definition: swbuf.h:197
void setSize(unsigned long len)
Definition: swbuf.h:255
bool SWBuf::operator< ( const SWBuf other) const
inline

Definition at line 512 of file swbuf.h.

512 { return compare(other) < 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
bool SWBuf::operator< ( const char *  other) const
inline

Definition at line 531 of file swbuf.h.

531 { return other && compare(other) < 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
SWBuf& SWBuf::operator<< ( unsigned long  n)
inline

Shift the buffer to the left, discarding the first bytes, decreasing the buffer size

Definition at line 409 of file swbuf.h.

409 { if (n && length()) { n = (n<=length())?n:(length()-1); memmove(buf, buf+n, length()-n); (*this)-=n; } return *this; }
char * buf
Definition: swbuf.h:50
unsigned long length() const
Definition: swbuf.h:197
bool SWBuf::operator<= ( const SWBuf other) const
inline

Definition at line 513 of file swbuf.h.

513 { return compare(other) <= 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
bool SWBuf::operator<= ( const char *  other) const
inline

Definition at line 532 of file swbuf.h.

532 { return other && compare(other) <= 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
SWBuf& SWBuf::operator= ( const char *  newVal)
inline

Definition at line 390 of file swbuf.h.

390 { set(newVal); return *this; }
void set(const SWBuf &newVal)
Definition: swbuf.h:204
SWBuf& SWBuf::operator= ( const SWBuf other)
inline

Definition at line 391 of file swbuf.h.

391 { set(other); return *this; }
void set(const SWBuf &newVal)
Definition: swbuf.h:204
bool SWBuf::operator== ( const SWBuf other) const
inline

Definition at line 509 of file swbuf.h.

509 { return compare(other) == 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
bool SWBuf::operator== ( const char *  other) const
inline

Definition at line 528 of file swbuf.h.

528 { return compare(other) == 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
bool SWBuf::operator> ( const SWBuf other) const
inline

Definition at line 511 of file swbuf.h.

511 { return compare(other) > 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
bool SWBuf::operator> ( const char *  other) const
inline

Definition at line 530 of file swbuf.h.

530 { return other && compare(other) > 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
bool SWBuf::operator>= ( const SWBuf other) const
inline

Definition at line 514 of file swbuf.h.

514 { return compare(other) >= 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
bool SWBuf::operator>= ( const char *  other) const
inline

Definition at line 533 of file swbuf.h.

533 { return other && compare(other) >= 0; }
int compare(const SWBuf &other) const
Definition: swbuf.h:508
SWBuf& SWBuf::operator>> ( unsigned long  n)
inline

Shift the buffer to the right, increasing the buffer size

Definition at line 414 of file swbuf.h.

414 { setSize(length()+n); memmove(buf+n, buf, length()-n); return *this; }
char * buf
Definition: swbuf.h:50
unsigned long length() const
Definition: swbuf.h:197
void setSize(unsigned long len)
Definition: swbuf.h:255
char& SWBuf::operator[] ( unsigned long  pos)
inline

Definition at line 382 of file swbuf.h.

382 { return charAt(pos); }
int pos
Definition: regex.c:5534
char & charAt(unsigned long pos)
Definition: swbuf.h:168
char& SWBuf::operator[] ( long  pos)
inline

Definition at line 383 of file swbuf.h.

383 { return charAt((unsigned long)pos); }
int pos
Definition: regex.c:5534
char & charAt(unsigned long pos)
Definition: swbuf.h:168
char& SWBuf::operator[] ( unsigned int  pos)
inline

Definition at line 384 of file swbuf.h.

384 { return charAt((unsigned long)pos); }
int pos
Definition: regex.c:5534
char & charAt(unsigned long pos)
Definition: swbuf.h:168
char& SWBuf::operator[] ( int  pos)
inline

Definition at line 385 of file swbuf.h.

385 { return charAt((unsigned long)pos); }
int pos
Definition: regex.c:5534
char & charAt(unsigned long pos)
Definition: swbuf.h:168
const char& SWBuf::operator[] ( unsigned long  pos) const
inline

Definition at line 386 of file swbuf.h.

386 { return charAt(pos); }
int pos
Definition: regex.c:5534
char & charAt(unsigned long pos)
Definition: swbuf.h:168
const char& SWBuf::operator[] ( long  pos) const
inline

Definition at line 387 of file swbuf.h.

387 { return charAt((unsigned long)pos); }
int pos
Definition: regex.c:5534
char & charAt(unsigned long pos)
Definition: swbuf.h:168
const char& SWBuf::operator[] ( unsigned int  pos) const
inline

Definition at line 388 of file swbuf.h.

388 { return charAt((unsigned long)pos); }
int pos
Definition: regex.c:5534
char & charAt(unsigned long pos)
Definition: swbuf.h:168
const char& SWBuf::operator[] ( int  pos) const
inline

Definition at line 389 of file swbuf.h.

389 { return charAt((unsigned long)pos); }
int pos
Definition: regex.c:5534
char & charAt(unsigned long pos)
Definition: swbuf.h:168
SWBuf& SWBuf::replaceBytes ( const char *  targets,
char  newByte 
)
inline

Replace with a new byte value all occurances in this buffer of any byte value specified in a set

Parameters
targetsa set of bytes, any of which will be replaced
newBytevalue to use as replacement or 0 to remove matching byte.

Example: replaceBytes("abc", 'z'); // replaces all occurances of 'a', 'b', and 'c' with 'z'

Definition at line 467 of file swbuf.h.

467  {
468  for (unsigned int i = 0; (i < size()); i++) {
469  if (strchr(targets, buf[i])) {
470  if (newByte) buf[i] = newByte;
471  // delete byte
472  else {
473  if (i < (size()-1)) {
474  memmove(buf+i, buf+i+1, length()-i-1);
475  }
476  (*this)-=1;
477  }
478  }
479  }
480  return *this;
481  }
char * buf
Definition: swbuf.h:50
unsigned long length() const
Definition: swbuf.h:197
unsigned long size() const
Definition: swbuf.h:185
void SWBuf::resize ( unsigned long  len)
inline

SWBuf::resize - Resize this buffer to a specific length.

Parameters
lenThe new size of the buffer. One byte for the null will be added.

Definition at line 266 of file swbuf.h.

266 { setSize(len); }
void setSize(unsigned long len)
Definition: swbuf.h:255
void SWBuf::set ( const SWBuf newVal)
inline

SWBuf::set - sets this buf to a new value If the allocated memory is bigger than the new string, it will NOT be resized.

Parameters
newValthe value to set this buffer to.

Definition at line 204 of file swbuf.h.

204  {
205  unsigned long len = newVal.allocSize;
206  assureSize(len);
207  memcpy(buf, newVal.c_str(), len);
208  end = buf + (newVal.length());
209  }
char * buf
Definition: swbuf.h:50
unsigned long length() const
Definition: swbuf.h:197
char * end
Definition: swbuf.h:51
const char * c_str() const
Definition: swbuf.h:158
void assureSize(size_t checkSize)
Definition: swbuf.h:62
unsigned long allocSize
Definition: swbuf.h:54
void SWBuf::set ( const char *  newVal,
unsigned long  maxSize = 0 
)
inline

SWBuf::set - sets this buf to a new value. If the allocated memory is bigger than the new string, it will NOT be resized.

Parameters
newValthe value to set this buffer to.

Definition at line 216 of file swbuf.h.

216  {
217  if (newVal) {
218  unsigned long len = strlen(newVal) + 1;
219  if (maxSize && maxSize < (len-1)) len = maxSize + 1;
220  assureSize(len);
221  memcpy(buf, newVal, len);
222  end = buf + (len - 1);
223  }
224  else {
225  assureSize(1);
226  end = buf;
227  }
228  *end = 0;
229  }
char * buf
Definition: swbuf.h:50
char * end
Definition: swbuf.h:51
void assureSize(size_t checkSize)
Definition: swbuf.h:62
void SWBuf::setFillByte ( char  ch)
inline

SWBuf::setFillByte - Set the fillByte character

Parameters
chThis character is used when the SWBuf is (re)sized. The memory will be filled with this character.
See Also
setSize()
resize()

Definition at line 146 of file swbuf.h.

146 { fillByte = ch; }
char fillByte
Definition: swbuf.h:53
SWBuf & SWBuf::setFormatted ( const char *  format,
  ... 
)

SWBuf::setFormatted - sets this buf to a formatted string. If the allocated memory is bigger than the new string, it will NOT be resized.

Warning
This function can only write at most JUNKBUFSIZE to the string per call.
This function is not very fast. For loops with many iterations you might consider replacing it by other calls. Example:
SWBuf buf.setFormatted("<%s>", stringVal);
should be replaced by:
buf.set("<"); buf.append(stringVal); buf.append(">");
This will produce much faster results.
Parameters
formatThe format string. Same syntax as printf, for example.
...Add all arguments here.

Definition at line 50 of file swbuf.cpp.

50  {
51  va_list argptr;
52 
53  va_start(argptr, format);
54 
55  setFormattedVA(format, argptr);
56 
57  va_end(argptr);
58  return *this;
59 }
SWBuf & setFormattedVA(const char *format, va_list argptr)
Definition: swbuf.cpp:61
SWBuf & SWBuf::setFormattedVA ( const char *  format,
va_list  argptr 
)

Definition at line 61 of file swbuf.cpp.

61  {
62  va_list argptr2;
63  va_copy(argptr2, argptr);
64 #ifdef NO_VSNPRINTF
65  static char junkBuf[JUNKBUFSIZE];
66  int len = vsprintf(junkBuf, format, argptr)+1;
67 #else
68  int len = vsnprintf(0, 0, format, argptr)+1;
69 #endif
70  assureSize(len);
71  end = vsprintf(buf, format, argptr2) + buf;
72  va_end(argptr2);
73  return *this;
74 }
char * buf
Definition: swbuf.h:50
char * end
Definition: swbuf.h:51
#define JUNKBUFSIZE
Definition: swbuf.h:38
void assureSize(size_t checkSize)
Definition: swbuf.h:62
void SWBuf::setSize ( unsigned long  len)
inline

SWBuf::setSize - Size this buffer to a specific length.

Parameters
lenThe new size of the buffer. One byte for the null will be added.

Definition at line 255 of file swbuf.h.

255  {
256  assureSize(len+1);
257  if ((unsigned)(end - buf) < len)
258  memset(end, fillByte, (size_t)len - (end-buf));
259  end = buf + len;
260  *end = 0;
261  }
char * buf
Definition: swbuf.h:50
char * end
Definition: swbuf.h:51
void assureSize(size_t checkSize)
Definition: swbuf.h:62
char fillByte
Definition: swbuf.h:53
unsigned long SWBuf::size ( ) const
inline
Returns
size() and length() return only the number of characters of the string. Add one for the following null and one for each char to be appended!

Definition at line 185 of file swbuf.h.

185 { return length(); }
unsigned long length() const
Definition: swbuf.h:197
void SWBuf::size ( unsigned long  newSize)
inline

set's the size of the buffer. This is a quick inline method which checks for changes before actually calling setSize().

Parameters
newSizenew size of the buffer

Definition at line 192 of file swbuf.h.

192 { if (end - buf - newSize) setSize(newSize); }
char * buf
Definition: swbuf.h:50
char * end
Definition: swbuf.h:51
void setSize(unsigned long len)
Definition: swbuf.h:255
bool SWBuf::startsWith ( const SWBuf prefix) const
inline
Returns
returns true if this buffer starts with the specified prefix

Definition at line 486 of file swbuf.h.

486 { return !strncmp(c_str(), prefix.c_str(), prefix.size()); }
const char * c_str() const
Definition: swbuf.h:158
unsigned long size() const
Definition: swbuf.h:185
bool SWBuf::startsWith ( const char *  prefix) const
inline
Returns
returns true if this buffer starts with the specified prefix

Definition at line 519 of file swbuf.h.

519 { return !strncmp(c_str(), prefix, strlen(prefix)); }
const char * c_str() const
Definition: swbuf.h:158
const char* SWBuf::stripPrefix ( char  separator,
bool  endOfStringAsSeparator = false 
)
inline

Strip a prefix from this buffer up to a separator byte. Returns the prefix and modifies this buffer, shifting left to remove prefix

Parameters
separatorto use (e.g. ':')
endOfStringAsSeparator- also count end of string as separator. this is useful for tokenizing entire string like: x|y|z if true it will also include 'z'.
Returns
prefix if separator character found; otherwise, null and leaves buffer unmodified

Definition at line 457 of file swbuf.h.

457 { const char *m = strchr(buf, separator); if (!m && endOfStringAsSeparator) { if (*buf) { operator >>(1); *buf=0; end = buf; return buf + 1;} else return buf; } if (m) { int len = (int)(m-buf); char *hold = new char[len]; memcpy(hold, buf, len); *this << (len+1); memcpy(end+1, hold, len); delete [] hold; end[len+1] = 0; } return (m) ? end+1 : 0; } // safe. we know we don't actually realloc and shrink buffer when shifting, so we can place our return val at end.
char * buf
Definition: swbuf.h:50
char * end
Definition: swbuf.h:51
SWBuf & operator>>(unsigned long n)
Definition: swbuf.h:414
SWBuf & SWBuf::toLower ( )

Converts this SWBuf to lowercase &return this

Definition at line 141 of file swbuf.cpp.

141  {
142  char *utf8 = 0;
143  stdstr(&utf8, c_str(), 3);
144  sword::tolowerstr(utf8, (unsigned int)size()*3-1);
145  *this = utf8;
146  delete [] utf8;
147 
148  return *this;
149 }
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
const char * c_str() const
Definition: swbuf.h:158
char * tolowerstr(char *t, unsigned int max=0)
Definition: stringmgr.h:111
unsigned long size() const
Definition: swbuf.h:185
SWBuf & SWBuf::toUpper ( )

Converts this SWBuf to uppercase &return this

Converts an SWBuf filled with UTF-8 to upper case

Parameters
bSWBuf to change to upper case
Returns
b for convenience

Definition at line 132 of file swbuf.cpp.

132  {
133  char *utf8 = 0;
134  stdstr(&utf8, c_str(), 3);
135  sword::toupperstr(utf8, (unsigned int)size()*3-1);
136  *this = utf8;
137  delete [] utf8;
138 
139  return *this;
140 }
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
const char * c_str() const
Definition: swbuf.h:158
unsigned long size() const
Definition: swbuf.h:185
char * toupperstr(char *t, unsigned int max=0)
Definition: stringmgr.h:107
SWBuf& SWBuf::trim ( )
inline

Trim whitespace from the start and end of this buffer, shifting left and decreasing size as necessary

Definition at line 443 of file swbuf.h.

443 { trimStart(); return trimEnd(); }
SWBuf & trimStart()
Definition: swbuf.h:433
SWBuf & trimEnd()
Definition: swbuf.h:438
SWBuf& SWBuf::trimEnd ( )
inline

Trim whitespace from the end of this buffer, decreasing the size as necessary

Definition at line 438 of file swbuf.h.

438 { while (size() && (strchr("\t\r\n ", *(end-1)))) setSize(size()-1); return *this; }
char * end
Definition: swbuf.h:51
unsigned long size() const
Definition: swbuf.h:185
void setSize(unsigned long len)
Definition: swbuf.h:255
SWBuf& SWBuf::trimStart ( )
inline

Trim whitespace from the start of this buffer, shifting the buffer left as necessary

Definition at line 433 of file swbuf.h.

433 { while (size() && (strchr("\t\r\n ", *(buf)))) *this << 1; return *this; }
char * buf
Definition: swbuf.h:50
unsigned long size() const
Definition: swbuf.h:185

Member Data Documentation

unsigned long SWBuf::allocSize
private

Definition at line 54 of file swbuf.h.

char* SWBuf::buf
private

Definition at line 50 of file swbuf.h.

char* SWBuf::end
private

Definition at line 51 of file swbuf.h.

char* SWBuf::endAlloc
private

Definition at line 52 of file swbuf.h.

char SWBuf::fillByte
private

Definition at line 53 of file swbuf.h.

SWORD_NAMESPACE_START char * SWBuf::nullStr = (char *)""
static

Definition at line 87 of file swbuf.h.


The documentation for this class was generated from the following files: