X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Farray.C;h=48deaf73ab7066e3b125c9b91b8cb0d7bb11a3bb;hb=dff2911bda426ad439e6475f62183cedd7044801;hp=47e6d32ccbd6c5311533c05602e53e5e0b8511c6;hpb=e0aa02ee9f638810aa7508800fa6e683a8988891;p=features.git diff --git a/src/mathed/array.C b/src/mathed/array.C index 47e6d32ccb..48deaf73ab 100644 --- a/src/mathed/array.C +++ b/src/mathed/array.C @@ -1,192 +1,76 @@ - #ifdef __GNUG__ #pragma implementation #endif #include "math_inset.h" +#include "math_charinset.h" +#include "math_scriptinset.h" +#include "math_stringinset.h" #include "debug.h" #include "array.h" -#include "math_scriptinset.h" -#include "math_parser.h" #include "mathed/support.h" +#include "support/LAssert.h" using std::ostream; using std::endl; + MathArray::MathArray() {} -MathArray::~MathArray() -{ - erase(); -} - - -MathArray::MathArray(MathArray const & array) - : bf_(array.bf_) -{ - deep_copy(0, size()); -} - - -MathArray::MathArray(MathArray const & array, int from, int to) - : bf_(array.bf_.begin() + from, array.bf_.begin() + to) -{ - deep_copy(0, size()); -} - - -void MathArray::deep_copy(int pos1, int pos2) -{ - for (int pos = pos1; pos < pos2; next(pos)) - if (isInset(pos)) { - MathInset * p = nextInset(pos)->clone(); - memcpy(&bf_[pos + 1], &p, sizeof(p)); - } -} - - -bool MathArray::next(int & pos) const -{ - if (pos >= size() - 1) - return false; - - pos += item_size(pos); - return true; -} - - -bool MathArray::prev(int & pos) const -{ - if (pos == 0) - return false; - - pos -= item_size(pos - 1); - return true; -} - - -bool MathArray::last(int & pos) const -{ - pos = bf_.size(); - return prev(pos); -} - - -int MathArray::item_size(int pos) const -{ - return 2 + (isInset(pos) ? sizeof(MathInset*) : 1); -} +MathArray::MathArray(MathArray const & array, size_type from, size_type to) + : bf_(array.begin() + from, array.begin() + to) +{} void MathArray::substitute(MathMacro const & m) { - MathArray tmp; - for (int pos = 0; pos < size(); next(pos)) { - if (isInset(pos)) - nextInset(pos)->substitute(tmp, m); - else - tmp.push_back(getChar(pos), getCode(pos)); - } - swap(tmp); -} - - -MathArray & MathArray::operator=(MathArray const & array) -{ - MathArray tmp(array); - swap(tmp); - return *this; + for (iterator it = begin(); it != end(); ++it) + it->nucleus()->substitute(m); } -MathInset * MathArray::nextInset(int pos) const +MathScriptInset const * MathArray::asScript(const_iterator it) const { - if (!isInset(pos)) + if (it->nucleus()->asScriptInset()) return 0; - MathInset * p; - memcpy(&p, &bf_[0] + pos + 1, sizeof(p)); - return p; -} - - -MathInset * MathArray::prevInset(int pos) const -{ - if (!prev(pos)) + const_iterator jt = it + 1; + if (jt == end()) return 0; - return nextInset(pos); -} - - -unsigned char MathArray::getChar(int pos) const -{ - return pos < size() ? bf_[pos + 1] : '\0'; -} - - -string MathArray::getString(int & pos) const -{ - string s; - if (isInset(pos)) - return s; - - MathTextCodes const fcode = getCode(pos); - do { - s += getChar(pos); - next(pos); - } while (pos < size() && !isInset(pos) && getCode(pos) == fcode); - - return s; + return jt->nucleus()->asScriptInset(); } -MathTextCodes MathArray::getCode(int pos) const +MathAtom & MathArray::at(size_type pos) { - return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN; + lyx::Assert(pos < size()); + return bf_[pos]; } -void MathArray::setCode(int pos, MathTextCodes t) +MathAtom const & MathArray::at(size_type pos) const { - if (pos > size() || isInset(pos)) - return; - bf_[pos] = t; - bf_[pos + 2] = t; + lyx::Assert(pos < size()); + return bf_[pos]; } - -void MathArray::insert(int pos, MathInset * p) +void MathArray::insert(size_type pos, MathAtom const & t) { - bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET); - memcpy(&bf_[pos + 1], &p, sizeof(p)); + bf_.insert(begin() + pos, t); } -void MathArray::insert(int pos, unsigned char b, MathTextCodes t) +void MathArray::insert(size_type pos, MathArray const & array) { - bf_.insert(bf_.begin() + pos, 3, t); - bf_[pos + 1] = b; + bf_.insert(begin() + pos, array.begin(), array.end()); } -void MathArray::insert(int pos, MathArray const & array) -{ - bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end()); - deep_copy(pos, pos + array.size()); -} - - -void MathArray::push_back(MathInset * p) +void MathArray::push_back(MathAtom const & t) { - insert(size(), p); -} - - -void MathArray::push_back(unsigned char b, MathTextCodes c) -{ - insert(size(), b, c); + bf_.push_back(t); } @@ -213,9 +97,9 @@ bool MathArray::empty() const { return bf_.empty(); } - -int MathArray::size() const + +MathArray::size_type MathArray::size() const { return bf_.size(); } @@ -227,52 +111,36 @@ void MathArray::erase() } -void MathArray::erase(int pos) +void MathArray::erase(size_type pos) { - if (pos < static_cast(bf_.size())) - erase(pos, pos + item_size(pos)); + if (pos < size()) + erase(pos, pos + 1); } -void MathArray::erase(int pos1, int pos2) +void MathArray::erase(size_type pos1, size_type pos2) { - for (int pos = pos1; pos < pos2; next(pos)) - if (isInset(pos)) - delete nextInset(pos); - bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2); + bf_.erase(begin() + pos1, begin() + pos2); } -bool MathArray::isInset(int pos) const +MathAtom & MathArray::back() { - if (pos >= size()) - return false; - return MathIsInset(static_cast(bf_[pos])); -} - - -MathInset * MathArray::back_inset() const -{ - return prevInset(size()); + return bf_.back(); } void MathArray::dump2(ostream & os) const { - for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it) - os << int(*it) << ' '; - os << endl; + for (const_iterator it = begin(); it != end(); ++it) + os << it->nucleus() << ' '; } void MathArray::dump(ostream & os) const { - for (int pos = 0; pos < size(); next(pos)) { - if (isInset(pos)) - os << ""; - else - os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">"; - } + for (const_iterator it = begin(); it != end(); ++it) + os << "<" << it->nucleus() << ">"; } @@ -283,89 +151,120 @@ std::ostream & operator<<(std::ostream & os, MathArray const & ar) } -void MathArray::write(ostream & os, bool fragile) const +// returns sequence of char with same code starting at it up to end +// it might be less, though... +string charSequence(MathArray::const_iterator it, MathArray::const_iterator end) { - if (empty()) - return; + string s; + MathCharInset const * p = it->nucleus()->asCharInset(); + if (!p) + return s; - int brace = 0; - - for (int pos = 0; pos < size(); next(pos)) { - if (isInset(pos)) { + for (MathTextCodes c = p->code(); it != end; ++it) { + if (!it->nucleus()) + break; + p = it->nucleus()->asCharInset(); + if (!p || p->code() != c) + break; + s += p->getChar(); + } + return s; +} - nextInset(pos)->write(os, fragile); +MathArray MathArray::glueChars() const +{ + MathArray ar; + const_iterator it = begin(); + while (it != end()) { + if (it->nucleus() && it->nucleus()->asCharInset()) { + string s = charSequence(it, end()); + MathTextCodes c = it->nucleus()->asCharInset()->code(); + ar.push_back(MathAtom(new MathStringInset(s, c))); + it += s.size(); } else { - - MathTextCodes fcode = getCode(pos); - unsigned char c = getChar(pos); - - if (MathIsSymbol(fcode)) { - latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM); - - if (l == 0) { - l = lm_get_key_by_id(c, LM_TK_BIGSYM); - } - - if (l) { - os << '\\' << l->name << ' '; - } else { - lyxerr << "Could not find the LaTeX name for " << c << " and fcode " << fcode << "!" << std::endl; - } - } else { - if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM) - os << '\\' << math_font_name[fcode - LM_TC_RM] << '{'; - - // Is there a standard logical XOR? - if ((fcode == LM_TC_TEX && c != '{' && c != '}') || - (fcode == LM_TC_SPECIAL)) - os << '\\'; - else { - if (c == '{') - ++brace; - if (c == '}') - --brace; - } - if (c == '}' && fcode == LM_TC_TEX && brace < 0) - lyxerr <<"Math warning: Unexpected closing brace.\n"; - else - os << c; - } - - if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM) - os << '}'; - + ar.push_back(*it); + ++it; } } + return ar; +} + - if (brace > 0) - os << string(brace, '}'); +void MathArray::write(MathWriteInfo & wi) const +{ + glueChars().write1(wi); } -void MathArray::writeNormal(ostream & os) const +void MathArray::write1(MathWriteInfo & wi) const { - if (empty()) { - os << "[par] "; - return; + for (const_iterator it = begin(); it != end(); ++it) { + MathInset * p = it->nucleus(); + if (!p) + continue; + if (MathScriptInset const * q = asScript(it)) { + q->write(p, wi); + ++it; + } else { + p->write(wi); + } } +} - write(os, true); + +void MathArray::writeNormal(ostream & os) const +{ + for (const_iterator it = begin(); it != end(); ++it) { + MathInset * p = it->nucleus(); + if (!p) + continue; + if (MathScriptInset const * q = asScript(it)) { + q->writeNormal(p, os); + ++it; + } else { + p->writeNormal(os); + } + } } void MathArray::validate(LaTeXFeatures & features) const { - for (int pos = 0; pos < size(); next(pos)) - if (isInset(pos)) - nextInset(pos)->validate(features); + for (const_iterator it = begin(); it != end(); ++it) + it->nucleus()->validate(features); } void MathArray::pop_back() { - int pos = size(); - prev(pos); - erase(pos, size()); + if (!size()) { + lyxerr << "pop_back from empty array!\n"; + return; + } + bf_.pop_back(); } + +MathArray::const_iterator MathArray::begin() const +{ + return bf_.begin(); +} + + +MathArray::const_iterator MathArray::end() const +{ + return bf_.end(); +} + + +MathArray::iterator MathArray::begin() +{ + return bf_.begin(); +} + + +MathArray::iterator MathArray::end() +{ + return bf_.end(); +}