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