@file
#ifndef SSTRING_H
#define SSTRING_H
int CompareCaseInsensitive(const char *a, const char *b);
int CompareNCaseInsensitive(const char *a, const char *b, size_t len);
bool EqualCaseInsensitive(const char *a, const char *b);
#ifdef SCI_NAMESPACE
namespace Scintilla {
#endif
class SContainer {
public:
typedef size_t lenpos_t;
enum { measure_length=0xffffffffU};
protected:
char *s;
lenpos_t sSize;
SContainer() : s(0), sSize(0) {}
~SContainer() {
delete []s;
s = 0;
sSize = 0;
}
lenpos_t size() const {
if (s) {
return sSize;
} else {
return 0;
}
}
public:
@return
static char *StringAllocate(lenpos_t len);
@return
static char *StringAllocate(
const char *s,
lenpos_t len=measure_length);
};
@brief
class SBuffer : protected SContainer {
public:
SBuffer(lenpos_t len) {
s = StringAllocate(len);
if (s) {
*s = '\0';
sSize = len;
} else {
sSize = 0;
}
}
private:
SBuffer(const SBuffer &source) : SContainer() {
s = StringAllocate(source.s, source.sSize);
sSize = (s) ? source.sSize : 0;
}
SBuffer &operator=(const SBuffer &source) {
if (this != &source) {
delete []s;
s = StringAllocate(source.s, source.sSize);
sSize = (s) ? source.sSize : 0;
}
return *this;
}
public:
char *ptr() {
return s;
}
void reset() {
s = 0;
sSize = 0;
}
lenpos_t size() const {
return SContainer::size();
}
};
@brief
@a
class SString : protected SContainer {
lenpos_t sLen;
lenpos_t sizeGrowth;
enum { sizeGrowthDefault = 64 };
bool grow(lenpos_t lenNew);
SString &assign(const char *sOther, lenpos_t sSize_=measure_length);
public:
SString() : sLen(0), sizeGrowth(sizeGrowthDefault) {}
SString(const SString &source) : SContainer(), sizeGrowth(sizeGrowthDefault) {
s = StringAllocate(source.s, source.sLen);
sSize = sLen = (s) ? source.sLen : 0;
}
SString(const char *s_) : sizeGrowth(sizeGrowthDefault) {
s = StringAllocate(s_);
sSize = sLen = (s) ? strlen(s) : 0;
}
SString(SBuffer &buf) : sizeGrowth(sizeGrowthDefault) {
s = buf.ptr();
sSize = sLen = buf.size();
buf.reset();
}
SString(const char *s_, lenpos_t first, lenpos_t last) : sizeGrowth(sizeGrowthDefault) {
s = StringAllocate(s_ + first, last - first);
sSize = sLen = (s) ? last - first : 0;
}
SString(int i);
SString(double d, int precision);
~SString() {
sLen = 0;
}
void clear() {
if (s) {
*s = '\0';
}
sLen = 0;
}
lenpos_t size() const {
return SContainer::size();
}
lenpos_t length() const {
return sLen;
}
char operator[](lenpos_t i) const {
return (s && i < sSize) ? s[i] : '\0';
}
SString &operator=(const char *source) {
return assign(source);
}
SString &operator=(const SString &source) {
if (this != &source) {
assign(source.s, source.sLen);
}
return *this;
}
bool operator==(const SString &sOther) const;
bool operator!=(const SString &sOther) const {
return !operator==(sOther);
}
bool operator==(const char *sOther) const;
bool operator!=(const char *sOther) const {
return !operator==(sOther);
}
bool contains(char ch) const {
return (s && *s) ? strchr(s, ch) != 0 : false;
}
void setsizegrowth(lenpos_t sizeGrowth_) {
sizeGrowth = sizeGrowth_;
}
const char *c_str() const {
return s ? s : "";
}
char *detach() {
char *sRet = s;
s = 0;
sSize = 0;
sLen = 0;
return sRet;
}
SString substr(lenpos_t subPos, lenpos_t subLen=measure_length) const;
SString &lowercase(lenpos_t subPos = 0, lenpos_t subLen=measure_length);
SString &uppercase(lenpos_t subPos = 0, lenpos_t subLen=measure_length);
SString &append(const char *sOther, lenpos_t sLenOther=measure_length, char sep = '\0');
SString &operator+=(const char *sOther) {
return append(sOther, static_cast<lenpos_t>(measure_length));
}
SString &operator+=(const SString &sOther) {
return append(sOther.s, sOther.sLen);
}
SString &operator+=(char ch) {
return append(&ch, 1);
}
SString &appendwithseparator(const char *sOther, char sep) {
return append(sOther, strlen(sOther), sep);
}
SString &insert(lenpos_t pos, const char *sOther, lenpos_t sLenOther=measure_length);
@a@a
@a
@a@a
void remove(lenpos_t pos, lenpos_t len);
SString &change(lenpos_t pos, char ch) {
if (pos < sLen) {
*(s + pos) = ch;
}
return *this;
}
int value() const {
return s ? atoi(s) : 0;
}
bool startswith(const char *prefix);
bool endswith(const char *suffix);
int search(const char *sFind, lenpos_t start=0) const;
bool contains(const char *sFind) const {
return search(sFind) >= 0;
}
int substitute(char chFind, char chReplace);
int substitute(const char *sFind, const char *sReplace);
int remove(const char *sFind) {
return substitute(sFind, "");
}
};
@return
inline char *StringDup(
const char *s,
SContainer::lenpos_t len=SContainer::measure_length)
{
return SContainer::StringAllocate(s, len);
}
#ifdef SCI_NAMESPACE
}
#endif
#endif