Changeset 973 for cpp/frams/util
- Timestamp:
- 07/03/20 00:37:13 (4 years ago)
- Location:
- cpp/frams/util
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/util/extvalue.cpp
r940 r973 326 326 { 327 327 SString tmp = getString(); 328 if (tmp.len () > 30) tmp = tmp.substr(0, 30) + "...";328 if (tmp.length() > 30) tmp = tmp.substr(0, 30) + "..."; 329 329 if (type == TString) tmp = SString("\"") + tmp + SString("\""); 330 330 logPrintf("ExtValue", "getObjectTarget", LOG_WARN, "%s object expected, %s found", classname, tmp.c_str()); … … 614 614 return; 615 615 case TDouble: 616 setDouble(double(getInt()) *src.getDouble());616 setDouble(double(getInt()) * src.getDouble()); 617 617 return; 618 618 default:; … … 790 790 // ^-cur ^-next 791 791 // ^^^^^^^^^^___sub 792 const char* begin = fmt.c_str(), *end = begin + fmt.len (), *curr = begin;792 const char* begin = fmt.c_str(), *end = begin + fmt.length(), *curr = begin; 793 793 int type = 0; 794 794 … … 1401 1401 err->message = args[0].getString(); 1402 1402 if (err->message.startsWith(TO_STRING_PREFIX.c_str())) 1403 err->message = err->message.substr(TO_STRING_PREFIX.len ());1403 err->message = err->message.substr(TO_STRING_PREFIX.length()); 1404 1404 *ret = makeDynamicObject(err); 1405 1405 } -
cpp/frams/util/sstring-simple.cpp
r970 r973 8 8 void SString::initEmpty() 9 9 { 10 txt = NULL; used = 0; size= 0;10 txt = NULL; used = 0; allocated = 0; 11 11 } 12 12 … … 18 18 SString::~SString() 19 19 { 20 re size(0);20 reallocate(0); 21 21 } 22 22 … … 36 36 SString::SString(SString&& from) 37 37 { 38 txt = from.txt; size = from.size; used = from.used;39 from.txt = NULL; from. size= 0; from.used = 0;38 txt = from.txt; allocated = from.allocated; used = from.used; 39 from.txt = NULL; from.allocated = 0; from.used = 0; 40 40 } 41 41 … … 46 46 } 47 47 48 void SString::re size(int newsize)49 { 50 if (newsize == size) return;48 void SString::reallocate(int newsize) 49 { 50 if (newsize == allocated) return; 51 51 txt = (char*)realloc(txt, newsize); 52 size= newsize;53 } 54 55 void SString::ensure Size(int needed)56 { 57 if ( size> needed) return;58 re size((size> 0) ? (needed + needed / 2 + 1) : (needed + 1));52 allocated = newsize; 53 } 54 55 void SString::ensureAllocated(int needed) 56 { 57 if (allocated > needed) return; 58 reallocate((allocated > 0) ? (needed + needed / 2 + 1) : (needed + 1)); 59 59 } 60 60 61 61 char *SString::directWrite(int ensuresize) 62 62 { 63 ensure Size(ensuresize);63 ensureAllocated(ensuresize); 64 64 appending = used; 65 65 return txt; … … 68 68 char *SString::directAppend(int maxappend) 69 69 { 70 ensure Size(used + maxappend);70 ensureAllocated(used + maxappend); 71 71 appending = used; 72 72 return txt + appending; … … 78 78 else txt[newlength] = 0; 79 79 used = newlength; 80 assert(used < size);80 assert(used < allocated); 81 81 } 82 82 … … 86 86 else txt[appending + newappend] = 0; 87 87 used = appending + newappend; 88 assert(used < size);88 assert(used < allocated); 89 89 } 90 90 … … 102 102 { 103 103 if (!n) return; 104 ensure Size(used + n);104 ensureAllocated(used + n); 105 105 memmove(txt + used, t, n); 106 106 used += n; … … 110 110 void SString::operator+=(const SString&s) 111 111 { 112 append(s.c_str(), s.len ());112 append(s.c_str(), s.length()); 113 113 } 114 114 … … 116 116 { 117 117 SString ret; 118 ret.reserve(len () + s.len());118 ret.reserve(length() + s.length()); 119 119 ret = *this; 120 120 ret += s; … … 130 130 if (chlen) 131 131 { 132 ensure Size(chlen);132 ensureAllocated(chlen); 133 133 memmove(txt, ch, chlen); 134 134 txt[chlen] = 0; … … 153 153 { 154 154 if (&s == this) return; 155 copyFrom(s.c_str(), s.len ());155 copyFrom(s.c_str(), s.length()); 156 156 } 157 157 158 158 /////////////////////////////////////// 159 159 160 SString SString::substr(int begin, int length) const161 { 162 if (begin < 0) { length+= begin; begin = 0; }163 if ( length >= (len() - begin)) length = len() - begin;164 if ( length<= 0) return SString();165 if ( length == len()) return *this;166 return SString((*this)(begin), length);160 SString SString::substr(int begin, int numchars) const 161 { 162 if (begin < 0) { numchars += begin; begin = 0; } 163 if (numchars >= (length() - begin)) numchars = length() - begin; 164 if (numchars <= 0) return SString(); 165 if (numchars == length()) return *this; 166 return SString((*this)(begin), numchars); 167 167 } 168 168 … … 172 172 { 173 173 if (this == &s) return true; 174 if (len () != s.len()) return false;174 if (length() != s.length()) return false; 175 175 return strcmp(getPtr(), s.getPtr()) == 0; 176 176 } … … 198 198 bool SString::getNextToken(int& pos, SString &token, char separator) const 199 199 { 200 if (pos >= len ()) { token = 0; return false; }200 if (pos >= length()) { token = 0; return false; } 201 201 int p1 = pos, p2; 202 202 const char *t1 = getPtr() + pos; 203 203 const char *t2 = strchr(t1, separator); 204 if (t2) pos = (p2 = (t2 - getPtr())) + 1; else p2 = pos = len ();204 if (t2) pos = (p2 = (t2 - getPtr())) + 1; else p2 = pos = length(); 205 205 strncpy(token.directWrite(p2 - p1), t1, p2 - p1); 206 206 token.endWrite(p2 - p1); … … 254 254 char* p = ret.directWrite(size); 255 255 assert(p != NULL); 256 size = ret. directMaxLen() + 1;256 size = ret.capacity() + 1; 257 257 /* Try to print in the allocated space. */ 258 258 va_start(ap, format); -
cpp/frams/util/sstring-simple.h
r955 r973 14 14 private: 15 15 char *txt; ///< string buffer or NULL for empty string 16 int size; ///< allocated memory (including \0)16 int allocated; ///< allocated memory (including \0) 17 17 int used; ///< string length 18 18 int appending; ///< append mode, changes can occur after character # 'appending' … … 20 20 void initEmpty(); 21 21 void copyFrom(SString &from); ///< copy from SString 22 void re size(int newsize);23 void ensure Size(int needed);22 void reallocate(int newsize); 23 void ensureAllocated(int needed); 24 24 const char* getPtr() const { return txt ? txt : ""; } 25 25 … … 34 34 35 35 void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown 36 37 void* operator new(size_t s, void* mem) { return mem; }36 37 void* operator new(size_t s, void* mem) { return mem; } 38 38 #ifdef _MSC_VER 39 39 void operator delete(void* mem, void* t) {} 40 40 #endif 41 void* operator new(size_t s) { return malloc(sizeof(SString)); }41 void* operator new(size_t s) { return malloc(sizeof(SString)); } 42 42 void operator delete(void* mem) { free(mem); } 43 43 44 int len() const { return used; } ///< get string length 44 int length() const { return used; } ///< get string length 45 int size() const { return length(); } ///< get string length 45 46 void shrink(); ///< free unnecessary buffer 46 void reserve(int needed) { ensureSize(needed); } ///< like in std::string47 void reserve(int cap) { ensureAllocated(cap + 1); } ///< like in std::string 47 48 48 49 /// after this call, you can modify sstring directly. … … 87 88 void endAppend(int appendlength = -1); 88 89 89 void memoryHint(int howbig) { ensureSize(howbig); } 90 int directMaxLen() { return size - 1; } ///< when called after directWrite: max number of characters allowed (can be more than requested) 90 int capacity() { return (allocated > 0) ? (allocated - 1) : allocated; } ///< std::string.capacity() 91 91 92 92 /// find a character in SString. -
cpp/frams/util/sstring.cpp
r970 r973 163 163 } 164 164 165 void SString::memoryHint(int howbig)166 {167 detachCopy(howbig);168 }169 170 165 void SString::detachEmpty(int ensuresize) 171 166 { … … 201 196 char *SString::directWrite(int ensuresize) 202 197 { 203 if (ensuresize < 0) ensuresize = len ();198 if (ensuresize < 0) ensuresize = length(); 204 199 REF_LOCK; 205 200 detachCopy(ensuresize); … … 259 254 void SString::operator+=(const SString&s) 260 255 { 261 append(s.c_str(), s.len ());256 append(s.c_str(), s.length()); 262 257 } 263 258 … … 298 293 /////////////////////////////////////// 299 294 300 SString SString::substr(int begin, int length) const301 { 302 if (begin < 0) { length+= begin; begin = 0; }303 if ( length >= (len() - begin)) length = len() - begin;304 if ( length<= 0) return SString();305 if ( length == len()) return *this;306 return SString((*this)(begin), length);295 SString SString::substr(int begin, int numchars) const 296 { 297 if (begin < 0) { numchars += begin; begin = 0; } 298 if (numchars >= (length() - begin)) numchars = length() - begin; 299 if (numchars <= 0) return SString(); 300 if (numchars == length()) return *this; 301 return SString((*this)(begin), numchars); 307 302 } 308 303 … … 337 332 bool SString::getNextToken(int& pos, SString &token, char separator) const 338 333 { 339 if (pos >= len ()) { token = 0; return false; }334 if (pos >= length()) { token = 0; return false; } 340 335 int p1 = pos, p2; 341 336 const char *t1 = buf->txt + pos; 342 337 const char *t2 = strchr(t1, separator); 343 if (t2) pos = (p2 = (t2 - buf->txt)) + 1; else p2 = pos = len ();338 if (t2) pos = (p2 = (t2 - buf->txt)) + 1; else p2 = pos = length(); 344 339 strncpy(token.directWrite(p2 - p1), t1, p2 - p1); 345 340 token.endWrite(p2 - p1); … … 401 396 char* p = ret.directWrite(size); 402 397 assert(p != NULL); 403 size = ret. directMaxLen() + 1;398 size = ret.capacity() + 1; 404 399 /* Try to print in the allocated space. */ 405 400 va_start(ap, format); -
cpp/frams/util/sstring.h
r955 r973 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 15Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 85 85 void operator delete(void* mem) { free(mem); } 86 86 87 int len() const { return buf->used; } ///< get string length 87 int size() const { return buf->used; } ///< get string length 88 int length() const { return buf->used; } ///< get string length 88 89 void shrink(); ///< free unnecessary buffer 89 void reserve(int needed) { ensureSize(needed); } ///< like in std::string90 void reserve(int needed) { detachCopy(needed); } ///< like in std::string 90 91 91 92 /// after this call, you can modify sstring directly. … … 129 130 /// t.endAppend(bytecount);</CODE> 130 131 void endAppend(int appendlength = -1); 131 /// argument is the amount of memory, that will be probably used 132 /// by this string instance. string can use this value 133 /// to optimize memory allocation (bigger chunks will be allocated). 134 void memoryHint(int howbig); 135 int directMaxLen() { return buf->size; } ///< when called after directWrite: max number of characters allowed (can be more than requested) 132 int capacity() { return buf->size; } ///< std::string.capacity() 136 133 137 134 /// find a character in SString. -
cpp/frams/util/sstringutils.cpp
r904 r973 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 15Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 79 79 else 80 80 { 81 if (tmp.len () == 0) return 0; // nothing was changed!81 if (tmp.length() == 0) return 0; // nothing was changed! 82 82 tmp += x; 83 83 target = tmp; … … 112 112 else 113 113 { 114 if (tmp.len () == 0) return 0; // nothing was changed!114 if (tmp.length() == 0) return 0; // nothing was changed! 115 115 tmp += x; 116 116 target = tmp; … … 139 139 bool changed = 0; 140 140 SString tmp; 141 tmp. memoryHint(target.len());141 tmp.reserve(target.length()); 142 142 while (*x) 143 143 { … … 160 160 { 161 161 SString out; 162 if (in.len () > maxlen)163 out = in.substr(0, maxlen / 2) + "..." + in.substr(in.len () - maxlen + maxlen / 2);162 if (in.length() > maxlen) 163 out = in.substr(0, maxlen / 2) + "..." + in.substr(in.length() - maxlen + maxlen / 2); 164 164 else 165 165 { … … 169 169 out = before + out + after; 170 170 if (show_length) 171 out += SString::sprintf(" (length %d)", in.len ());171 out += SString::sprintf(" (length %d)", in.length()); 172 172 return out; 173 173 } … … 208 208 else 209 209 { 210 if (tmp.len () == 0) return 0; // nothing was changed!210 if (tmp.length() == 0) return 0; // nothing was changed! 211 211 tmp += x; 212 212 target = tmp; … … 223 223 { 224 224 n = strchr(t + pos, ','); 225 if ((!strncmp(t + pos, name.c_str(), name.len ())) && (t[pos + name.len()] == '='))226 { 227 if (n) end = n - t; else end = txt.len ();225 if ((!strncmp(t + pos, name.c_str(), name.length())) && (t[pos + name.length()] == '=')) 226 { 227 if (n) end = n - t; else end = txt.length(); 228 228 return pos; 229 229 } … … 238 238 p = strFindField(txt, name, e); 239 239 if (p < 0) return SString(); 240 p += name.len () + 1;240 p += name.length() + 1; 241 241 return SString(txt.substr(p, e - p)); 242 242 } … … 248 248 if (p < 0) 249 249 { 250 if (!value.len ()) return;251 char *t = txt.directAppend(1 + name.len () + value.len());250 if (!value.length()) return; 251 char *t = txt.directAppend(1 + name.length() + value.length()); 252 252 char *b = t; 253 if (txt.len ()) *(t++) = ',';254 strcpy(t, name.c_str()); t += name.len ();253 if (txt.length()) *(t++) = ','; 254 strcpy(t, name.c_str()); t += name.length(); 255 255 *(t++) = '='; 256 strcpy(t, value.c_str()); t += value.len ();256 strcpy(t, value.c_str()); t += value.length(); 257 257 txt.endAppend(t - b); 258 258 } 259 259 else 260 260 { 261 if (!value.len ())262 { 263 if (p > 0) p--; else if (e < txt.len ()) e++;261 if (!value.length()) 262 { 263 if (p > 0) p--; else if (e < txt.length()) e++; 264 264 char *t = txt.directWrite(0); 265 memmove(t + p, t + e, txt.len () - e);266 txt.endWrite(txt.len () + value.len() - (e - p));267 } 268 else 269 { 270 p += name.len () + 1;271 char *t = txt.directWrite(txt.len () + value.len() - (e - p));272 memmove(t + p + value.len (), t + e, txt.len() - e);273 memmove(t + p, value.c_str(), value.len ());274 txt.endWrite(txt.len () + value.len() - (e - p));265 memmove(t + p, t + e, txt.length() - e); 266 txt.endWrite(txt.length() + value.length() - (e - p)); 267 } 268 else 269 { 270 p += name.length() + 1; 271 char *t = txt.directWrite(txt.length() + value.length() - (e - p)); 272 memmove(t + p + value.length(), t + e, txt.length() - e); 273 memmove(t + p, value.c_str(), value.length()); 274 txt.endWrite(txt.length() + value.length() - (e - p)); 275 275 } 276 276 } … … 280 280 { 281 281 const unsigned char*b = (const unsigned char*)s.c_str(); 282 const unsigned char*e = b + s.len ();282 const unsigned char*e = b + s.length(); 283 283 while ((b < e) && (*b <= ' ')) b++; 284 284 while ((b < e) && (e[-1] <= ' ')) e--; 285 if ((e - b) == s.len ()) return s;285 if ((e - b) == s.length()) return s; 286 286 SString newstring; 287 287 char* t = newstring.directWrite(e - b); … … 291 291 } 292 292 293 SString concatPath(const SString& in1, const SString& in2)294 { 295 SString out =in1;296 if (out.len ()>0 && out[out.len()-1]!=PATH_SEPARATOR_CHAR)297 out +=PATH_SEPARATOR_CHAR;298 out +=in2;293 SString concatPath(const SString& in1, const SString& in2) 294 { 295 SString out = in1; 296 if (out.length() > 0 && out[out.length() - 1] != PATH_SEPARATOR_CHAR) 297 out += PATH_SEPARATOR_CHAR; 298 out += in2; 299 299 return out; 300 300 } … … 318 318 bool matchWildcard(const SString& word, const SString& pattern) 319 319 { 320 if (pattern.len () == 0)321 return word.len () == 0;320 if (pattern.length() == 0) 321 return word.length() == 0; 322 322 int aster = pattern.indexOf('*'); 323 323 if (aster >= 0) … … 325 325 SString before = pattern.substr(0, aster); 326 326 SString after = pattern.substr(aster + 1); 327 if (!word.len ()) return false;328 if (before.len ()) if (!word.startsWith(before.c_str())) return false;329 if (after.len ())330 if ((word.len () < after.len())331 || (strcmp(after.c_str(), word.c_str() + word.len () - after.len())))327 if (!word.length()) return false; 328 if (before.length()) if (!word.startsWith(before.c_str())) return false; 329 if (after.length()) 330 if ((word.length() < after.length()) 331 || (strcmp(after.c_str(), word.c_str() + word.length() - after.length()))) 332 332 return false; 333 333 return true; … … 339 339 bool matchWildcardList(const SString& word, const SString& patterns) 340 340 { 341 if (patterns.len () == 0)342 return word.len () == 0;341 if (patterns.length() == 0) 342 return word.length() == 0; 343 343 int pos = 0; 344 344 SString pattern; -
cpp/frams/util/sstringutils.h
r929 r973 40 40 int i, next = 0; 41 41 bool first = true; 42 if (!separator.len ()) { result.push_back(text); return; }42 if (!separator.length()) { result.push_back(text); return; } 43 43 while (1) 44 44 { … … 46 46 if (i < 0) 47 47 { 48 if ((next <= text.len ()) || first)48 if ((next <= text.length()) || first) 49 49 result.push_back(text.substr(next)); 50 50 return; … … 55 55 first = false; 56 56 } 57 next = i + separator.len ();57 next = i + separator.length(); 58 58 } 59 59 }
Note: See TracChangeset
for help on using the changeset viewer.