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