Ignore:
Timestamp:
07/03/20 00:37:13 (4 years ago)
Author:
Maciej Komosinski
Message:

Increased SString and std::string compatibility: introduced length(), size(), and capacity(), and removed legacy methods that have std::string equivalents

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/sstring-simple.cpp

    r970 r973  
    88void SString::initEmpty()
    99{
    10         txt = NULL; used = 0; size = 0;
     10        txt = NULL; used = 0; allocated = 0;
    1111}
    1212
     
    1818SString::~SString()
    1919{
    20         resize(0);
     20        reallocate(0);
    2121}
    2222
     
    3636SString::SString(SString&& from)
    3737{
    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;
    4040}
    4141
     
    4646}
    4747
    48 void SString::resize(int newsize)
    49 {
    50         if (newsize == size) return;
     48void SString::reallocate(int newsize)
     49{
     50        if (newsize == allocated) return;
    5151        txt = (char*)realloc(txt, newsize);
    52         size = newsize;
    53 }
    54 
    55 void SString::ensureSize(int needed)
    56 {
    57         if (size > needed) return;
    58         resize((size > 0) ? (needed + needed / 2 + 1) : (needed + 1));
     52        allocated = newsize;
     53}
     54
     55void SString::ensureAllocated(int needed)
     56{
     57        if (allocated > needed) return;
     58        reallocate((allocated > 0) ? (needed + needed / 2 + 1) : (needed + 1));
    5959}
    6060
    6161char *SString::directWrite(int ensuresize)
    6262{
    63         ensureSize(ensuresize);
     63        ensureAllocated(ensuresize);
    6464        appending = used;
    6565        return txt;
     
    6868char *SString::directAppend(int maxappend)
    6969{
    70         ensureSize(used + maxappend);
     70        ensureAllocated(used + maxappend);
    7171        appending = used;
    7272        return txt + appending;
     
    7878        else txt[newlength] = 0;
    7979        used = newlength;
    80         assert(used < size);
     80        assert(used < allocated);
    8181}
    8282
     
    8686        else txt[appending + newappend] = 0;
    8787        used = appending + newappend;
    88         assert(used < size);
     88        assert(used < allocated);
    8989}
    9090
     
    102102{
    103103        if (!n) return;
    104         ensureSize(used + n);
     104        ensureAllocated(used + n);
    105105        memmove(txt + used, t, n);
    106106        used += n;
     
    110110void SString::operator+=(const SString&s)
    111111{
    112         append(s.c_str(), s.len());
     112        append(s.c_str(), s.length());
    113113}
    114114
     
    116116{
    117117        SString ret;
    118         ret.reserve(len() + s.len());
     118        ret.reserve(length() + s.length());
    119119        ret = *this;
    120120        ret += s;
     
    130130        if (chlen)
    131131        {
    132                 ensureSize(chlen);
     132                ensureAllocated(chlen);
    133133                memmove(txt, ch, chlen);
    134134                txt[chlen] = 0;
     
    153153{
    154154        if (&s == this) return;
    155         copyFrom(s.c_str(), s.len());
     155        copyFrom(s.c_str(), s.length());
    156156}
    157157
    158158///////////////////////////////////////
    159159
    160 SString SString::substr(int begin, int length) const
    161 {
    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);
     160SString 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);
    167167}
    168168
     
    172172{
    173173        if (this == &s) return true;
    174         if (len() != s.len()) return false;
     174        if (length() != s.length()) return false;
    175175        return strcmp(getPtr(), s.getPtr()) == 0;
    176176}
     
    198198bool SString::getNextToken(int& pos, SString &token, char separator) const
    199199{
    200         if (pos >= len()) { token = 0; return false; }
     200        if (pos >= length()) { token = 0; return false; }
    201201        int p1 = pos, p2;
    202202        const char *t1 = getPtr() + pos;
    203203        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();
    205205        strncpy(token.directWrite(p2 - p1), t1, p2 - p1);
    206206        token.endWrite(p2 - p1);
     
    254254                char* p = ret.directWrite(size);
    255255                assert(p != NULL);
    256                 size = ret.directMaxLen() + 1;
     256                size = ret.capacity() + 1;
    257257                /* Try to print in the allocated space. */
    258258                va_start(ap, format);
Note: See TracChangeset for help on using the changeset viewer.