]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxstring.C
small changes and two patches from Dekel
[lyx.git] / src / support / lyxstring.C
index fd22e800c90441102568e9c36f921bbcf0cb1ae7..4e7438d69f5128183056ac42d59ea9d2311c9291 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.
  *
  * ====================================================== */
 
@@ -114,7 +114,8 @@ 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;
@@ -148,7 +149,8 @@ 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;
@@ -223,7 +225,7 @@ 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) {
                do {
@@ -271,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);
@@ -298,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
@@ -377,7 +379,7 @@ void lyxstringInvariant::helper() const
 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
 #else
 #define TestlyxstringInvariant(s)
-#endif //DEVEL_VERSION
+#endif /* ENABLE_ASSERTIONS */
 
 
 ///////////////////////////////////////
@@ -412,8 +414,8 @@ lyxstring::lyxstring(value_type const * s, size_type n)
 {
        Assert(s && n < npos); // STD!
        static Srep empty_rep(0, "");
-       if (*s && n) { // s is not empty string and n > 0
-               rep = new Srep(min(strlen(s), n), s);
+       if (n) { // n > 0
+               rep = new Srep(n, s);
        } else {
                ++empty_rep.ref;
                rep = &empty_rep;
@@ -458,6 +460,7 @@ lyxstring::~lyxstring()
 
 lyxstring::iterator lyxstring::begin()
 {
+       rep = rep->get_own_copy();
        return rep->s;
 }
 
@@ -470,6 +473,7 @@ lyxstring::const_iterator lyxstring::begin() const
 
 lyxstring::iterator lyxstring::end()
 {
+       rep = rep->get_own_copy();
        return rep->s + rep->sz;
 }
 
@@ -545,7 +549,7 @@ void lyxstring::reserve(size_type res_arg)
 // Assignment
 ////////////////
 
-lyxstring & lyxstring::operator= (lyxstring const & x)
+lyxstring & lyxstring::operator=(lyxstring const & x)
 {
        TestlyxstringInvariant(this);
 
@@ -553,7 +557,7 @@ 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);
@@ -601,14 +605,13 @@ lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
 
 lyxstring & lyxstring::assign(value_type const * s, size_type n)
 {
-       Assert(s); // OURS!
+       Assert(s && n < npos); // STD!
        TestlyxstringInvariant(this);
 
-       n = min(strlen(s), n);
        if (rep->ref == 1) // recycle rep
                rep->assign(n, s);
        else {
-               rep->ref--;
+               --rep->ref;
                rep = new Srep(n, s);
        }
        return *this;
@@ -749,7 +752,7 @@ lyxstring & lyxstring::append(value_type const * p, size_type n)
 
        if (!*p || !n) return *this;
        rep = rep->get_own_copy();
-       rep->append(min(n, strlen(p)), p);
+       rep->append(n, p);
        return *this;
 }
 
@@ -757,12 +760,7 @@ lyxstring & lyxstring::append(value_type const * p, size_type n)
 lyxstring & lyxstring::append(value_type const * p)
 {
        Assert(p); // OURS!
-       TestlyxstringInvariant(this);
-
-       if (!*p) return *this;
-       rep = rep->get_own_copy();
-       rep->append(strlen(p), p);
-       return *this;
+       return append(p, strlen(p));
 }
 
 
@@ -788,7 +786,7 @@ lyxstring & lyxstring::append(iterator first, iterator last)
        return *this;
 }
 
-// insert value_typeacters before (*this)[pos]
+// insert characters before (*this)[pos]
 
 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
 {
@@ -799,7 +797,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);
@@ -818,7 +816,7 @@ lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
        if (*p && n) {
                // insert nothing and you change nothing
                rep = rep->get_own_copy();
-               rep->insert(pos, p, min(n, strlen(p)));
+               rep->insert(pos, p, n);
        }
        return *this;
 }
@@ -827,14 +825,7 @@ lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
 {
        Assert(p); // OURS!
-       TestlyxstringInvariant(this);
-
-       if (*p) {
-               // insert nothing and you change nothing
-               rep = rep->get_own_copy();
-               rep->insert(pos, p, strlen(p));
-       }
-       return *this;
+       return insert(pos, p, strlen(p));
 }
 
 
@@ -912,7 +903,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;
@@ -987,7 +978,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);
@@ -1313,7 +1304,7 @@ lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
 lyxstring & lyxstring::replace(size_type i, size_type n,
                               size_type n2, value_type c)
 {
-       Assert(i < rep->sz);  // OURS!
+       Assert(i <= rep->sz);  // OURS!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();