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