]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxstring.C
in addition to the changes mentioned in ChangeLog, there is the usual batch of whites...
[lyx.git] / src / support / lyxstring.C
index 10db7b232dca0f78a7dc4a826fe38a02a1a1f88e..13b9b151d72b0b9123916ededcb8d4c55445aa43 100644 (file)
@@ -1,12 +1,12 @@
 /* This file is part of
- * ======================================================
+ * ====================================================== 
  * 
  *           LyX, The Document Processor
  *      
  *         Copyright (C) 1995 Matthias Ettrich
  *          Copyright (C) 1995-1999 The LyX Team.
  *
- * ======================================================*/
+ * ====================================================== */
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -33,26 +33,42 @@ 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
 ///////////////////////////////////////
+#define NEW_ALLOC 1
 
 struct lyxstring::Srep {
+#ifndef NEW_ALLOC
        ///
        static size_t const xtra = static_cast<size_t>(8);
+#endif
        /// size
        size_t sz;
        /// Reference count
@@ -69,12 +85,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 +118,15 @@ 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;
+#ifdef NEW_ALLOC
+       res = sz ? sz : 1;
+#else
        res = sz + xtra;
+#endif
        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 +143,11 @@ lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
 {
        sz = nsz;
        ref = 1;
+#ifdef NEW_ALLOC
+       res = sz ? sz : 1;
+#else
        res = sz + xtra;
+#endif
        s = new value_type[res + 1]; // add space for terminator
        memset(s, ch, sz);
        if (!ch) {
@@ -137,12 +160,16 @@ 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;
+#ifdef NEW_ALLOC
+               res = sz ? sz : 1;
+#else
                res = sz + xtra;
+#endif
                s = new value_type[res + 1]; // add space for terminator
        } else {
                sz = nsz;
@@ -163,7 +190,11 @@ void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
        sz = nsz;
        if (res < nsz) {
                delete[] s;
+#ifdef NEW_ALLOC
+               res = sz ? sz : 1;
+#else
                res = sz + xtra;
+#endif
                s = new value_type[res + 1]; // add space for terminator
        }
        memset(s, ch, sz);
@@ -179,7 +210,13 @@ void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
 {
        register unsigned int const len = sz + asz;
        if (res < len) {
+#ifdef NEW_ALLOC
+               do {
+                       res *= 2;
+               } while (res < len);
+#else
                res = len + xtra;
+#endif
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, sz);
                memcpy(tmp + sz, p, asz);
@@ -198,7 +235,13 @@ 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) {
+#ifdef NEW_ALLOC
+               do {
+                       res *= 2;
+               } while (res < sz);
+#else
                res = sz + xtra;
+#endif
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, sz);
                delete[] s;
@@ -211,7 +254,13 @@ void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
                           lyxstring::size_type n)
 {
        if (res < n + sz) {
+#ifdef NEW_ALLOC
+               do {
+                       res *= 2;
+               } while (res < n + sz);
+#else
                res = sz + n + xtra;
+#endif
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, pos);
                memcpy(tmp + pos, p, n);
@@ -256,7 +305,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)
 {
-// can be called with p=0 and n2=0
+// can be called with p= 0 and n2= 0
        n = min(sz - i, n);
        sz -= n;
        if (res >= n2 + sz) {
@@ -264,7 +313,13 @@ void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
                memcpy(s + i, p, n2);
                sz += n2;
        } else {
+#ifdef NEW_ALLOC
+               do {
+                       res *= 2;
+               } while (res < n2 + sz);
+#else
                res = sz + n2 + xtra;
+#endif
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, i);
                memcpy(tmp + i, p, n2);
@@ -346,10 +401,10 @@ 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 < static_cast<size_t>(1 << (8 * sizeof(object->rep->ref) - 1)));
+       Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
        // if it does ever == then we should be generating a new copy
        // and starting again.  (Is char always 8-bits?)
 }
@@ -419,7 +474,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);
 }
@@ -523,7 +578,7 @@ void lyxstring::reserve(size_type res_arg)
 // Assignment
 ////////////////
 
-lyxstring & lyxstring::operator=(lyxstring const & x)
+lyxstring & lyxstring::operator= (lyxstring const & x)
 {
        TestlyxstringInvariant(this);
 
@@ -531,11 +586,11 @@ lyxstring & lyxstring::operator=(lyxstring const & x)
 }
 
 
-lyxstring & lyxstring::operator=(value_type const * s)
+lyxstring & lyxstring::operator= (value_type const * s)
 {
        Assert(s); // OURS!
        TestlyxstringInvariant(this);
-//     printf("lyxstring::operator=(value_type const *)\n");
+//     printf("lyxstring::operator= (value_type const *)\n");
 
        return assign(s);
 }
@@ -612,7 +667,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);
 
@@ -1345,6 +1400,15 @@ lyxstring & lyxstring::replace(iterator i, iterator i2,
 }
 
 
+void lyxstring::swap(lyxstring & str)
+{
+       if (rep == str.rep) return;
+       Srep * tmp = str.rep;
+       str.rep = rep;
+       rep = tmp;
+}
+
+
 lyxstring & lyxstring::erase(size_type i, size_type n)
 {
        Assert(i <= rep->sz); // STD!
@@ -1424,7 +1488,7 @@ int lyxstring::internal_compare(size_type pos, size_type n,
 {
        if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
        if (!*s) return 1;
-       // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
+       // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
 
         // remember that n can very well be a lot larger than rep->sz
         // so we have to ensure that n is no larger than rep->sz
@@ -1432,7 +1496,7 @@ int lyxstring::internal_compare(size_type pos, size_type n,
        n2 = min(n2, slen);
         if (n == n2)
                return memcmp(&(rep->s[pos]), s, n);
-       int c = memcmp(&(rep->s[pos]), s, min(n,n2));
+       int c = memcmp(&(rep->s[pos]), s, min(n, n2));
        if (c)
                return c;
        if (n < n2)
@@ -1667,16 +1731,36 @@ lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
        return tmp;
 }
 
+
+void swap(lyxstring & str1, lyxstring & str2)
+{
+       str1.swap(str2);
+}
+
+
 #include <iostream>
 
 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;
 }