38 #define JUNKBUFSIZE 65534
57 if (
size_t(endAlloc-end) < pastEnd) {
58 assureSize(allocSize + pastEnd);
63 if (checkSize > allocSize) {
64 long size = (end - buf);
66 buf = (
char *)((allocSize) ?
realloc(buf, checkSize) :
malloc(checkSize));
67 allocSize = checkSize;
70 endAlloc = buf + allocSize - 1;
74 inline void init(
size_t initSize) {
102 inline SWBuf(
const char *initVal,
unsigned long initSize = 0) {
105 set(initVal, initSize);
113 inline SWBuf(
const SWBuf &other,
unsigned long initSize = 0) {
123 inline SWBuf(
char initVal,
unsigned long initSize = 0) {
136 if ((buf) && (buf != nullStr))
158 inline const char *
c_str()
const{
return buf; }
165 inline char &
charAtGuarded(
unsigned long pos) {
return ((pos <= (
unsigned long)(endAlloc - buf)) ? buf[pos] : (*endAlloc)); }
169 inline const char &
charAt(
unsigned long pos)
const {
return *(buf +
pos); }
192 inline void size(
unsigned long newSize) {
if (end - buf - newSize) setSize(newSize); }
197 inline unsigned long length()
const {
return end - buf; }
207 memcpy(buf, newVal.
c_str(), len);
208 end = buf + (newVal.
length());
216 inline void set(
const char *newVal,
unsigned long maxSize = 0) {
218 unsigned long len = strlen(newVal) + 1;
219 if (maxSize && maxSize < (len-1)) len = maxSize + 1;
221 memcpy(buf, newVal, len);
222 end = buf + (len - 1);
248 SWBuf &setFormatted(
const char *format, ...);
249 SWBuf &setFormattedVA(
const char *format, va_list argptr);
257 if ((
unsigned)(end - buf) < len)
258 memset(end, fillByte, (
size_t)len - (end-buf));
266 inline void resize(
unsigned long len) { setSize(len); }
280 for (;((max)&&(*str));max--)
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;
342 SWBuf &appendFormatted(
const char *format, ...);
351 void insert(
unsigned long pos,
const char* str,
unsigned long start = 0,
signed long max = -1);
360 inline void insert(
unsigned long pos,
const SWBuf &str,
unsigned long start = 0,
signed long max = -1) {
361 insert(pos, str.
c_str(), start, max);
370 insert(pos,
SWBuf(c));
381 inline operator const char *()
const {
return c_str(); }
384 inline char &
operator[](
unsigned int pos) {
return charAt((
unsigned long)pos); }
386 inline const char &
operator[](
unsigned long pos)
const {
return charAt(pos); }
387 inline const char &
operator[](
long pos)
const {
return charAt((
unsigned long)pos); }
388 inline const char &
operator[](
unsigned int pos)
const {
return charAt((
unsigned long)pos); }
389 inline const char &
operator[](
int pos)
const {
return charAt((
unsigned long)pos); }
390 inline SWBuf &operator =(
const char *newVal) { set(newVal);
return *
this; }
391 inline SWBuf &operator =(
const SWBuf &other) { set(other);
return *
this; }
392 inline SWBuf &operator +=(
const char *str) {
return append(str); }
393 inline SWBuf &operator +=(
char ch) {
return append(ch); }
399 inline SWBuf &operator -=(
unsigned long len) { setSize(
length()-len);
return *
this; }
404 inline SWBuf &operator --(
int) { operator -=(1);
return *
this; }
409 inline SWBuf &operator <<(
unsigned long n) {
if (n &&
length()) { n = (n<=
length())?n:(
length()-1); memmove(buf, buf+n,
length()-n); (*this)-=n; }
return *
this; }
414 inline SWBuf &operator >>(
unsigned long n) { setSize(
length()+n); memmove(buf+n, buf,
length()-n);
return *
this; }
428 inline SWBuf operator +(
char ch)
const {
return (*
this) +
SWBuf(ch); }
433 inline SWBuf &
trimStart() {
while (
size() && (strchr(
"\t\r\n ", *(buf)))) *
this << 1;
return *
this; }
457 inline const char *
stripPrefix(
char separator,
bool endOfStringAsSeparator =
false) {
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; }
468 for (
unsigned int i = 0; (i <
size()); i++) {
469 if (strchr(targets, buf[i])) {
470 if (newByte) buf[i] = newByte;
473 if (i < (
size()-1)) {
474 memmove(buf+i, buf+i+1,
length()-i-1);
506 inline long indexOf(
const SWBuf &needle)
const {
const char *ch = strstr(buf, needle.
c_str());
return (ch) ? ch - buf : -1; }
509 inline bool operator ==(
const SWBuf &other)
const {
return compare(other) == 0; }
510 inline bool operator !=(
const SWBuf &other)
const {
return compare(other) != 0; }
511 inline bool operator > (
const SWBuf &other)
const {
return compare(other) > 0; }
512 inline bool operator < (
const SWBuf &other)
const {
return compare(other) < 0; }
513 inline bool operator <=(
const SWBuf &other)
const {
return compare(other) <= 0; }
514 inline bool operator >=(
const SWBuf &other)
const {
return compare(other) >= 0; }
519 inline bool startsWith(
const char *prefix)
const {
return !strncmp(c_str(), prefix, strlen(prefix)); }
524 inline bool endsWith(
const char *postfix)
const {
unsigned int psize = (
unsigned int)strlen(postfix);
return (
size() >= psize)?!strncmp(end-psize, postfix, psize):
false; }
527 inline int compare(
const char *other)
const {
return (other?strcmp(c_str(), other):-1); }
528 inline bool operator ==(
const char *other)
const {
return compare(other) == 0; }
529 inline bool operator !=(
const char *other)
const {
return compare(other) != 0; }
530 inline bool operator > (
const char *other)
const {
return other && compare(other) > 0; }
531 inline bool operator < (
const char *other)
const {
return other && compare(other) < 0; }
532 inline bool operator <=(
const char *other)
const {
return other && compare(other) <= 0; }
533 inline bool operator >=(
const char *other)
const {
return other && compare(other) >= 0; }
void setFillByte(char ch)
#define SWORD_NAMESPACE_START
SWBuf & append(const unsigned char ch)
char & operator[](unsigned long pos)
unsigned long length() const
char & charAt(unsigned long pos)
bool startsWith(const SWBuf &prefix) const
void assureMore(size_t pastEnd)
void size(unsigned long newSize)
void resize(unsigned long len)
SWBuf(char initVal, unsigned long initSize=0)
void insert(unsigned long pos, const SWBuf &str, unsigned long start=0, signed long max=-1)
bool endsWith(const SWBuf &postfix) const
void set(const char *newVal, unsigned long maxSize=0)
const char & operator[](unsigned long pos) const
const char & charAt(unsigned long pos) const
const char * c_str() const
const char & operator[](long pos) const
SWBuf & append(const char *str, long max=-1)
void assureSize(size_t checkSize)
void insert(unsigned long pos, char c)
SWBuf(const SWBuf &other, unsigned long initSize=0)
bool endsWith(const char *postfix) const
SWBuf & append(wchar_t wch)
void init(size_t initSize)
SWBuf & replaceBytes(const char *targets, char newByte)
unsigned long size() const
const char * stripPrefix(char separator, bool endOfStringAsSeparator=false)
bool startsWith(const char *prefix) const
SWBuf & append(const SWBuf &str, long max=-1)
char & charAtGuarded(unsigned long pos)
SWBuf(const char *initVal, unsigned long initSize=0)
void set(const SWBuf &newVal)
#define SWORD_NAMESPACE_END
char & operator[](unsigned int pos)
long indexOf(const SWBuf &needle) const
char & operator[](long pos)
char & operator[](int pos)
int compare(const char *other) const
const char & operator[](int pos) const
const char & operator[](unsigned int pos) const
void setSize(unsigned long len)
int compare(const SWBuf &other) const