Changeset 793 for cpp/frams/util/sstring.h
- Timestamp:
- 05/29/18 16:51:14 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/util/sstring.h
r395 r793 32 32 class SBuf 33 33 { 34 char *txt;35 int used; ///< data size36 int size; ///< buffer size, not including \0, special case: 0==buffer not allocated37 int refcount; ///< buffer is used by 'refcount' objects.38 void initEmpty();39 void ensureSize(int wantsize);40 void copyFrom(const char* ch, int chlen=-1);41 void freeBuf();42 void append(const char* ch, int chlen=-1);43 static SBuf &empty();44 SBuf(int initsize);45 friend class SString;46 SBuf(const SBuf& b) {}34 char *txt; 35 int used; ///< data size 36 int size; ///< buffer size, not including \0, special case: 0==buffer not allocated 37 int refcount; ///< buffer is used by 'refcount' objects. 38 void initEmpty(); 39 void ensureSize(int wantsize); 40 void copyFrom(const char* ch, int chlen = -1); 41 void freeBuf(); 42 void append(const char* ch, int chlen = -1); 43 static SBuf &empty(); 44 SBuf(int initsize); 45 friend class SString; 46 SBuf(const SBuf& b) {} 47 47 public: 48 SBuf();49 ~SBuf();50 uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash48 SBuf(); 49 ~SBuf(); 50 uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash 51 51 }; 52 52 … … 56 56 { 57 57 private: 58 SBuf *buf; ///< buffer59 int appending; ///< append mode, changes can occur after character # 'appending'60 //int memhint;58 SBuf *buf; ///< buffer 59 int appending; ///< append mode, changes can occur after character # 'appending' 60 //int memhint; 61 61 62 void initEmpty();63 int guessMemSize(int request);64 void copyFrom(SString &from); ///< copy from SString, reference if possible65 void detach(); ///< detach from shared buffer, if any66 void detachEmpty(int ensuresize=0); ///< detach and make empty67 void detachCopy(int ensuresize=0); ///< detach and make private copy62 void initEmpty(); 63 int guessMemSize(int request); 64 void copyFrom(SString &from); ///< copy from SString, reference if possible 65 void detach(); ///< detach from shared buffer, if any 66 void detachEmpty(int ensuresize = 0); ///< detach and make empty 67 void detachCopy(int ensuresize = 0); ///< detach and make private copy 68 68 69 69 public: 70 SString(); ///< make an empty string71 SString(const char*t,int t_len=-1); ///< make a string from char*72 SString(int x); ///< string with initial buffer size73 SString(const SString& from); ///< duplicate string74 SString(SString&& from); ///< move75 ~SString();70 SString(); ///< make an empty string 71 SString(const char*t, int t_len = -1); ///< make a string from char* 72 SString(int x); ///< string with initial buffer size 73 SString(const SString& from); ///< duplicate string 74 SString(SString&& from); ///< move 75 ~SString(); 76 76 77 void copyFrom(const char* ch, int chlen=-1); ///< copy string, length of -1 == unknown77 void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown 78 78 79 void* operator new(size_t s, void* mem) {return mem;}79 void* operator new(size_t s, void* mem){ return mem; } 80 80 #ifdef _MSC_VER 81 void operator delete(void* mem, void* t) {}81 void operator delete(void* mem, void* t) {} 82 82 #endif 83 void* operator new(size_t s) {return malloc(sizeof(SString));}84 void operator delete(void* mem) {free(mem);}83 void* operator new(size_t s){ return malloc(sizeof(SString)); } 84 void operator delete(void* mem) { free(mem); } 85 85 86 int len() const {return buf->used;} ///< get string length87 void shrink(); ///< free unnecessary buffer86 int len() const { return buf->used; } ///< get string length 87 void shrink(); ///< free unnecessary buffer 88 88 89 /// after this call, you can modify sstring directly.90 /// returned value is the pointer to the internal buffer.91 /// <B>ensuresize</B> is minimal value of bytes you need,92 /// the buffer will be resized as needed.93 /// all "direct" operations have to leave the buffer with trailing '\0'94 /// at the end. endWrite() will search for this value in order to determine95 /// new string length.96 /// <P>Sample:<CODE>97 /// SString t;98 /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b);99 /// t.endWrite();</CODE>100 char *directWrite(int ensuresize=-1);101 //char *directWrite();102 /// like directWrite, but it returns the pointer to the first char after current string103 /// for easy appending. <B>maxappend</B> is minimum of character in buffer104 /// that can be appended after this call.105 /// <P>Sample:<CODE>106 /// SString t;107 /// sprintf(t.directAppend(10),"c=%d",c);108 /// t.endAppend();</CODE>109 char *directAppend(int maxappend=0);110 /// update string length, after directWrite.111 /// you don't have to to call endWrite after directWrite if the string's length doesn't change.112 /// optional <B>newlength</B> parameter gives a chance to further optimize113 /// this operation if you know exact length of resulting string.114 /// <P>Sample:<CODE>115 /// SString t("samplestring");116 /// strncpy(t.directWrite(50),src,bytecount);117 /// t.endWrite(bytecount);</CODE>118 void endWrite(int newlength=-1);119 /// update string length, after directAppend.120 /// you will usually need to call endAppend (or endWrite) after directAppend,121 /// because the purpose of directAppend is to change string's length.122 /// optional <B>appendlength</B> parameter gives a chance to further optimize123 /// this operation if you know exact length of the appended string.124 /// <P>Sample:<CODE>125 /// SString t("samplestring");126 /// strncpy(t.directAppend(50),src,bytecount);127 /// t.endAppend(bytecount);</CODE>128 void endAppend(int appendlength=-1);129 /// argument is the amount of memory, that will be probably used130 /// by this string instance. string can use this value131 /// to optimize memory allocation (bigger chunks will be allocated).132 void memoryHint(int howbig);133 int directMaxLen() {return buf->size;} ///< when called after directWrite: max number of characters allowed (can be more than requested)89 /// after this call, you can modify sstring directly. 90 /// returned value is the pointer to the internal buffer. 91 /// <B>ensuresize</B> is minimal value of bytes you need, 92 /// the buffer will be resized as needed. 93 /// all "direct" operations have to leave the buffer with trailing '\0' 94 /// at the end. endWrite() will search for this value in order to determine 95 /// new string length. 96 /// <P>Sample:<CODE> 97 /// SString t; 98 /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b); 99 /// t.endWrite();</CODE> 100 char *directWrite(int ensuresize = -1); 101 //char *directWrite(); 102 /// like directWrite, but it returns the pointer to the first char after current string 103 /// for easy appending. <B>maxappend</B> is minimum of character in buffer 104 /// that can be appended after this call. 105 /// <P>Sample:<CODE> 106 /// SString t; 107 /// sprintf(t.directAppend(10),"c=%d",c); 108 /// t.endAppend();</CODE> 109 char *directAppend(int maxappend = 0); 110 /// update string length, after directWrite. 111 /// you don't have to to call endWrite after directWrite if the string's length doesn't change. 112 /// optional <B>newlength</B> parameter gives a chance to further optimize 113 /// this operation if you know exact length of resulting string. 114 /// <P>Sample:<CODE> 115 /// SString t("samplestring"); 116 /// strncpy(t.directWrite(50),src,bytecount); 117 /// t.endWrite(bytecount);</CODE> 118 void endWrite(int newlength = -1); 119 /// update string length, after directAppend. 120 /// you will usually need to call endAppend (or endWrite) after directAppend, 121 /// because the purpose of directAppend is to change string's length. 122 /// optional <B>appendlength</B> parameter gives a chance to further optimize 123 /// this operation if you know exact length of the appended string. 124 /// <P>Sample:<CODE> 125 /// SString t("samplestring"); 126 /// strncpy(t.directAppend(50),src,bytecount); 127 /// t.endAppend(bytecount);</CODE> 128 void endAppend(int appendlength = -1); 129 /// argument is the amount of memory, that will be probably used 130 /// by this string instance. string can use this value 131 /// to optimize memory allocation (bigger chunks will be allocated). 132 void memoryHint(int howbig); 133 int directMaxLen() { return buf->size; } ///< when called after directWrite: max number of characters allowed (can be more than requested) 134 134 135 /// find a character in SString.136 /// return index if the character was found or -1 otherwise.137 int indexOf(int character,int start=0) const;135 /// find a character in SString. 136 /// return index if the character was found or -1 otherwise. 137 int indexOf(int character, int start = 0) const; 138 138 139 /// find a substring.140 /// return index if the substring was found or -1 otherwise.141 int indexOf(const char *substring,int start=0) const;139 /// find a substring. 140 /// return index if the substring was found or -1 otherwise. 141 int indexOf(const char *substring, int start = 0) const; 142 142 143 /// find a substring.144 /// return index if the substring was found or -1 otherwise.145 int indexOf(const SString & substring,int start=0) const;143 /// find a substring. 144 /// return index if the substring was found or -1 otherwise. 145 int indexOf(const SString & substring, int start = 0) const; 146 146 147 const char* c_str() const {return buf->txt;} ///< get SString's readonly buffer148 //operator char*() {detachCopy(len()); return buf->txt;} ///< get SString's writable buffer149 void operator=(const char*t); ///< assign from const char*150 //void operator=(int x) {free(txt);nowy(x);} ///< clear string and make new empty one151 void operator=(const SString &s);147 const char* c_str() const { return buf->txt; } ///< get SString's readonly buffer 148 //operator char*() {detachCopy(len()); return buf->txt;} ///< get SString's writable buffer 149 void operator=(const char*t); ///< assign from const char* 150 //void operator=(int x) {free(txt);nowy(x);} ///< clear string and make new empty one 151 void operator=(const SString &s); 152 152 153 void append(const char *txt,int count);154 SString operator+(const SString &s) const;155 void operator+=(int x); ///< append x spaces after current string156 void operator+=(const char*); ///< append char* contents157 void operator+=(const SString&); ///< append other SString153 void append(const char *txt, int count); 154 SString operator+(const SString &s) const; 155 void operator+=(int x); ///< append x spaces after current string 156 void operator+=(const char*); ///< append char* contents 157 void operator+=(const SString&); ///< append other SString 158 158 159 bool equals(const SString &s) const; ///< TRUE if equal160 bool operator==(const SString &s) const {return equals(s);} ///< TRUE if equal161 bool operator!=(const SString &s) const {return !equals(s);}162 const char* operator()(int p) const {return buf->txt+p;} ///< pointer to p'th character in SString163 char operator[](int i) const {return buf->txt[i];} ///< get char like in array159 bool equals(const SString &s) const; ///< TRUE if equal 160 bool operator==(const SString &s) const { return equals(s); } ///< TRUE if equal 161 bool operator!=(const SString &s) const { return !equals(s); } 162 const char* operator()(int p) const { return buf->txt + p; } ///< pointer to p'th character in SString 163 char operator[](int i) const { return buf->txt[i]; } ///< get char like in array 164 164 165 /// return a substring of the current string166 SString substr(int begin, int length=1<<30) const;165 /// return a substring of the current string 166 SString substr(int begin, int length = 1 << 30) const; 167 167 168 /// simple tokenization:169 /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character170 /// and put it in output parameter <B>token</B>.171 /// <B>pos</B> is moved accordingly.172 /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise.173 bool getNextToken(int& pos,SString &token,char separator) const;168 /// simple tokenization: 169 /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character 170 /// and put it in output parameter <B>token</B>. 171 /// <B>pos</B> is moved accordingly. 172 /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise. 173 bool getNextToken(int& pos, SString &token, char separator) const; 174 174 175 void operator+=(char ch) {directAppend(1)[0]=ch;endAppend(1);} ///< append single character175 void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character 176 176 177 bool startsWith(const char *pattern) const;178 char charAt(int pos) const {return buf->txt[pos];}179 uint32_t hash() const {return buf->hash();}177 bool startsWith(const char *pattern) const; 178 char charAt(int pos) const { return buf->txt[pos]; } 179 uint32_t hash() const { return buf->hash(); } 180 180 181 static SString valueOf(int);182 static SString valueOf(long);183 static SString valueOf(double);184 static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu185 static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu186 static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu187 static SString sprintf(const char* format, ...);181 static SString valueOf(int); 182 static SString valueOf(long); 183 static SString valueOf(double); 184 static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu 185 static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu 186 static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu 187 static SString sprintf(const char* format, ...); 188 188 189 static SString &empty();189 static SString &empty(); 190 190 }; 191 191
Note: See TracChangeset
for help on using the changeset viewer.