]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxstring.C
another safety belt
[lyx.git] / src / support / lyxstring.C
index cc25a90a9f543d52e2fae01a58c3b8443fb23e26..73f2f67ceb48cc057102cfd52d86f12d6c7033e0 100644 (file)
@@ -1,12 +1,12 @@
-/* This file is part of
- * ====================================================== 
- * 
- *           LyX, The Document Processor
- *      
- *         Copyright 1995 Matthias Ettrich
- *          Copyright 1995-2000 The LyX Team.
+/**
+ * \file lyxstring.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * ====================================================== */
+ * \author Lars Gullik Bjønnes
+ *
+ * Full author contact details are available in file CREDITS
+ */
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -77,7 +77,7 @@ struct lyxstring::Srep {
        size_t res;
        /// Data. At least 1 char for trailing null.
        lyxstring::value_type * s;
-       
+
        ///
        Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
        ///
@@ -90,7 +90,7 @@ struct lyxstring::Srep {
                --ref;
                return new Srep(sz, s);
        }
-       
+
        ///
        void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
        ///
@@ -149,7 +149,7 @@ lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
                sz = 0;
        }
 }
-       
+
 
 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
 {
@@ -296,7 +296,7 @@ void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
                memcpy(tmp + i + n2, &s[i + n], sz - i);
                delete[] s;
                s = tmp;
-               sz += n2; 
+               sz += n2;
        }
 }
 
@@ -304,12 +304,16 @@ void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
 ///////////////////////////////////////
 // The lyxstring Invariant tester
 ///////////////////////////////////////
