Changeset 973 for cpp/frams/util/sstring-simple.cpp
- Timestamp:
- 07/03/20 00:37:13 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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);
Note: See TracChangeset
for help on using the changeset viewer.