#ifndef _SSTRING_SIMPLE_H_ #define _SSTRING_SIMPLE_H_ #include #include #include #include class ExtValue; //this include would result in recurrent inclusion: #include "extvalue.h" class ExtObject; class SString { private: char *txt; ///< string buffer or NULL for empty string int allocated; ///< allocated memory (including \0) int used; ///< string length int appending; ///< append mode, changes can occur after character # 'appending' void initEmpty(); void copyFrom(SString &from); ///< copy from SString void reallocate(int newsize); void ensureAllocated(int needed); const char* getPtr() const { return txt ? txt : ""; } public: SString(); ///< make an empty string SString(const char*t, int t_len = -1); ///< make a string from char* SString(int x) = delete; ///< disallow the former 'int' constructor (so the new 'char' version is not used through implicit conversion) SString(const SString& from); ///< duplicate string SString(SString&& from);///< move SString(char in); ~SString(); void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown void* operator new(size_t s, void* mem) { return mem; } #ifdef _MSC_VER void operator delete(void* mem, void* t) {} #endif void* operator new(size_t s) { return malloc(sizeof(SString)); } void operator delete(void* mem) { free(mem); } int length() const { return used; } ///< get string length int size() const { return length(); } ///< get string length void shrink(); ///< free unnecessary buffer void reserve(int cap) { ensureAllocated(cap + 1); } ///< like in std::string /// after this call, you can modify sstring directly. /// returned value is the pointer to the internal buffer. /// ensuresize is minimal value of bytes you need, /// the buffer will be resized as needed. /// all "direct" operations have to leave the buffer with trailing '\0' /// at the end. endWrite() will search for this value in order to determine /// new string length. ///

Sample: /// SString t; /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b); /// t.endWrite(); char *directWrite(int ensuresize = -1); //char *directWrite(); /// like directWrite, but it returns the pointer to the first char after current string /// for easy appending. maxappend is minimum of character in buffer /// that can be appended after this call. ///

Sample: /// SString t; /// sprintf(t.directAppend(10),"c=%d",c); /// t.endAppend(); char *directAppend(int maxappend = 0); /// update string length, after directWrite. /// you don't have to to call endWrite after directWrite if the string's length doesn't change. /// optional newlength parameter gives a chance to further optimize /// this operation if you know exact length of resulting string. ///

Sample: /// SString t("samplestring"); /// strncpy(t.directWrite(50),src,bytecount); /// t.endWrite(bytecount); void endWrite(int newlength = -1); /// update string length, after directAppend. /// you will usually need to call endAppend (or endWrite) after directAppend, /// because the purpose of directAppend is to change string's length. /// optional appendlength parameter gives a chance to further optimize /// this operation if you know exact length of the appended string. ///

Sample: /// SString t("samplestring"); /// strncpy(t.directAppend(50),src,bytecount); /// t.endAppend(bytecount); void endAppend(int appendlength = -1); int capacity() { return (allocated > 0) ? (allocated - 1) : allocated; } ///< std::string.capacity() /// find a character in SString. /// return index if the character was found or -1 otherwise. int indexOf(int character, int start = 0) const; /// find a substring. /// return index if the substring was found or -1 otherwise. int indexOf(const char *substring, int start = 0) const; /// find a substring. /// return index if the substring was found or -1 otherwise. int indexOf(const SString & substring, int start = 0) const; const char* c_str() const { return getPtr(); } ///< get SString's readonly buffer void operator=(const char*t); ///< assign from const char* void operator=(const SString &s); void append(const char *t, int n); SString operator+(const SString &s) const; void operator+=(int x); ///< append x spaces after current string void operator+=(const char*); ///< append char* contents void operator+=(const SString&); ///< append other SString bool equals(const SString &s) const; ///< TRUE if equal bool operator==(const SString &s) const { return equals(s); } ///< TRUE if equal bool operator!=(const SString &s) const { return !equals(s); } ///< TRUE if not equal bool operator<(const SString &s) const { return strcmp(getPtr(), s.getPtr()) < 1; } const char* operator()(int p) const { return getPtr() + p; } ///< pointer to p'th character in SString char operator[](int i) const { return getPtr()[i]; } ///< get char like in array /// return a substring of the current string SString substr(int begin, int length = 1 << 30) const; /// simple tokenization: /// starting at pos, get next substring delimited by separator character /// and put it in output parameter token. /// pos is moved accordingly. /// returns false if no more tokens are available or true otherwise. bool getNextToken(int& pos, SString &token, char separator) const; void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character bool startsWith(const char *pattern) const; char charAt(int pos) const { return operator[](pos); } uint32_t hash() const; static SString valueOf(int); static SString valueOf(long); static SString valueOf(double); static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu static SString sprintf(const char* format, ...); static SString &empty(); }; #endif