]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxstring.C
use more explicit on constructors use the pimpl idom to reduce size with about 500k
[lyx.git] / src / support / lyxstring.C
index 1083669415cb55e15b69b6226a7c82261672e078..606e689304baf35e3e50f1fcd10a990edd6352d2 100644 (file)
@@ -3,8 +3,8 @@
  * 
  *           LyX, The Document Processor
  *      
- *         Copyright (C) 1995 Matthias Ettrich
- *          Copyright (C) 1995-1999 The LyX Team.
+ *         Copyright 1995 Matthias Ettrich
+ *          Copyright 1995-2000 The LyX Team.
  *
  * ====================================================== */
 
@@ -33,26 +33,38 @@ using std::min;
 
 // Asserts with a STD! are required by the standard.
 // Asserts with a OURS! are added by me.
+// Some asserts could still be missing and some of the existing
+// ones might be wrong or not needed.
+
 // Reference count has been checked, empty_rep removed and
 // introduced again in a similar guise. Where is empty_rep _really_
 // needed?
 
-// Insertion and replace is implemented, as far as I can see everything
-// works, but could perhaps be done smarter.
+// We are missing a couple of imporant things from the standard:
+// reverse iterators and methods taking InputIterators as paramters.
+// Also the methods returning iterators is returning the wrong value.
 
 // All the different find functions need a good look over.
 // I have so far not tested them extensively and would be
 // happy if others took the time to have a peek.
 
+// Space allocation of string.
+// I have tried to do this very simple without using any special tricks.
+// Earlier we used a fixed value to enlarge the string with this would
+// cause a lot of reallocations with large strings (especially if
+// push_back was used) and wasting space for very small strings.
+// I have now changed the allocation to use a doubling of reserved
+// space until it is large enough. So far tests show a small speed
+// increase and a noticable memory saving.
+
 // Lgb.
 
+
 ///////////////////////////////////////
 // The internal string representation
 ///////////////////////////////////////
 
 struct lyxstring::Srep {
-       ///
-       static size_t const xtra = static_cast<size_t>(8);
        /// size
        size_t sz;
        /// Reference count
@@ -69,12 +81,11 @@ struct lyxstring::Srep {
        ///
        ~Srep() { delete[] s; }
        ///
-       Srep * get_own_copy()
-               {
-                       if (ref == 1) return this;
-                       --ref;
-                       return new Srep(sz, s);
-               }
+       Srep * get_own_copy() {
+               if (ref == 1) return this;
+               --ref;
+               return new Srep(sz, s);
+       }
        
        ///
        void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
@@ -103,11 +114,12 @@ private:
 
 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
 {
-// can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
+       // can be called with p == 0 by
+       // lyxstring::assign(const value_type *, size_type)
 
        sz = nsz;
        ref = 1;
-       res = sz + xtra;
+       res = sz ? sz : 1;
        s = new value_type[res + 1]; // add space for terminator
        if (p && sz) {
                // if sz = 0 nothing gets copied and we have an error
@@ -124,7 +136,7 @@ lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
 {
        sz = nsz;
        ref = 1;
-       res = sz + xtra;
+       res = sz ? sz : 1;
        s = new value_type[res + 1]; // add space for terminator
        memset(s, ch, sz);
        if (!ch) {
@@ -137,12 +149,13 @@ lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
 
 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
 {
-// can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
+       // can be called with p == 0
+       // by lyxstring::assign(const value_type *, size_type)
 
        if (res < nsz) {
                delete[] s;
                sz = nsz;
-               res = sz + xtra;
+               res = sz ? sz : 1;
                s = new value_type[res + 1]; // add space for terminator
        } else {
                sz = nsz;
@@ -163,7 +176,7 @@ void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
        sz = nsz;
        if (res < nsz) {
                delete[] s;
-               res = sz + xtra;
+               res = sz ? sz : 1;
                s = new value_type[res + 1]; // add space for terminator
        }
        memset(s, ch, sz);
@@ -179,7 +192,9 @@ void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
 {
        register unsigned int const len = sz + asz;
        if (res < len) {
-               res = len + xtra;
+               do {
+                       res *= 2;
+               } while (res < len);
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, sz);
                memcpy(tmp + sz, p, asz);
@@ -198,7 +213,9 @@ void lyxstring::Srep::push_back(value_type c)
        s[sz] = c; // it is always room to put a value_type at the end
        ++sz;
        if (res < sz) {
-               res = sz + xtra;
+               do {
+                       res *= 2;
+               } while (res < sz);
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, sz);
                delete[] s;
@@ -208,19 +225,21 @@ void lyxstring::Srep::push_back(value_type c)
 
 
 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
-                          lyxstring::size_type n)
+                            lyxstring::size_type n)
 {
        if (res < n + sz) {
-               res = sz + n + xtra;
+               do {
+                       res *= 2;
+               } while (res < n + sz);
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, pos);
                memcpy(tmp + pos, p, n);
-               memcpy(tmp + pos + n, & s[pos], sz - pos);
+               memcpy(tmp + pos + n, &s[pos], sz - pos);
                sz += n;
                delete[] s;
                s = tmp;
        } else {
-               memmove(s + pos + n, & s[pos], sz - pos);
+               memmove(s + pos + n, &s[pos], sz - pos);
                memcpy(s + pos, p, n);
                sz += n;
        }
@@ -254,7 +273,7 @@ void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
 
 
 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
-                           value_type const * p, size_type n2)
+                             value_type const * p, size_type n2)
 {
 // can be called with p= 0 and n2= 0
        n = min(sz - i, n);
@@ -264,7 +283,9 @@ void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
                memcpy(s + i, p, n2);
                sz += n2;
        } else {
-               res = sz + n2 + xtra;
+               do {
+                       res *= 2;
+               } while (res < n2 + sz);
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, i);
                memcpy(tmp + i, p, n2);
@@ -279,7 +300,7 @@ void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
 ///////////////////////////////////////
 // The lyxstring Invariant tester
 ///////////////////////////////////////
-#ifdef DEVEL_VERSION
+#ifdef ENABLE_ASSERTIONS
 
 /** Testing of the lyxstring invariant
  * By creating an object that tests the lyxstring invariant during its
@@ -329,12 +350,14 @@ lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
        helper();
 }
 
+
 lyxstringInvariant::~lyxstringInvariant()
 {
        helper();
        // printf("lyxstringInvariant destructor completed\n");
 }
 
+
 void lyxstringInvariant::helper() const
 {
        // Some of these tests might look pointless but they are
@@ -346,7 +369,7 @@ void lyxstringInvariant::helper() const
        Assert(object);
        Assert(object->rep);
        Assert(object->rep->s);    // s is never 0
-       Assert(object->rep->res);  // always some space allocated
+       Assert(object->rep->res);  // res cannot be 0
        Assert(object->rep->sz <= object->rep->res);
        Assert(object->rep->ref >= 1);  // its in use so it must be referenced
        Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
@@ -356,7 +379,7 @@ void lyxstringInvariant::helper() const
 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
 #else
 #define TestlyxstringInvariant(s)
-#endif //DEVEL_VERSION
+#endif /* ENABLE_ASSERTIONS */
 
 
 ///////////////////////////////////////
@@ -366,6 +389,7 @@ void lyxstringInvariant::helper() const
 lyxstring::size_type const lyxstring::npos =
 static_cast<lyxstring::size_type>(-1);
 
+
 lyxstring::lyxstring()
 {
        static Srep empty_rep(0, "");
@@ -419,7 +443,7 @@ lyxstring::lyxstring(size_type n, value_type c)
 }
 
 
-lyxstring::lyxstring(iterator first, iterator last)
+lyxstring::lyxstring(const_iterator first, const_iterator last)
 {
        rep = new Srep(last - first, first);
 }
@@ -612,7 +636,7 @@ lyxstring & lyxstring::assign(size_type n, value_type ch)
 }
 
 
-lyxstring & lyxstring::assign(iterator first, iterator last)
+lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
 {
        TestlyxstringInvariant(this);
 
@@ -777,7 +801,7 @@ lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
 
 
 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
-                         size_type pos2, size_type n)
+                             size_type pos2, size_type n)
 {
        Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
        TestlyxstringInvariant(this);
@@ -890,7 +914,7 @@ lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
 
 
 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
-                                size_type n) const
+                                    size_type n) const
 {
        Assert(ptr); // OURS!
        if (!rep->sz || !*ptr || i >= rep->sz) return npos;
@@ -965,7 +989,7 @@ lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
 
 
 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
-                                 size_type n) const
+                                     size_type n) const
 {
        Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
@@ -1021,7 +1045,7 @@ lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
 
 
 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
-                                         size_type i) const
+                                             size_type i) const
 {
        Assert(i < rep->sz); // OURS!
        TestlyxstringInvariant(this);
@@ -1033,8 +1057,9 @@ lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
 }
 
 
-lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
-                                         size_type n) const
+lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
+                                             size_type i,
+                                             size_type n) const
 {
        Assert(ptr && i < rep->sz); // OURS!
        TestlyxstringInvariant(this);
@@ -1048,7 +1073,7 @@ lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type
 
 
 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
-                                         size_type i) const
+                                             size_type i) const
 {
        Assert(ptr && i < rep->sz); // OURS!
        TestlyxstringInvariant(this);
@@ -1073,7 +1098,7 @@ lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
 
 
 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
-                                        size_type i) const
+                                            size_type i) const
 {
        TestlyxstringInvariant(this);
 
@@ -1084,8 +1109,10 @@ lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
        return npos;
 }
 
-lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
-                                        size_type n) const
+
+lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
+                                            size_type i,
+                                            size_type n) const
 {
        Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
@@ -1100,7 +1127,7 @@ lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i
 
 
 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
-                                        size_type i) const
+                                            size_type i) const
 {
        Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
@@ -1467,7 +1494,8 @@ int lyxstring::compare(value_type const * s) const
 }
 
 
-int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
+int lyxstring::compare(size_type pos, size_type n,
+                      lyxstring const & str) const
 {
        Assert(pos <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
@@ -1687,20 +1715,35 @@ void swap(lyxstring & str1, lyxstring & str2)
 
 istream & operator>>(istream & is, lyxstring & s)
 {
+#if 1
        // very bad solution
        char * nome = new char[1024];
        is >> nome;
        lyxstring tmp(nome);
        delete [] nome;
        if (!tmp.empty()) s = tmp;
+#else
+       // better solution
+       int w = is.widdth(0);
+       s.clear();
+       char c = 0;
+       while (is.get(c)) {
+               if (isspace(c)) { is.putback(c); break; }
+               s += c;
+               if (--w == 1) break;
+       }
+       if (s.empty()) is.setstate(ios::failbit);
+#endif
        return is;
 }
 
+
 ostream & operator<<(ostream & o, lyxstring const & s)
 {
        return o.write(s.data(), s.length());
 }
 
+
 istream & getline(istream & is, lyxstring & s,
                  lyxstring::value_type delim)
 {