+
+// There are no know bugs in lyxstring now, and it have been
+// tested for a long time. so we disable the invariant checker. (Lgb)
+#undef ENABLE_ASSERTIONS
 #ifdef ENABLE_ASSERTIONS
 
 /** Testing of the lyxstring invariant
  * By creating an object that tests the lyxstring invariant during its
  * construction *and* its deconstruction we greatly simplify our code.
- * Calling TestlyxstringInvariant() upon entry to an lyxstring method 
+ * Calling TestlyxstringInvariant() upon entry to an lyxstring method
  * will test the invariant upon entry to the code.  If the Asserts fail
  * then we know from the stack trace that the corruption occurred *before*
  * entry to this method.  We can also be sure it didn't happen in any of
@@ -519,7 +523,7 @@ const_reverse_iterator lyxstring::rend() const
 ///////////////////////
 
 lyxstring::size_type lyxstring::size() const
-{ 
+{
        return rep->sz;
 }
 
@@ -597,7 +601,7 @@ lyxstring & lyxstring::assign(lyxstring const & x)
        rep = x.rep; // share representation
        return *this;
 }
-       
+
 
 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
 {
@@ -606,7 +610,7 @@ lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
 
        return assign(x.substr(pos, n));
 }
-       
+
 
 lyxstring & lyxstring::assign(value_type const * s, size_type n)
 {
@@ -621,7 +625,7 @@ lyxstring & lyxstring::assign(value_type const * s, size_type n)
        }
        return *this;
 }
-       
+
 
 lyxstring & lyxstring::assign(value_type const * s)
 {
@@ -659,10 +663,13 @@ lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
 lyxstring::const_reference lyxstring::operator[](size_type pos) const
 {
 #if 0
+       // This is actually what the standard requires,
        lyx::Assert(pos <= rep->sz); // OURS!
-       static char helper = '\0';
+       static char const helper = '\0';
        return pos == rep->sz ? helper : rep->s[pos];
 #else
+       // but we use this one since it is stricter
+       // and more according to the real intent of std::string.
        lyx::Assert(pos < rep->sz); // OURS!
        return rep->s[pos];
 #endif
@@ -877,21 +884,21 @@ void lyxstring::insert(iterator p, iterator first, iterator last)
 
        insert(p - begin(), first, last - first);
 }
-       
+
 
 ////////////////
 // Find
 ////////////////
-         // All the below find functions should be verified,
-         // it is very likely that I have mixed up or interpreted
-         // some of the parameters wrong, also some of the funcs can surely
-         // be written more effectively.
+
+        // All the below find functions should be verified,
+        // it is very likely that I have mixed up or interpreted
+        // some of the parameters wrong, also some of the funcs can surely
+        // be written more effectively.
 
 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
 {
        if (!rep->sz || i >= rep->sz) return npos;
-       
+
        TestlyxstringInvariant(this);
 
        size_type n = a.length();
@@ -919,7 +926,7 @@ lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
 {
        lyx::Assert(ptr); // OURS!
        if (!rep->sz || !*ptr || i >= rep->sz) return npos;
-       
+
        TestlyxstringInvariant(this);
 
        // What is "n" here? is it the number of value_types to use in ptr
@@ -950,7 +957,7 @@ lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
 {
        lyx::Assert(s); // OURS!
        if (!rep->sz || i >= rep->sz) return npos;
-       
+
        TestlyxstringInvariant(this);
 
        if (!s || !*s) return npos;
@@ -964,10 +971,10 @@ lyxstring::size_type lyxstring::find(value_type c, size_type i) const
 
        TestlyxstringInvariant(this);
 
-        for (size_type t = 0; t + i < rep->sz; ++t) {
-               if (rep->s[t + i] == c) return t + i;
+       for (size_type t = 0; t + i < rep->sz; ++t) {
+               if (rep->s[t + i] == c) return t + i;
        }
-        return npos;
+       return npos;
 }
 
 
@@ -992,7 +999,7 @@ lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
                        }
                        if (equal) return t;
                }
-       } while(t-- > 0);
+       } while (t-- > 0);
        return npos;
 }
 
@@ -1332,11 +1339,18 @@ lyxstring & lyxstring::replace(size_type i, size_type n,
 }
 
 
+/// FY! FY! FY! go away !
+lyxstring & lyxstring::replace(size_type i, size_type n, value_type c)
+{
+       return replace(i, n, 1, c);
+}
+
+
 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
 {
        TestlyxstringInvariant(this);
 
-       return replace(i - begin(), i2 - i, str); 
+       return replace(i - begin(), i2 - i, str);
 }
 
 
@@ -1366,7 +1380,7 @@ lyxstring & lyxstring::replace(iterator i, iterator i2,
 
        return replace(i - begin(), i2 - i, n, c);
 }
-       
+
 
 lyxstring & lyxstring::replace(iterator i, iterator i2,
                               iterator j, iterator j2)
@@ -1467,11 +1481,11 @@ int lyxstring::internal_compare(size_type pos, size_type n,
        if (!*s) return 1;
        // 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
-        n = min(n, rep->sz);
+       // 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
+       n = min(n, rep->sz);
        n2 = min(n2, slen);
-        if (n == n2)
+       if (n == n2)
                return memcmp(&(rep->s[pos]), s, n);
        int c = memcmp(&(rep->s[pos]), s, min(n, n2));
        if (c)
@@ -1720,7 +1734,7 @@ void swap(lyxstring & str1, lyxstring & str2)
 
 istream & operator>>(istream & is, lyxstring & s)
 {
-#if 1
+#if 0
        // very bad solution
        char * nome = new char[1024];
        is >> nome;
@@ -1729,15 +1743,23 @@ istream & operator>>(istream & is, lyxstring & s)
        if (!tmp.empty()) s = tmp;
 #else
        // better solution
-       int w = is.widdth(0);
+       int w = is.width(0);
        s.clear();
        char c = 0;
+       bool skipspace = true;
        while (is.get(c)) {
-               if (isspace(c)) { is.putback(c); break; }
-               s += c;
+               if (isspace(c)) {
+                       if (!skipspace) {
+                               is.putback(c);
+                               break;
+                       }
+               } else {
+                       s += c;
+                       skipspace = false;
+               }
                if (--w == 1) break;
        }
-       if (s.empty()) is.setstate(ios::failbit);
+       if (s.empty()) is.setstate(std::ios::failbit);
 #endif
        return is;
 }
@@ -1755,7 +1777,7 @@ istream & getline(istream & is, lyxstring & s,
        // very bad solution
        char tmp = 0;
        s.erase();
-       while(is) {
+       while (is) {
                is.get(tmp);
                if (tmp != delim) {
                        s += tmp;