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