[64] | 1 | // This file is a part of the Framsticks GDK library. |
---|
| 2 | // Copyright (C) 2002-2011 Szymon Ulatowski. See LICENSE.txt for details. |
---|
| 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
[5] | 4 | |
---|
| 5 | |
---|
| 6 | #include "sstring.h" |
---|
[81] | 7 | #include "nonstd_stl.h" |
---|
[92] | 8 | #include "extvalue.h" |
---|
[5] | 9 | |
---|
[104] | 10 | #ifdef MULTITHREADED |
---|
| 11 | #include <pthread.h> |
---|
| 12 | static pthread_mutex_t sstring_ref_lock; |
---|
| 13 | #define REF_LOCK pthread_mutex_lock(&sstring_ref_lock); |
---|
| 14 | #define REF_UNLOCK pthread_mutex_unlock(&sstring_ref_lock) |
---|
| 15 | #else |
---|
| 16 | #define REF_LOCK |
---|
| 17 | #define REF_UNLOCK |
---|
| 18 | #endif |
---|
| 19 | |
---|
[5] | 20 | static int guessMemSize(int request, int memhint) |
---|
| 21 | { |
---|
| 22 | if (memhint>=0) |
---|
| 23 | return request+memhint; |
---|
| 24 | else |
---|
| 25 | return request+min(request/2,10000)+8; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | SBuf::SBuf() |
---|
| 29 | { |
---|
| 30 | txt=""; |
---|
| 31 | size=used=0; |
---|
| 32 | refcount=1; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | SBuf::SBuf(int initsize, int memhint) |
---|
| 36 | { |
---|
| 37 | size=guessMemSize(initsize,memhint); |
---|
| 38 | if (size>0) { txt=(char*)malloc(size+1); txt[0]=0; } |
---|
| 39 | else txt=""; |
---|
| 40 | used=0; |
---|
| 41 | refcount=1; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | SBuf::~SBuf() |
---|
| 45 | { |
---|
| 46 | freeBuf(); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void SBuf::initEmpty() |
---|
| 50 | { |
---|
| 51 | txt=""; |
---|
| 52 | used=size=0; |
---|
| 53 | refcount=1; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | void SBuf::freeBuf() |
---|
| 57 | { |
---|
| 58 | if (!size) return; |
---|
| 59 | free(txt); used=0; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | void SBuf::copyFrom(const char *ch,int chlen,int memhint) |
---|
| 63 | { |
---|
| 64 | if (chlen==-1) chlen=strlen(ch); |
---|
| 65 | if (chlen>0) |
---|
| 66 | { |
---|
| 67 | if (chlen<size) |
---|
| 68 | { |
---|
| 69 | memcpy(txt,ch,chlen); |
---|
| 70 | } |
---|
| 71 | else |
---|
| 72 | { |
---|
| 73 | size=guessMemSize(chlen,memhint); |
---|
| 74 | char *newtxt=(char*)malloc(size+1); |
---|
| 75 | memcpy(newtxt,ch,chlen); |
---|
| 76 | free(txt); |
---|
| 77 | txt=newtxt; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | txt[chlen]=0; |
---|
| 81 | used=chlen; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | void SBuf::append(const char *ch,int chlen) |
---|
| 85 | { // doesn't check anything! |
---|
| 86 | memcpy(txt+used,ch,chlen); |
---|
| 87 | used+=chlen; |
---|
| 88 | txt[used]=0; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void SBuf::ensureSize(int needed, int memhint) |
---|
| 92 | { |
---|
| 93 | if (size>=needed) return; |
---|
| 94 | needed=guessMemSize(needed,memhint); |
---|
| 95 | txt=(char*)realloc(txt,needed+1); |
---|
| 96 | size=needed; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | ///////////////////////////////////////////// |
---|
| 100 | |
---|
| 101 | SString::SString() |
---|
| 102 | { |
---|
| 103 | initEmpty(); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | SString::~SString() |
---|
| 107 | { |
---|
[104] | 108 | REF_LOCK; |
---|
[5] | 109 | detach(); |
---|
[104] | 110 | REF_UNLOCK; |
---|
[5] | 111 | } |
---|
| 112 | |
---|
| 113 | SString::SString(int x) |
---|
| 114 | { |
---|
| 115 | buf=new SBuf(x); |
---|
[104] | 116 | memhint=-1; |
---|
[5] | 117 | } |
---|
| 118 | |
---|
| 119 | SString::SString(const char *t,int t_len) |
---|
| 120 | { |
---|
| 121 | initEmpty(); |
---|
| 122 | if (!t) return; |
---|
| 123 | copyFrom(t,t_len); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | SString::SString(const SString &from) |
---|
| 127 | { |
---|
[104] | 128 | if (from.buf==&SBuf::empty()) |
---|
| 129 | buf=&SBuf::empty(); |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | REF_LOCK; |
---|
| 133 | buf=from.buf; |
---|
| 134 | if (buf->size) |
---|
| 135 | buf->refcount++; |
---|
| 136 | REF_UNLOCK; |
---|
| 137 | } |
---|
[5] | 138 | memhint=-1; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | void SString::initEmpty() |
---|
| 142 | { |
---|
[81] | 143 | buf=&SBuf::empty(); |
---|
[5] | 144 | memhint=-1; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | void SString::memoryHint(int howbig) |
---|
| 148 | { |
---|
| 149 | memhint=howbig; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | void SString::detachEmpty(int ensuresize) |
---|
| 153 | { |
---|
[81] | 154 | if (buf==&SBuf::empty()) { buf=new SBuf(ensuresize,memhint); return; } |
---|
[5] | 155 | if (buf->refcount<2) buf->ensureSize(ensuresize,memhint); |
---|
| 156 | else |
---|
| 157 | { |
---|
| 158 | buf->refcount--; |
---|
| 159 | buf=new SBuf(ensuresize,memhint); |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void SString::detach() |
---|
| 164 | { |
---|
[81] | 165 | if (buf==&SBuf::empty()) return; |
---|
[5] | 166 | if (!--buf->refcount) delete buf; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | void SString::detachCopy(int ensuresize) |
---|
| 170 | { |
---|
[81] | 171 | if (buf==&SBuf::empty()) { buf=new SBuf(ensuresize,memhint); return; } |
---|
[5] | 172 | if (buf->refcount<2) |
---|
| 173 | { |
---|
| 174 | buf->ensureSize(ensuresize,memhint); |
---|
| 175 | return; |
---|
| 176 | } |
---|
| 177 | buf->refcount--; |
---|
| 178 | SBuf *newbuf=new SBuf(ensuresize,memhint); |
---|
| 179 | newbuf->copyFrom(buf->txt,min(ensuresize,buf->used),memhint); |
---|
| 180 | buf=newbuf; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | char *SString::directWrite(int ensuresize) |
---|
| 184 | { |
---|
| 185 | if (ensuresize<0) ensuresize=len(); |
---|
[104] | 186 | REF_LOCK; |
---|
[5] | 187 | detachCopy(ensuresize); |
---|
[104] | 188 | REF_UNLOCK; |
---|
[5] | 189 | appending=buf->used; |
---|
| 190 | return buf->txt; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /* |
---|
| 194 | char *SString::directWrite() |
---|
| 195 | { |
---|
| 196 | return directWrite(buf->used); |
---|
| 197 | } |
---|
| 198 | */ |
---|
| 199 | char *SString::directAppend(int maxappend) |
---|
| 200 | { |
---|
[104] | 201 | REF_LOCK; |
---|
[5] | 202 | detachCopy(buf->used+maxappend); |
---|
[104] | 203 | REF_UNLOCK; |
---|
[5] | 204 | appending=buf->used; |
---|
| 205 | return buf->txt+appending; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | void SString::endWrite(int newlength) |
---|
| 209 | { |
---|
| 210 | if (newlength<0) newlength=strlen(buf->txt); |
---|
| 211 | else buf->txt[newlength]=0; |
---|
| 212 | buf->used=newlength; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | void SString::endAppend(int newappend) |
---|
| 216 | { |
---|
| 217 | if (newappend<0) newappend=strlen(buf->txt+appending); |
---|
| 218 | else buf->txt[appending+newappend]=0; |
---|
| 219 | buf->used=appending+newappend; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | ////////////// append ///////////////// |
---|
| 223 | |
---|
| 224 | void SString::operator+=(const char *s) |
---|
| 225 | { |
---|
| 226 | if (!s) return; |
---|
| 227 | int x=strlen(s); |
---|
| 228 | if (!x) return; |
---|
| 229 | append(s,x); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | void SString::append(const char *txt,int count) |
---|
| 233 | { |
---|
| 234 | if (!count) return; |
---|
[104] | 235 | REF_LOCK; |
---|
[5] | 236 | detachCopy(buf->used+count); |
---|
[104] | 237 | REF_UNLOCK; |
---|
[5] | 238 | buf->append(txt,count); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | void SString::operator+=(const SString&s) |
---|
| 242 | { |
---|
| 243 | append((const char*)s,s.len()); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | SString SString::operator+(const SString& s) const |
---|
| 247 | { |
---|
| 248 | SString ret(*this); |
---|
| 249 | ret+=s; |
---|
| 250 | return ret; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | ///////////////////////////// |
---|
| 254 | |
---|
| 255 | void SString::copyFrom(const char *ch,int chlen) |
---|
| 256 | { |
---|
| 257 | if (!ch) chlen=0; |
---|
| 258 | else if (chlen<0) chlen=strlen(ch); |
---|
[104] | 259 | REF_LOCK; |
---|
[5] | 260 | detachEmpty(chlen); |
---|
[104] | 261 | REF_UNLOCK; |
---|
[5] | 262 | memcpy(buf->txt,ch,chlen); |
---|
| 263 | buf->txt[chlen]=0; buf->used=chlen; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | void SString::operator=(const char *ch) |
---|
| 267 | { |
---|
| 268 | copyFrom(ch); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | void SString::operator=(const SString&s) |
---|
| 272 | { |
---|
| 273 | if (s.buf==buf) return; |
---|
[104] | 274 | REF_LOCK; |
---|
[5] | 275 | detach(); |
---|
| 276 | buf=s.buf; |
---|
| 277 | if (buf->size) buf->refcount++; |
---|
[104] | 278 | REF_UNLOCK; |
---|
[5] | 279 | } |
---|
| 280 | /////////////////////////////////////// |
---|
| 281 | |
---|
| 282 | SString SString::substr(int begin, int length) const |
---|
| 283 | { |
---|
| 284 | if (begin<0) { length+=begin; begin=0; } |
---|
| 285 | if (length>=(len()-begin)) length=len()-begin; |
---|
| 286 | if (length<=0) return SString(); |
---|
| 287 | if (length==len()) return *this; |
---|
| 288 | return SString((*this)(begin),length); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | /////////////////////////////////////// |
---|
| 292 | |
---|
| 293 | int SString::equals(const SString& s) const |
---|
| 294 | { |
---|
| 295 | if (s.buf==buf) return 1; |
---|
| 296 | return !strcmp(buf->txt,s.buf->txt); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /////////////////////////////////////// |
---|
| 300 | |
---|
| 301 | int SString::indexOf(int character,int start) const |
---|
| 302 | { |
---|
| 303 | const char *found=strchr(buf->txt+start,character); |
---|
| 304 | return found?found-buf->txt:-1; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | int SString::indexOf(const char *substring,int start) const |
---|
| 308 | { |
---|
| 309 | char *found=strstr(buf->txt+start,substring); |
---|
| 310 | return found?found-buf->txt:-1; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | int SString::indexOf(const SString & substring,int start) const |
---|
| 314 | { |
---|
| 315 | char *found=strstr(buf->txt+start,((const char*)substring)); |
---|
| 316 | return found?found-buf->txt:-1; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | int SString::getNextToken (int& pos,SString &token,char separator) const |
---|
| 320 | { |
---|
| 321 | if (pos>=len()) {token=0;return 0;} |
---|
| 322 | int p1=pos,p2; |
---|
| 323 | const char *t1=buf->txt+pos; |
---|
| 324 | const char *t2=strchr(t1,separator); |
---|
| 325 | if (t2) pos=(p2=(t2-buf->txt))+1; else p2=pos=len(); |
---|
| 326 | strncpy(token.directWrite(p2-p1),t1,p2-p1); |
---|
| 327 | token.endWrite(p2-p1); |
---|
| 328 | return 1; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | int SString::startsWith(const char *pattern) const |
---|
| 332 | { |
---|
| 333 | const char *t=(const char*)(*this); |
---|
| 334 | for (;*pattern;pattern++,t++) |
---|
| 335 | if (*t != *pattern) return 0; |
---|
| 336 | return 1; |
---|
| 337 | } |
---|
| 338 | |
---|
[104] | 339 | SString SString::valueOf(int i) |
---|
[5] | 340 | { |
---|
[104] | 341 | SString t; |
---|
[5] | 342 | sprintf(t.directWrite(20),"%d",i); t.endWrite(); |
---|
| 343 | return t; |
---|
| 344 | } |
---|
[104] | 345 | SString SString::valueOf(long i) |
---|
[92] | 346 | { |
---|
[104] | 347 | SString t; |
---|
[92] | 348 | sprintf(t.directWrite(20),"%d",i); t.endWrite(); |
---|
| 349 | return t; |
---|
| 350 | } |
---|
[104] | 351 | SString SString::valueOf(double d) |
---|
[5] | 352 | { |
---|
[104] | 353 | SString t; |
---|
[5] | 354 | sprintf(t.directWrite(20),"%g",d); t.endWrite(); |
---|
| 355 | return t; |
---|
| 356 | } |
---|
[104] | 357 | SString SString::valueOf(const SString& s) |
---|
[92] | 358 | { |
---|
| 359 | return s; |
---|
| 360 | } |
---|
| 361 | SString SString::valueOf(const ExtValue& v) |
---|
| 362 | { |
---|
| 363 | return v.getString(); |
---|
| 364 | } |
---|
| 365 | SString SString::valueOf(const ExtObject& v) |
---|
| 366 | { |
---|
| 367 | return v.toString(); |
---|
| 368 | } |
---|
[5] | 369 | |
---|
[81] | 370 | SString &SString::empty() |
---|
| 371 | { |
---|
| 372 | static SString empty; |
---|
| 373 | return empty; |
---|
| 374 | } |
---|
[5] | 375 | |
---|
[81] | 376 | SBuf &SBuf::empty() |
---|
| 377 | { |
---|
| 378 | static SBuf empty; |
---|
| 379 | return empty; |
---|
| 380 | } |
---|