* @brief A simple string class. * * Hold the length of the string for quick operations, * can have a buffer bigger than the string to avoid too many memory allocations and copies. * May have embedded zeroes as a result of @a substitute, but relies too heavily on C string * functions to allow reliable manipulations of these strings, other than simple appends, etc. */
| 134 | * functions to allow reliable manipulations of these strings, other than simple appends, etc. |
| 135 | */ |
| 136 | class SString : protected SContainer { |
| 137 | lenpos_t sLen; ///< The size of the string in s |
| 138 | lenpos_t sizeGrowth; ///< Minimum growth size when appending strings |
| 139 | enum { sizeGrowthDefault = 64 }; |
| 140 | |
| 141 | bool grow(lenpos_t lenNew); |
| 142 | SString &assign(const char *sOther, lenpos_t sSize_=measure_length); |
| 143 | |
| 144 | public: |
| 145 | SString() : sLen(0), sizeGrowth(sizeGrowthDefault) {} |
| 146 | SString(const SString &source) : SContainer(), sizeGrowth(sizeGrowthDefault) { |
| 147 | s = StringAllocate(source.s, source.sLen); |
| 148 | sSize = sLen = (s) ? source.sLen : 0; |
| 149 | } |
| 150 | SString(const char *s_) : sizeGrowth(sizeGrowthDefault) { |
| 151 | s = StringAllocate(s_); |
| 152 | sSize = sLen = (s) ? strlen(s) : 0; |
| 153 | } |
| 154 | SString(SBuffer &buf) : sizeGrowth(sizeGrowthDefault) { |
| 155 | s = buf.ptr(); |
| 156 | sSize = sLen = buf.size(); |
| 157 | // Consumes the given buffer! |
| 158 | buf.reset(); |
| 159 | } |
| 160 | SString(const char *s_, lenpos_t first, lenpos_t last) : sizeGrowth(sizeGrowthDefault) { |
| 161 | // note: expects the "last" argument to point one beyond the range end (a la STL iterators) |
| 162 | s = StringAllocate(s_ + first, last - first); |
| 163 | sSize = sLen = (s) ? last - first : 0; |
| 164 | } |
| 165 | SString(int i); |
| 166 | SString(double d, int precision); |
| 167 | ~SString() { |
| 168 | sLen = 0; |
| 169 | } |
| 170 | void clear() { |
| 171 | if (s) { |
| 172 | *s = '\0'; |
| 173 | } |
| 174 | sLen = 0; |
| 175 | } |
| 176 | /** Size of buffer. */ |
| 177 | lenpos_t size() const { |
| 178 | return SContainer::size(); |
| 179 | } |
| 180 | /** Size of string in buffer. */ |
| 181 | lenpos_t length() const { |
| 182 | return sLen; |
| 183 | } |
| 184 | /** Read access to a character of the string. */ |
| 185 | char operator[](lenpos_t i) const { |
| 186 | return (s && i < sSize) ? s[i] : '\0'; |
| 187 | } |
| 188 | SString &operator=(const char *source) { |
| 189 | return assign(source); |
| 190 | } |
| 191 | SString &operator=(const SString &source) { |
| 192 | if (this != &source) { |
| 193 | assign(source.s, source.sLen); |