]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/array.C
mathed58.diff
[lyx.git] / src / mathed / array.C
index 661fee1ae8d729f8b97ebb5c5a21e6614460e7e4..bc5b02596d7fb8a7704f6c016ce97a8cbc988018 100644 (file)
@@ -9,7 +9,11 @@
 #include "math_iter.h"
 #include "math_inset.h"
 
-// Is this still needed? (Lgb)
+#include "support/LOstream.h"
+
+using std::ostream;
+using std::endl;
+
 static inline
 void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
 {
@@ -21,9 +25,10 @@ void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
 
 
 MathedArray::MathedArray()
-       : bf_(1, 0), last_(0)
+       : bf_(1, '\0'), last_(0)
 {}
 
+
 MathedArray::~MathedArray()
 {
        // deep destruction
@@ -54,14 +59,17 @@ MathedArray::MathedArray(MathedArray const & array)
        last_ = array.last_;
 
        // deep copy
-       // we'll not yet get exeption safety
-       MathedIter it;
-       it.SetData(this);
+       deep_copy();
+}
+
+void MathedArray::deep_copy()
+{
+       MathedIter it(this);
        while (it.OK()) {
                if (it.IsInset()) {
                        MathedInset * inset = it.GetInset();
                        inset = inset->Clone();
-                       raw_pointer_insert(inset, it.getPos() + 1, sizeof(inset));
+                       raw_pointer_insert(inset, it.getPos() + 1);
                }
                it.Next();
        }
@@ -75,6 +83,13 @@ MathedArray & MathedArray::operator=(MathedArray const & array)
        return *this;
 }
 
+void MathedArray::clear()
+{
+       last_ = 0;
+       bf_.resize(1);
+       bf_[0] = 0;
+}
+
 void MathedArray::swap(MathedArray & array)
 {
        if (this != &array) {
@@ -155,6 +170,44 @@ void MathedArray::move(int p, int shift)
 }
 
 
+
+void MathedArray::shrink(int pos1, int pos2)
+{
+       if (pos1 == 0 && pos2 >= last())        
+               return;
+
+       short fc = 0;
+       if (pos1 > 0 && bf_[pos1] > ' ') {
+               for (int p = pos1; p >= 0; --p) {
+                       if (MathIsFont(bf_[p])) {
+                               if (p != pos1 - 1)
+                                       fc = bf_[p];
+                               else
+                                       --pos1;
+                               break;
+                       }
+               }
+       }
+
+       if (pos2 > 0 && bf_[pos2] >= ' ' && MathIsFont(bf_[pos2 - 1]))
+               --pos2;
+
+       int dx = pos2 - pos1;
+       MathedArray a;
+       a.resize(dx + 1);
+       strange_copy(&a, (fc) ? 1 : 0, pos1, dx);
+       if (fc) {
+               a[0] = fc;
+               ++dx;
+       }
+       a.last(dx);
+       a[dx] = '\0';
+
+       swap(a);
+       deep_copy();
+}
+
+
 #if 0
 void MathedArray::insert(MathedArray::iterator pos,
                         MathedArray::const_iterator beg,
@@ -164,9 +217,9 @@ void MathedArray::insert(MathedArray::iterator pos,
        last_ = bf_.size() - 1;
 }
 #else
-void MathedArray::mergeF(MathedArray * a, int p, int dx)
+void MathedArray::merge(MathedArray const & a, int p)
 {
-       my_memcpy(&bf_[p], &a->bf_[0], dx);
+       my_memcpy(&bf_[p], &a.bf_[0], a.last());
 }
 #endif
 
@@ -201,9 +254,9 @@ MathedInset * MathedArray::getInset(int pos)
 }
 
 #else
-void MathedArray::raw_pointer_insert(void * p, int pos, int len)
+void MathedArray::raw_pointer_insert(void * p, int pos)
 {
-       my_memcpy(&bf_[pos], &p, len);
+       my_memcpy(&bf_[pos], &p, sizeof(p));
 }
 #endif
 
@@ -225,3 +278,16 @@ byte & MathedArray::operator[](int i)
 {
        return bf_[i];
 }
+
+
+void MathedArray::dump(ostream & os) const
+{
+       buffer_type::const_iterator cit = bf_.begin();
+       buffer_type::const_iterator end = bf_.end();
+       for (; cit != end; ++cit) {
+               os << (*cit);
+       }
+       os << endl;
+}
+
+