]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxstring.C
small changes + patch from Dekel
[lyx.git] / src / support / lyxstring.C
index 1e3b542a6d7c58e9734eaeb7720a12d03dba48d8..e3fd5d3e60c5323cde4724bfc569fc412e0cb6cd 100644 (file)
@@ -24,6 +24,8 @@
 #include "LAssert.h"
 
 using std::min;
+using std::istream;
+using std::ostream;
 
 // This class is supposed to be functionaly equivalent to a
 // standard conformant string. This mean among others that we
@@ -414,8 +416,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;
@@ -549,7 +551,7 @@ void lyxstring::reserve(size_type res_arg)
 // Assignment
 ////////////////
 
-lyxstring & lyxstring::operator= (lyxstring const & x)
+lyxstring & lyxstring::operator=(lyxstring const & x)
 {
        TestlyxstringInvariant(this);
 
@@ -557,7 +559,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);
@@ -605,14 +607,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;
@@ -753,7 +754,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;
 }
 
@@ -761,12 +762,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));
 }
 
 
@@ -792,7 +788,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)
 {
@@ -822,7 +818,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;
 }
@@ -831,14 +827,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));
 }
 
 
@@ -1068,7 +1057,7 @@ lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
        if (!n) return npos;
 
        for (size_type t = i; t < rep->sz; ++t) {
-               if(memchr(ptr, rep->s[t], n) != 0) return t;
+               if (memchr(ptr, rep->s[t], n) != 0) return t;
        }
        return npos;
 }
@@ -1122,7 +1111,7 @@ lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
 
        size_type ii = min(rep->sz - 1, i);
        for (int t = ii; t >= 0; --t) {
-               if(memchr(ptr, rep->s[t], n) != 0) return t;
+               if (memchr(ptr, rep->s[t], n) != 0) return t;
        }
        return npos;
 }
@@ -1178,7 +1167,7 @@ lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
 
        if (!n) return (i < rep->sz) ? i : npos;
        for (size_type t = i; t < rep->sz; ++t) {
-               if(memchr(ptr, rep->s[t], n) == 0) return t;
+               if (memchr(ptr, rep->s[t], n) == 0) return t;
        }
        return npos;
 }
@@ -1235,7 +1224,7 @@ lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
        size_type ii = min(rep->sz - 1, i);
 
        for (int t = ii; t >= 0; --t) {
-               if(memchr(ptr, rep->s[t], n) == 0) return t;
+               if (memchr(ptr, rep->s[t], n) == 0) return t;
        }
        return npos;
 }