[385] | 1 | #include "sstring.h" |
---|
| 2 | #include <common/nonstd_stl.h> |
---|
| 3 | #include "extvalue.h" |
---|
| 4 | #include <assert.h> |
---|
| 5 | |
---|
| 6 | void SString::initEmpty() |
---|
| 7 | { |
---|
[793] | 8 | txt = NULL; used = 0; size = 0; |
---|
[385] | 9 | } |
---|
| 10 | |
---|
| 11 | SString::SString() |
---|
| 12 | { |
---|
[793] | 13 | initEmpty(); |
---|
[385] | 14 | } |
---|
| 15 | |
---|
| 16 | SString::~SString() |
---|
| 17 | { |
---|
[793] | 18 | resize(0); |
---|
[385] | 19 | } |
---|
| 20 | |
---|
| 21 | SString::SString(int x) |
---|
| 22 | { |
---|
[793] | 23 | initEmpty(); |
---|
| 24 | if (x) |
---|
| 25 | ensureSize(x + 1); |
---|
[385] | 26 | } |
---|
| 27 | |
---|
[793] | 28 | SString::SString(const char *t, int t_len) |
---|
[385] | 29 | { |
---|
[793] | 30 | initEmpty(); |
---|
| 31 | if (!t) return; |
---|
| 32 | copyFrom(t, t_len); |
---|
[385] | 33 | } |
---|
| 34 | |
---|
| 35 | SString::SString(const SString &from) |
---|
| 36 | { |
---|
[793] | 37 | initEmpty(); |
---|
| 38 | operator=(from); |
---|
[385] | 39 | } |
---|
| 40 | |
---|
| 41 | SString::SString(SString&& from) |
---|
| 42 | { |
---|
[793] | 43 | txt = from.txt; size = from.size; used = from.used; |
---|
| 44 | from.txt = NULL; from.size = 0; from.used = 0; |
---|
[385] | 45 | } |
---|
| 46 | |
---|
| 47 | void SString::resize(int newsize) |
---|
| 48 | { |
---|
[793] | 49 | if (newsize == size) return; |
---|
| 50 | txt = (char*)realloc(txt, newsize); |
---|
| 51 | size = newsize; |
---|
[385] | 52 | } |
---|
| 53 | |
---|
| 54 | void SString::ensureSize(int needed) |
---|
| 55 | { |
---|
[793] | 56 | if (size > needed) return; |
---|
| 57 | resize((size > 0) ? (needed + needed / 2 + 1) : (needed + 1)); |
---|
[385] | 58 | } |
---|
| 59 | |
---|
| 60 | char *SString::directWrite(int ensuresize) |
---|
| 61 | { |
---|
[793] | 62 | ensureSize(ensuresize); |
---|
| 63 | appending = used; |
---|
| 64 | return txt; |
---|
[385] | 65 | } |
---|
| 66 | |
---|
| 67 | char *SString::directAppend(int maxappend) |
---|
| 68 | { |
---|
[793] | 69 | ensureSize(used + maxappend); |
---|
| 70 | appending = used; |
---|
| 71 | return txt + appending; |
---|
[385] | 72 | } |
---|
| 73 | |
---|
| 74 | void SString::endWrite(int newlength) |
---|
| 75 | { |
---|
[793] | 76 | if (newlength < 0) newlength = strlen(txt); |
---|
| 77 | else txt[newlength] = 0; |
---|
| 78 | used = newlength; |
---|
| 79 | assert(used < size); |
---|
[385] | 80 | } |
---|
| 81 | |
---|
| 82 | void SString::endAppend(int newappend) |
---|
| 83 | { |
---|
[793] | 84 | if (newappend < 0) newappend = strlen(txt + appending); |
---|
| 85 | else txt[appending + newappend] = 0; |
---|
| 86 | used = appending + newappend; |
---|
| 87 | assert(used < size); |
---|
[385] | 88 | } |
---|
| 89 | |
---|
| 90 | ////////////// append ///////////////// |
---|
| 91 | |
---|
| 92 | void SString::operator+=(const char *s) |
---|
| 93 | { |
---|
[793] | 94 | if (!s) return; |
---|
| 95 | int x = strlen(s); |
---|
| 96 | if (!x) return; |
---|
| 97 | append(s, x); |
---|
[385] | 98 | } |
---|
| 99 | |
---|
[793] | 100 | void SString::append(const char *t, int n) |
---|
[385] | 101 | { |
---|
[793] | 102 | if (!n) return; |
---|
| 103 | ensureSize(used + n); |
---|
| 104 | memmove(txt + used, t, n); |
---|
| 105 | used += n; |
---|
| 106 | txt[used] = 0; |
---|
[385] | 107 | } |
---|
| 108 | |
---|
| 109 | void SString::operator+=(const SString&s) |
---|
| 110 | { |
---|
[793] | 111 | append(s.c_str(), s.len()); |
---|
[385] | 112 | } |
---|
| 113 | |
---|
| 114 | SString SString::operator+(const SString& s) const |
---|
| 115 | { |
---|
[793] | 116 | SString ret(len() + s.len()); |
---|
| 117 | ret = *this; |
---|
| 118 | ret += s; |
---|
| 119 | return ret; |
---|
[385] | 120 | } |
---|
| 121 | |
---|
| 122 | ///////////////////////////// |
---|
| 123 | |
---|
[793] | 124 | void SString::copyFrom(const char *ch, int chlen) |
---|
[385] | 125 | { |
---|
[793] | 126 | if (!ch) chlen = 0; |
---|
| 127 | else if (chlen < 0) chlen = strlen(ch); |
---|
| 128 | if (chlen) |
---|
[385] | 129 | { |
---|
[793] | 130 | ensureSize(chlen); |
---|
| 131 | memmove(txt, ch, chlen); |
---|
| 132 | txt[chlen] = 0; |
---|
| 133 | used = chlen; |
---|
[385] | 134 | } |
---|
[793] | 135 | else |
---|
[385] | 136 | { |
---|
[793] | 137 | if (txt) |
---|
[385] | 138 | { |
---|
[793] | 139 | txt[0] = 0; |
---|
| 140 | used = 0; |
---|
[385] | 141 | } |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void SString::operator=(const char *ch) |
---|
| 146 | { |
---|
[793] | 147 | copyFrom(ch); |
---|
[385] | 148 | } |
---|
| 149 | |
---|
| 150 | void SString::operator=(const SString&s) |
---|
| 151 | { |
---|
[793] | 152 | if (&s == this) return; |
---|
| 153 | copyFrom(s.c_str(), s.len()); |
---|
[385] | 154 | } |
---|
| 155 | |
---|
| 156 | /////////////////////////////////////// |
---|
| 157 | |
---|
| 158 | SString SString::substr(int begin, int length) const |
---|
| 159 | { |
---|
[793] | 160 | if (begin < 0) { length += begin; begin = 0; } |
---|
| 161 | if (length >= (len() - begin)) length = len() - begin; |
---|
| 162 | if (length <= 0) return SString(); |
---|
| 163 | if (length == len()) return *this; |
---|
| 164 | return SString((*this)(begin), length); |
---|
[385] | 165 | } |
---|
| 166 | |
---|
| 167 | /////////////////////////////////////// |
---|
| 168 | |
---|
[395] | 169 | bool SString::equals(const SString& s) const |
---|
[385] | 170 | { |
---|
[793] | 171 | if (this == &s) return true; |
---|
| 172 | if (len() != s.len()) return false; |
---|
| 173 | return strcmp(getPtr(), s.getPtr()) == 0; |
---|
[385] | 174 | } |
---|
| 175 | |
---|
| 176 | /////////////////////////////////////// |
---|
| 177 | |
---|
[793] | 178 | int SString::indexOf(int character, int start) const |
---|
[385] | 179 | { |
---|
[793] | 180 | const char *found = strchr(getPtr() + start, character); |
---|
| 181 | return found ? found - getPtr() : -1; |
---|
[385] | 182 | } |
---|
| 183 | |
---|
[793] | 184 | int SString::indexOf(const char *substring, int start) const |
---|
[385] | 185 | { |
---|
[793] | 186 | const char *found = strstr(getPtr() + start, substring); |
---|
| 187 | return found ? found - getPtr() : -1; |
---|
[385] | 188 | } |
---|
| 189 | |
---|
[793] | 190 | int SString::indexOf(const SString & substring, int start) const |
---|
[385] | 191 | { |
---|
[793] | 192 | const char *found = strstr(getPtr() + start, substring.c_str()); |
---|
| 193 | return found ? found - getPtr() : -1; |
---|
[385] | 194 | } |
---|
| 195 | |
---|
[793] | 196 | bool SString::getNextToken(int& pos, SString &token, char separator) const |
---|
[385] | 197 | { |
---|
[793] | 198 | if (pos >= len()) { token = 0; return false; } |
---|
| 199 | int p1 = pos, p2; |
---|
| 200 | const char *t1 = getPtr() + pos; |
---|
| 201 | const char *t2 = strchr(t1, separator); |
---|
| 202 | if (t2) pos = (p2 = (t2 - getPtr())) + 1; else p2 = pos = len(); |
---|
| 203 | strncpy(token.directWrite(p2 - p1), t1, p2 - p1); |
---|
| 204 | token.endWrite(p2 - p1); |
---|
| 205 | return true; |
---|
[385] | 206 | } |
---|
| 207 | |
---|
[395] | 208 | bool SString::startsWith(const char *pattern) const |
---|
[385] | 209 | { |
---|
[793] | 210 | const char *t = this->c_str(); |
---|
| 211 | for (; *pattern; pattern++, t++) |
---|
| 212 | if (*t != *pattern) return false; |
---|
| 213 | return true; |
---|
[385] | 214 | } |
---|
| 215 | |
---|
| 216 | SString SString::valueOf(int i) |
---|
| 217 | { |
---|
[793] | 218 | return SString::sprintf("%d", i); |
---|
[385] | 219 | } |
---|
| 220 | SString SString::valueOf(long i) |
---|
| 221 | { |
---|
[793] | 222 | return SString::sprintf("%d", i); |
---|
[385] | 223 | } |
---|
| 224 | SString SString::valueOf(double d) |
---|
| 225 | { |
---|
[793] | 226 | SString tmp = SString::sprintf("%.15g", d); |
---|
| 227 | if ((!strchr(tmp.c_str(), '.')) && (!strchr(tmp.c_str(), 'e'))) tmp += ".0"; |
---|
| 228 | return tmp; |
---|
[385] | 229 | } |
---|
| 230 | SString SString::valueOf(const SString& s) |
---|
| 231 | { |
---|
[793] | 232 | return s; |
---|
[385] | 233 | } |
---|
| 234 | |
---|
| 235 | SString SString::sprintf(const char* format, ...) |
---|
| 236 | { |
---|
[793] | 237 | int n, size = 30; |
---|
| 238 | va_list ap; |
---|
[385] | 239 | |
---|
[793] | 240 | SString ret; |
---|
[385] | 241 | |
---|
| 242 | #ifdef USE_VSCPRINTF |
---|
[793] | 243 | va_start(ap, format); |
---|
| 244 | size = _vscprintf(format, ap); |
---|
| 245 | va_end(ap); |
---|
[385] | 246 | #endif |
---|
| 247 | |
---|
[793] | 248 | while (1) |
---|
[385] | 249 | { |
---|
[793] | 250 | char* p = ret.directWrite(size); |
---|
| 251 | assert(p != NULL); |
---|
| 252 | size = ret.directMaxLen() + 1; |
---|
| 253 | /* Try to print in the allocated space. */ |
---|
| 254 | va_start(ap, format); |
---|
| 255 | n = vsnprintf(p, size, format, ap); |
---|
| 256 | va_end(ap); |
---|
| 257 | /* If that worked, return the string. */ |
---|
| 258 | if (n > -1 && n < size) |
---|
[385] | 259 | { |
---|
[793] | 260 | ret.endWrite(n); |
---|
| 261 | return ret; |
---|
[385] | 262 | } |
---|
[793] | 263 | /* Else try again with more space. */ |
---|
[385] | 264 | #ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE |
---|
[793] | 265 | if (n > -1) /* glibc 2.1 */ |
---|
| 266 | size = n; /* precisely what is needed */ |
---|
| 267 | else /* glibc 2.0 */ |
---|
[385] | 268 | #endif |
---|
[793] | 269 | size *= 2; /* twice the old size */ |
---|
[385] | 270 | } |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | SString &SString::empty() |
---|
| 274 | { |
---|
[793] | 275 | static SString empty; |
---|
| 276 | return empty; |
---|
[385] | 277 | } |
---|