[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include "sstring.h" |
---|
[222] | 6 | #include <common/nonstd.h> //to be sure the vsnprintf-related stuff gets included |
---|
[109] | 7 | |
---|
| 8 | #ifdef SSTRING_SIMPLE |
---|
| 9 | |
---|
| 10 | // simple sstring implementation using direct character arrays |
---|
| 11 | // - duplicate = copy all characters |
---|
| 12 | // - no mutex needed |
---|
| 13 | |
---|
| 14 | #include "sstring-simple.cpp" |
---|
| 15 | |
---|
| 16 | #else |
---|
| 17 | /////////////////////////////////////////////////////////////////////////// |
---|
| 18 | // old sstring implementation using SBuf references |
---|
| 19 | // - duplicate = copy buffer pointer |
---|
| 20 | // - mutex required to be thread safe |
---|
| 21 | |
---|
| 22 | #include <common/nonstd_stl.h> |
---|
| 23 | #include "extvalue.h" |
---|
| 24 | #include <assert.h> |
---|
[826] | 25 | #ifdef USE_PRINTFLOAT_DRAGON4 |
---|
| 26 | #include <PrintFloat/PrintFloat.h> |
---|
| 27 | #endif |
---|
[109] | 28 | |
---|
| 29 | #ifdef MULTITHREADED |
---|
| 30 | #include <pthread.h> |
---|
[793] | 31 | static pthread_mutex_t sstring_ref_lock = PTHREAD_MUTEX_INITIALIZER; |
---|
[109] | 32 | #define REF_LOCK pthread_mutex_lock(&sstring_ref_lock); |
---|
| 33 | #define REF_UNLOCK pthread_mutex_unlock(&sstring_ref_lock) |
---|
| 34 | #else |
---|
| 35 | #define REF_LOCK |
---|
| 36 | #define REF_UNLOCK |
---|
| 37 | #endif |
---|
| 38 | |
---|
| 39 | static int guessMemSize(int request) |
---|
| 40 | { |
---|
[793] | 41 | return request + min(request / 2, 10000) + 8; |
---|
[109] | 42 | } |
---|
| 43 | |
---|
| 44 | SBuf::SBuf() |
---|
| 45 | { |
---|
[793] | 46 | txt = (char*)""; |
---|
| 47 | size = used = 0; |
---|
| 48 | refcount = 1; |
---|
[109] | 49 | } |
---|
| 50 | |
---|
| 51 | SBuf::SBuf(int initsize) |
---|
| 52 | { |
---|
[793] | 53 | size = guessMemSize(initsize); |
---|
| 54 | if (size > 0) { txt = (char*)malloc(size + 1); txt[0] = 0; } |
---|
| 55 | else txt = (char*)""; |
---|
| 56 | used = 0; |
---|
| 57 | refcount = 1; |
---|
[109] | 58 | } |
---|
| 59 | |
---|
| 60 | SBuf::~SBuf() |
---|
| 61 | { |
---|
[793] | 62 | freeBuf(); |
---|
[109] | 63 | } |
---|
| 64 | |
---|
| 65 | void SBuf::initEmpty() |
---|
| 66 | { |
---|
[793] | 67 | txt = (char*)""; |
---|
| 68 | used = size = 0; |
---|
| 69 | refcount = 1; |
---|
[109] | 70 | } |
---|
| 71 | |
---|
| 72 | void SBuf::freeBuf() |
---|
| 73 | { |
---|
[793] | 74 | if (!size) return; |
---|
| 75 | free(txt); used = 0; |
---|
[109] | 76 | } |
---|
| 77 | |
---|
[793] | 78 | void SBuf::copyFrom(const char *ch, int chlen) |
---|
[109] | 79 | { |
---|
[793] | 80 | if (chlen == -1) chlen = strlen(ch); |
---|
| 81 | if (chlen > 0) |
---|
[109] | 82 | { |
---|
[793] | 83 | if (chlen < size) |
---|
| 84 | { |
---|
| 85 | memmove(txt, ch, chlen); |
---|
| 86 | } |
---|
| 87 | else |
---|
| 88 | { |
---|
| 89 | size = guessMemSize(chlen); |
---|
| 90 | char *newtxt = (char*)malloc(size + 1); |
---|
| 91 | memcpy(newtxt, ch, chlen); |
---|
| 92 | free(txt); |
---|
| 93 | txt = newtxt; |
---|
| 94 | } |
---|
[109] | 95 | } |
---|
[793] | 96 | txt[chlen] = 0; |
---|
| 97 | used = chlen; |
---|
[109] | 98 | } |
---|
| 99 | |
---|
[793] | 100 | void SBuf::append(const char *ch, int chlen) |
---|
[109] | 101 | { // doesn't check anything! |
---|
[793] | 102 | memmove(txt + used, ch, chlen); |
---|
| 103 | used += chlen; |
---|
| 104 | txt[used] = 0; |
---|
[109] | 105 | } |
---|
| 106 | |
---|
| 107 | void SBuf::ensureSize(int needed) |
---|
| 108 | { |
---|
[793] | 109 | if (size >= needed) return; |
---|
| 110 | needed = guessMemSize(needed); |
---|
| 111 | txt = (char*)realloc(txt, needed + 1); |
---|
| 112 | size = needed; |
---|
[109] | 113 | } |
---|
| 114 | |
---|
| 115 | ///////////////////////////////////////////// |
---|
| 116 | |
---|
| 117 | SString::SString() |
---|
| 118 | { |
---|
[793] | 119 | initEmpty(); |
---|
[109] | 120 | } |
---|
| 121 | |
---|
| 122 | SString::~SString() |
---|
| 123 | { |
---|
[793] | 124 | REF_LOCK; |
---|
| 125 | detach(); |
---|
| 126 | REF_UNLOCK; |
---|
[109] | 127 | } |
---|
| 128 | |
---|
| 129 | SString::SString(int x) |
---|
| 130 | { |
---|
[793] | 131 | buf = new SBuf(x); |
---|
[109] | 132 | } |
---|
| 133 | |
---|
[793] | 134 | SString::SString(const char *t, int t_len) |
---|
[109] | 135 | { |
---|
[793] | 136 | initEmpty(); |
---|
| 137 | if (!t) return; |
---|
| 138 | copyFrom(t, t_len); |
---|
[109] | 139 | } |
---|
| 140 | |
---|
[367] | 141 | SString::SString(SString&& from) |
---|
| 142 | { |
---|
[793] | 143 | buf = from.buf; |
---|
| 144 | from.buf = &SBuf::empty(); |
---|
[367] | 145 | } |
---|
| 146 | |
---|
[109] | 147 | SString::SString(const SString &from) |
---|
| 148 | { |
---|
[793] | 149 | if (from.buf == &SBuf::empty()) |
---|
| 150 | buf = &SBuf::empty(); |
---|
| 151 | else |
---|
[109] | 152 | { |
---|
[793] | 153 | REF_LOCK; |
---|
| 154 | buf = from.buf; |
---|
| 155 | if (buf->size) |
---|
| 156 | buf->refcount++; |
---|
| 157 | REF_UNLOCK; |
---|
[109] | 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | void SString::initEmpty() |
---|
| 162 | { |
---|
[793] | 163 | buf = &SBuf::empty(); |
---|
[109] | 164 | } |
---|
| 165 | |
---|
| 166 | void SString::memoryHint(int howbig) |
---|
| 167 | { |
---|
[793] | 168 | detachCopy(howbig); |
---|
[109] | 169 | } |
---|
[793] | 170 | |
---|
[109] | 171 | void SString::detachEmpty(int ensuresize) |
---|
| 172 | { |
---|
[793] | 173 | if (buf == &SBuf::empty()) { buf = new SBuf(ensuresize); return; } |
---|
| 174 | if (buf->refcount < 2) buf->ensureSize(ensuresize); |
---|
| 175 | else |
---|
[109] | 176 | { |
---|
[793] | 177 | buf->refcount--; |
---|
| 178 | buf = new SBuf(ensuresize); |
---|
[109] | 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | void SString::detach() |
---|
| 183 | { |
---|
[793] | 184 | if (buf == &SBuf::empty()) return; |
---|
| 185 | if (!--buf->refcount) delete buf; |
---|
[109] | 186 | } |
---|
| 187 | |
---|
| 188 | void SString::detachCopy(int ensuresize) |
---|
| 189 | { |
---|
[793] | 190 | if (buf == &SBuf::empty()) { buf = new SBuf(ensuresize); return; } |
---|
| 191 | if (buf->refcount < 2) |
---|
[109] | 192 | { |
---|
[793] | 193 | buf->ensureSize(ensuresize); |
---|
| 194 | return; |
---|
[109] | 195 | } |
---|
[793] | 196 | buf->refcount--; |
---|
| 197 | SBuf *newbuf = new SBuf(ensuresize); |
---|
| 198 | newbuf->copyFrom(buf->txt, min(ensuresize, buf->used)); |
---|
| 199 | buf = newbuf; |
---|
[109] | 200 | } |
---|
| 201 | |
---|
| 202 | char *SString::directWrite(int ensuresize) |
---|
| 203 | { |
---|
[793] | 204 | if (ensuresize < 0) ensuresize = len(); |
---|
| 205 | REF_LOCK; |
---|
| 206 | detachCopy(ensuresize); |
---|
| 207 | REF_UNLOCK; |
---|
| 208 | appending = buf->used; |
---|
| 209 | return buf->txt; |
---|
[109] | 210 | } |
---|
| 211 | |
---|
| 212 | /* |
---|
| 213 | char *SString::directWrite() |
---|
| 214 | { |
---|
| 215 | return directWrite(buf->used); |
---|
| 216 | } |
---|
| 217 | */ |
---|
| 218 | char *SString::directAppend(int maxappend) |
---|
| 219 | { |
---|
[793] | 220 | REF_LOCK; |
---|
| 221 | detachCopy(buf->used + maxappend); |
---|
| 222 | REF_UNLOCK; |
---|
| 223 | appending = buf->used; |
---|
| 224 | return buf->txt + appending; |
---|
[109] | 225 | } |
---|
| 226 | |
---|
| 227 | void SString::endWrite(int newlength) |
---|
| 228 | { |
---|
[793] | 229 | if (newlength < 0) newlength = strlen(buf->txt); |
---|
| 230 | else buf->txt[newlength] = 0; |
---|
| 231 | buf->used = newlength; |
---|
[109] | 232 | } |
---|
| 233 | |
---|
| 234 | void SString::endAppend(int newappend) |
---|
| 235 | { |
---|
[793] | 236 | if (newappend < 0) newappend = strlen(buf->txt + appending); |
---|
| 237 | else buf->txt[appending + newappend] = 0; |
---|
| 238 | buf->used = appending + newappend; |
---|
[109] | 239 | } |
---|
| 240 | |
---|
| 241 | ////////////// append ///////////////// |
---|
| 242 | |
---|
| 243 | void SString::operator+=(const char *s) |
---|
| 244 | { |
---|
[793] | 245 | if (!s) return; |
---|
| 246 | int x = strlen(s); |
---|
| 247 | if (!x) return; |
---|
| 248 | append(s, x); |
---|
[109] | 249 | } |
---|
| 250 | |
---|
[793] | 251 | void SString::append(const char *txt, int count) |
---|
[109] | 252 | { |
---|
[793] | 253 | if (!count) return; |
---|
| 254 | REF_LOCK; |
---|
| 255 | detachCopy(buf->used + count); |
---|
| 256 | REF_UNLOCK; |
---|
| 257 | buf->append(txt, count); |
---|
[109] | 258 | } |
---|
| 259 | |
---|
| 260 | void SString::operator+=(const SString&s) |
---|
| 261 | { |
---|
[793] | 262 | append(s.c_str(), s.len()); |
---|
[109] | 263 | } |
---|
| 264 | |
---|
| 265 | SString SString::operator+(const SString& s) const |
---|
| 266 | { |
---|
[793] | 267 | SString ret(*this); |
---|
| 268 | ret += s; |
---|
| 269 | return ret; |
---|
[109] | 270 | } |
---|
| 271 | |
---|
| 272 | ///////////////////////////// |
---|
| 273 | |
---|
[793] | 274 | void SString::copyFrom(const char *ch, int chlen) |
---|
[109] | 275 | { |
---|
[793] | 276 | if (!ch) chlen = 0; |
---|
| 277 | else if (chlen < 0) chlen = strlen(ch); |
---|
| 278 | REF_LOCK; |
---|
| 279 | detachEmpty(chlen); |
---|
| 280 | REF_UNLOCK; |
---|
| 281 | memmove(buf->txt, ch, chlen); |
---|
| 282 | buf->txt[chlen] = 0; buf->used = chlen; |
---|
[109] | 283 | } |
---|
| 284 | |
---|
| 285 | void SString::operator=(const char *ch) |
---|
| 286 | { |
---|
[793] | 287 | copyFrom(ch); |
---|
[109] | 288 | } |
---|
| 289 | |
---|
| 290 | void SString::operator=(const SString&s) |
---|
| 291 | { |
---|
[793] | 292 | if (s.buf == buf) return; |
---|
| 293 | REF_LOCK; |
---|
| 294 | detach(); |
---|
| 295 | buf = s.buf; |
---|
| 296 | if (buf->size) buf->refcount++; |
---|
| 297 | REF_UNLOCK; |
---|
[109] | 298 | } |
---|
| 299 | /////////////////////////////////////// |
---|
| 300 | |
---|
| 301 | SString SString::substr(int begin, int length) const |
---|
| 302 | { |
---|
[793] | 303 | if (begin < 0) { length += begin; begin = 0; } |
---|
| 304 | if (length >= (len() - begin)) length = len() - begin; |
---|
| 305 | if (length <= 0) return SString(); |
---|
| 306 | if (length == len()) return *this; |
---|
| 307 | return SString((*this)(begin), length); |
---|
[109] | 308 | } |
---|
| 309 | |
---|
| 310 | /////////////////////////////////////// |
---|
| 311 | |
---|
[247] | 312 | bool SString::equals(const SString& s) const |
---|
[109] | 313 | { |
---|
[793] | 314 | if (s.buf == buf) return true; |
---|
| 315 | return strcmp(buf->txt, s.buf->txt) == 0; |
---|
[109] | 316 | } |
---|
| 317 | |
---|
| 318 | /////////////////////////////////////// |
---|
| 319 | |
---|
[793] | 320 | int SString::indexOf(int character, int start) const |
---|
[109] | 321 | { |
---|
[793] | 322 | const char *found = strchr(buf->txt + start, character); |
---|
| 323 | return found ? found - buf->txt : -1; |
---|
[109] | 324 | } |
---|
| 325 | |
---|
[793] | 326 | int SString::indexOf(const char *substring, int start) const |
---|
[109] | 327 | { |
---|
[793] | 328 | char *found = strstr(buf->txt + start, substring); |
---|
| 329 | return found ? found - buf->txt : -1; |
---|
[109] | 330 | } |
---|
| 331 | |
---|
[793] | 332 | int SString::indexOf(const SString & substring, int start) const |
---|
[109] | 333 | { |
---|
[793] | 334 | char *found = strstr(buf->txt + start, substring.c_str()); |
---|
| 335 | return found ? found - buf->txt : -1; |
---|
[109] | 336 | } |
---|
| 337 | |
---|
[793] | 338 | bool SString::getNextToken(int& pos, SString &token, char separator) const |
---|
[109] | 339 | { |
---|
[793] | 340 | if (pos >= len()) { token = 0; return false; } |
---|
| 341 | int p1 = pos, p2; |
---|
| 342 | const char *t1 = buf->txt + pos; |
---|
| 343 | const char *t2 = strchr(t1, separator); |
---|
| 344 | if (t2) pos = (p2 = (t2 - buf->txt)) + 1; else p2 = pos = len(); |
---|
| 345 | strncpy(token.directWrite(p2 - p1), t1, p2 - p1); |
---|
| 346 | token.endWrite(p2 - p1); |
---|
| 347 | return true; |
---|
[109] | 348 | } |
---|
| 349 | |
---|
[395] | 350 | bool SString::startsWith(const char *pattern) const |
---|
[109] | 351 | { |
---|
[793] | 352 | const char *t = this->c_str(); |
---|
| 353 | for (; *pattern; pattern++, t++) |
---|
| 354 | if (*t != *pattern) return false; |
---|
| 355 | return true; |
---|
[109] | 356 | } |
---|
| 357 | |
---|
| 358 | SString SString::valueOf(int i) |
---|
| 359 | { |
---|
[793] | 360 | return SString::sprintf("%d", i); |
---|
[109] | 361 | } |
---|
| 362 | SString SString::valueOf(long i) |
---|
| 363 | { |
---|
[793] | 364 | return SString::sprintf("%d", i); |
---|
[109] | 365 | } |
---|
| 366 | SString SString::valueOf(double d) |
---|
| 367 | { |
---|
[826] | 368 | #ifdef USE_PRINTFLOAT_DRAGON4 |
---|
| 369 | SString tmp; |
---|
| 370 | char* here=tmp.directWrite(30); |
---|
| 371 | tmp.endWrite(PrintFloat64(here,30,d, |
---|
| 372 | ((d<-1e17)||(d>1e17)||((d<1e-4)&&(d>-1e-4)&&(d!=0.0))) |
---|
| 373 | ? PrintFloatFormat_Scientific : PrintFloatFormat_Positional, |
---|
| 374 | -1));//http://www.ryanjuckett.com/programming/printing-floating-point-numbers/ |
---|
| 375 | #else |
---|
| 376 | SString tmp = SString::sprintf("%.17g", d); //https://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value |
---|
| 377 | #endif |
---|
[793] | 378 | if ((!strchr(tmp.c_str(), '.')) && (!strchr(tmp.c_str(), 'e'))) tmp += ".0"; |
---|
| 379 | return tmp; |
---|
[109] | 380 | } |
---|
| 381 | SString SString::valueOf(const SString& s) |
---|
| 382 | { |
---|
[793] | 383 | return s; |
---|
[109] | 384 | } |
---|
| 385 | |
---|
| 386 | #if 0 //testing _vscprintf |
---|
| 387 | #define USE_VSCPRINTF |
---|
[793] | 388 | int _vscprintf(const char *format, va_list argptr) |
---|
[109] | 389 | { |
---|
[793] | 390 | return vsnprintf("", 0, format, argptr); |
---|
[109] | 391 | } |
---|
| 392 | #endif |
---|
| 393 | |
---|
| 394 | SString SString::sprintf(const char* format, ...) |
---|
| 395 | { |
---|
[793] | 396 | int n, size = 30; |
---|
| 397 | va_list ap; |
---|
[109] | 398 | |
---|
[793] | 399 | SString ret; |
---|
[109] | 400 | |
---|
| 401 | #ifdef USE_VSCPRINTF |
---|
[793] | 402 | va_start(ap, format); |
---|
| 403 | size = _vscprintf(format, ap); |
---|
| 404 | va_end(ap); |
---|
[109] | 405 | #endif |
---|
| 406 | |
---|
[793] | 407 | while (1) |
---|
[109] | 408 | { |
---|
[793] | 409 | char* p = ret.directWrite(size); |
---|
| 410 | assert(p != NULL); |
---|
| 411 | size = ret.directMaxLen() + 1; |
---|
| 412 | /* Try to print in the allocated space. */ |
---|
| 413 | va_start(ap, format); |
---|
| 414 | n = vsnprintf(p, size, format, ap); |
---|
| 415 | va_end(ap); |
---|
| 416 | /* If that worked, return the string. */ |
---|
| 417 | if (n > -1 && n < size) |
---|
[109] | 418 | { |
---|
[793] | 419 | ret.endWrite(n); |
---|
| 420 | return ret; |
---|
[109] | 421 | } |
---|
[793] | 422 | /* Else try again with more space. */ |
---|
[109] | 423 | #ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE |
---|
[793] | 424 | if (n > -1) /* glibc 2.1 */ |
---|
| 425 | size = n; /* precisely what is needed */ |
---|
| 426 | else /* glibc 2.0 */ |
---|
[109] | 427 | #endif |
---|
[793] | 428 | size *= 2; /* twice the old size */ |
---|
[109] | 429 | } |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | SString &SString::empty() |
---|
| 433 | { |
---|
[793] | 434 | static SString empty; |
---|
| 435 | return empty; |
---|
[109] | 436 | } |
---|
| 437 | |
---|
| 438 | SBuf &SBuf::empty() |
---|
| 439 | { |
---|
[793] | 440 | static SBuf empty; |
---|
| 441 | return empty; |
---|
[109] | 442 | } |
---|
| 443 | |
---|
| 444 | #endif //#ifdef SSTRING_SIMPLE |
---|
[347] | 445 | |
---|
| 446 | ////////////////////////////////////////////////// |
---|
| 447 | // to be moved somewhere else? |
---|
| 448 | // public domain source: http://isthe.com/chongo/src/fnv |
---|
| 449 | typedef uint32_t Fnv32_t; |
---|
| 450 | |
---|
| 451 | #define FNV_32_PRIME ((Fnv32_t)0x01000193) |
---|
| 452 | #define FNV1_32_INIT ((Fnv32_t)0x811c9dc5) |
---|
| 453 | #define FNV1_32A_INIT FNV1_32_INIT |
---|
| 454 | |
---|
| 455 | Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hval) |
---|
| 456 | { |
---|
[793] | 457 | unsigned char *bp = (unsigned char *)buf; /* start of buffer */ |
---|
| 458 | unsigned char *be = bp + len; /* beyond end of buffer */ |
---|
[347] | 459 | |
---|
[793] | 460 | while (bp < be) { |
---|
[347] | 461 | |
---|
[793] | 462 | /* xor the bottom with the current octet */ |
---|
| 463 | hval ^= (Fnv32_t)*bp++; |
---|
[347] | 464 | |
---|
[793] | 465 | /* multiply by the 32 bit FNV magic prime mod 2^32 */ |
---|
[347] | 466 | #if defined(NO_FNV_GCC_OPTIMIZATION) |
---|
[793] | 467 | hval *= FNV_32_PRIME; |
---|
[347] | 468 | #else |
---|
[793] | 469 | hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); |
---|
[347] | 470 | #endif |
---|
| 471 | |
---|
[793] | 472 | } |
---|
[347] | 473 | |
---|
[793] | 474 | /* return our new hash value */ |
---|
| 475 | return hval; |
---|
[347] | 476 | } |
---|
| 477 | ////////////////////////////////////////////////// |
---|
| 478 | |
---|
| 479 | #ifdef SSTRING_SIMPLE |
---|
| 480 | uint32_t SString::hash() const |
---|
| 481 | { |
---|
[793] | 482 | return fnv_32a_buf(txt, used, FNV1_32A_INIT); |
---|
[347] | 483 | } |
---|
| 484 | #else |
---|
| 485 | uint32_t SBuf::hash() const |
---|
| 486 | { |
---|
[793] | 487 | return fnv_32a_buf(txt, used, FNV1_32A_INIT); |
---|
[347] | 488 | } |
---|
| 489 | #endif |
---|