#include "InsetMathFrac.h"
#include "InsetMathLim.h"
#include "InsetMathMatrix.h"
-#include "MathMLStream.h"
+#include "MathStream.h"
#include "InsetMathNumber.h"
#include "InsetMathScript.h"
#include "InsetMathString.h"
using frontend::function_names;
-using std::string;
using std::endl;
using std::find_if;
using std::auto_ptr;
using std::istringstream;
using std::ostream;
using std::swap;
+using std::string;
using std::vector;
+static size_t const npos = lyx::docstring::npos;
// define a function for tests
typedef bool TestItemFunc(MathAtom const &);
// returns sequence of char with same code starting at it up to end
// it might be less, though...
-string charSequence
+docstring charSequence
(MathArray::const_iterator it, MathArray::const_iterator end)
{
- string s;
+ docstring s;
for (; it != end && (*it)->asCharInset(); ++it)
s += (*it)->getChar();
return s;
for (size_t i = 0; i < ar.size(); ++i) {
if (!ar[i]->asCharInset())
continue;
- string s = charSequence(ar.begin() + i, ar.end());
+ docstring s = charSequence(ar.begin() + i, ar.end());
ar[i] = MathAtom(new InsetMathString(s));
ar.erase(i + 1, i + s.size());
}
// convert this inset somehow to a string
-bool extractString(MathAtom const & at, string & str)
+bool extractString(MathAtom const & at, docstring & str)
{
if (at->getChar()) {
- str = string(1, at->getChar());
+ str = docstring(1, at->getChar());
return true;
}
if (at->asStringInset()) {
// is this a known function?
-bool isKnownFunction(string const & str)
+bool isKnownFunction(docstring const & str)
{
for (int i = 0; *function_names[i]; ++i) {
if (str == function_names[i])
// extract a function name from this inset
-bool extractFunctionName(MathAtom const & at, string & str)
+bool extractFunctionName(MathAtom const & at, docstring & str)
{
if (at->asSymbolInset()) {
str = at->asSymbolInset()->name();
// convert this inset somehow to a number
bool extractNumber(MathArray const & ar, int & i)
{
- istringstream is(charSequence(ar.begin(), ar.end()));
+ idocstringstream is(charSequence(ar.begin(), ar.end()));
is >> i;
return is;
}
bool extractNumber(MathArray const & ar, double & d)
{
- istringstream is(charSequence(ar.begin(), ar.end()));
+ idocstringstream is(charSequence(ar.begin(), ar.end()));
is >> d;
return is;
}
-bool testString(MathAtom const & at, string const & str)
+bool testString(MathAtom const & at, docstring const & str)
{
- string s;
+ docstring s;
return extractString(at, s) && str == s;
}
+bool testString(MathAtom const & at, char const * const str)
+{
+ return testString(at, from_ascii(str));
+}
+
// search end of nested sequence
MathArray::iterator endNestSearch(
MathArray::iterator it,
continue;
// create a proper exp-inset as replacement
- ar[i] = MathAtom(new InsetMathExFunc("exp", sup->cell(1)));
+ ar[i] = MathAtom(new InsetMathExFunc(from_ascii("exp"), sup->cell(1)));
ar.erase(i + 1);
}
//lyxerr << "\nExps to: " << ar << endl;
continue;
if (!del->isAbs())
continue;
- *it = MathAtom(new InsetMathExFunc("det", del->cell(0)));
+ *it = MathAtom(new InsetMathExFunc(from_ascii("det"), del->cell(0)));
}
//lyxerr << "\ndet to: " << ar << endl;
}
// search numbers
//
-bool isDigitOrSimilar(char c)
+bool isDigitOrSimilar(char_type c)
{
return ('0' <= c && c <= '9') || c == '.';
}
// returns sequence of digits
-string digitSequence
+docstring digitSequence
(MathArray::const_iterator it, MathArray::const_iterator end)
{
- string s;
+ docstring s;
for (; it != end && (*it)->asCharInset(); ++it) {
if (!isDigitOrSimilar((*it)->getChar()))
break;
if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
continue;
- string s = digitSequence(ar.begin() + i, ar.end());
+ docstring s = digitSequence(ar.begin() + i, ar.end());
ar[i] = MathAtom(new InsetMathNumber(s));
ar.erase(i + 1, i + s.size());
MathAtom replaceParenDelims(const MathArray & ar)
{
- return MathAtom(new InsetMathDelim("(", ")", ar));
+ return MathAtom(new InsetMathDelim(from_ascii("("), from_ascii(")"), ar));
}
MathAtom replaceBracketDelims(const MathArray & ar)
{
- return MathAtom(new InsetMathDelim("[", "]", ar));
+ return MathAtom(new InsetMathDelim(from_ascii("["), from_ascii("]"), ar));
}
MathArray::iterator it = ar.begin() + i;
MathArray::iterator jt = it + 1;
- string name;
+ docstring name;
// is it a function?
// it certainly is if it is well known...
if (!extractFunctionName(*it, name)) {
// search integrals
//
-bool testSymbol(MathAtom const & at, string const & name)
+bool testSymbol(MathAtom const & at, docstring const & name)
{
return at->asSymbolInset() && at->asSymbolInset()->name() == name;
}
+bool testSymbol(MathAtom const & at, char const * const name)
+{
+ return at->asSymbolInset() && at->asSymbolInset()->name() == from_ascii(name);
+}
+
+
bool testIntSymbol(MathAtom const & at)
{
- return testSymbol(at, "int");
+ return testSymbol(at, from_ascii("int"));
}
continue;
// core ist part from behind the scripts to the 'd'
- auto_ptr<InsetMathExInt> p(new InsetMathExInt("int"));
+ auto_ptr<InsetMathExInt> p(new InsetMathExInt(from_ascii("int")));
// handle scripts if available
if (!testIntSymbol(*it)) {
bool testSumSymbol(MathAtom const & p)
{
- return testSymbol(p, "sum");
+ return testSymbol(p, from_ascii("sum"));
}
continue;
// create a proper inset as replacement
- auto_ptr<InsetMathExInt> p(new InsetMathExInt("sum"));
+ auto_ptr<InsetMathExInt> p(new InsetMathExInt(from_ascii("sum")));
// collect lower bound and summation index
InsetMathScript const * sub = ar[i]->asScriptInset();
}
-void mathmlize(MathArray const & dat, MathMLStream & os)
+void mathmlize(MathArray const & dat, MathStream & os)
{
MathArray ar = dat;
extractStructure(ar);
namespace {
- string captureOutput(string const & cmd, string const & data)
+ std::string captureOutput(std::string const & cmd, std::string const & data)
{
- string command = "echo '" + data + "' | " + cmd;
+ std::string command = "echo '" + data + "' | " + cmd;
lyxerr << "calling: " << command << endl;
cmd_ret const ret = runCommand(command);
return ret.second;
}
- string::size_type get_matching_brace(string const & str, string::size_type i)
+ size_t get_matching_brace(std::string const & str, size_t i)
{
int count = 1;
- string::size_type n = str.size();
+ size_t n = str.size();
while (i < n) {
i = str.find_first_of("{}", i+1);
- if (i == string::npos) return i;
+ if (i == npos)
+ return i;
if (str[i] == '{')
++count;
else
if (count == 0)
return i;
}
- return string::npos;
+ return npos;
}
- string::size_type get_matching_brace_back(string const & str, string::size_type i)
+ size_t get_matching_brace_back(std::string const & str, size_t i)
{
int count = 1;
while (i > 0) {
i = str.find_last_of("{}", i-1);
- if (i == string::npos) return i;
+ if (i == npos)
+ return i;
if (str[i] == '}')
++count;
else
if (count == 0)
return i;
}
- return string::npos;
+ return npos;
}
MathArray pipeThroughMaxima(docstring const &, MathArray const & ar)
odocstringstream os;
MaximaStream ms(os);
ms << ar;
- // FIXME UNICODE Is utf8 encoding correct?
- string expr = to_utf8(os.str());
- string const header = "simpsum:true;";
+ docstring expr = os.str();
+ docstring const header = from_ascii("simpsum:true;");
- string out;
+ std::string out;
for (int i = 0; i < 100; ++i) { // at most 100 attempts
// try to fix missing '*' the hard way
//
// 2x;
// ^
//
- lyxerr << "checking expr: '" << expr << "'" << endl;
- string full = header + "tex(" + expr + ");";
- out = captureOutput("maxima", full);
+ lyxerr << "checking expr: '" << to_utf8(expr) << "'" << endl;
+ docstring full = header + "tex(" + expr + ");";
+ out = captureOutput("maxima", to_utf8(full));
// leave loop if expression syntax is probably ok
- if (out.find("Incorrect syntax") == string::npos)
+ if (out.find("Incorrect syntax") == npos)
break;
// search line with "Incorrect syntax"
istringstream is(out);
- string line;
+ std::string line;
while (is) {
getline(is, line);
- if (line.find("Incorrect syntax") != string::npos)
+ if (line.find("Incorrect syntax") != npos)
break;
}
// 2nd next line is the one with caret
getline(is, line);
getline(is, line);
- string::size_type pos = line.find('^');
+ size_t pos = line.find('^');
lyxerr << "found caret at pos: '" << pos << "'" << endl;
- if (pos == string::npos || pos < 4)
+ if (pos == npos || pos < 4)
break; // caret position not found
pos -= 4; // skip the "tex(" part
if (expr[pos] == '*')
break; // two '*' in a row are definitely bad
- expr.insert(pos, "*");
+ expr.insert(pos, from_ascii("*"));
}
- vector<string> tmp = getVectorFromString(out, "$$");
+ vector<std::string> tmp = getVectorFromString(out, "$$");
if (tmp.size() < 2)
return MathArray();
- out = subst(tmp[1],"\\>", "");
+ out = subst(tmp[1], "\\>", std::string());
lyxerr << "out: '" << out << "'" << endl;
// Ugly code that tries to make the result prettier
-
- string::size_type i = out.find("\\mathchoice");
- while (i != string::npos) {
- string::size_type j = get_matching_brace(out, i + 12);
- string::size_type k = get_matching_brace(out, j + 1);
+ size_t i = out.find("\\mathchoice");
+ while (i != npos) {
+ size_t j = get_matching_brace(out, i + 12);
+ size_t k = get_matching_brace(out, j + 1);
k = get_matching_brace(out, k + 1);
k = get_matching_brace(out, k + 1);
- string mid = out.substr(i + 13,j - i - 13);
- if (mid.find("\\over") != string::npos)
+ std::string mid = out.substr(i + 13, j - i - 13);
+ if (mid.find("\\over") != npos)
mid = '{' + mid + '}';
out = out.substr(0,i)
+ mid
}
i = out.find("\\over");
- while (i != string::npos) {
- string::size_type j = get_matching_brace_back(out, i - 1);
- if (j == string::npos || j == 0) break;
- string::size_type k = get_matching_brace(out, i + 5);
- if (k == string::npos || k + 1 == out.size()) break;
+ while (i != npos) {
+ size_t j = get_matching_brace_back(out, i - 1);
+ if (j == npos || j == 0)
+ break;
+ size_t k = get_matching_brace(out, i + 5);
+ if (k == npos || k + 1 == out.size())
+ break;
out = out.substr(0,j - 1)
+ "\\frac"
+ out.substr(j,i - j)
i = out.find("\\over", i + 4);
}
MathArray res;
- mathed_parse_cell(res, out);
+ mathed_parse_cell(res, from_utf8(out));
return res;
}
MathArray pipeThroughMaple(docstring const & extra, MathArray const & ar)
{
- string header = "readlib(latex):\n";
+ std::string header = "readlib(latex):\n";
// remove the \\it for variable names
//"#`latex/csname_font` := `\\it `:"
//"#`latex/latex/symbol` "
// " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
- string trailer = "quit;";
+ std::string trailer = "quit;";
odocstringstream os;
MapleStream ms(os);
ms << ar;
- // FIXME UNICODE Is utf8 encoding correct?
- string expr = to_utf8(os.str());
+ std::string expr = to_utf8(os.str());
lyxerr << "ar: '" << ar << "'\n"
<< "ms: '" << expr << "'" << endl;
if (line.find("on line") != 0)
break; // error message not identified
getline(is, line);
- string::size_type pos = line.find('^');
+ size_t pos = line.find('^');
if (pos == string::npos || pos < 15)
break; // caret position not found
pos -= 15; // skip the "on line ..." part
//
MathArray res;
- mathed_parse_cell(res, out);
+ mathed_parse_cell(res, from_utf8(out));
return res;
}
odocstringstream os;
OctaveStream vs(os);
vs << ar;
- // FIXME UNICODE Is utf8 encoding correct?
string expr = to_utf8(os.str());
string out;
// found line with error, next line is the one with caret
getline(is, line);
- string::size_type pos = line.find('^');
+ size_t pos = line.find('^');
lyxerr << "caret line: '" << line << "'" << endl;
lyxerr << "found caret at pos: '" << pos << "'" << endl;
if (pos == string::npos || pos < 4)
out = out.substr(6);
// parse output as matrix or single number
- MathAtom at(new InsetMathArray("array", out));
+ MathAtom at(new InsetMathArray(from_ascii("array"), from_utf8(out)));
InsetMathArray const * mat = at->asArrayInset();
MathArray res;
if (mat->ncols() == 1 && mat->nrows() == 1)
res.append(mat->cell(0));
else {
- res.push_back(MathAtom(new InsetMathDelim("(", ")")));
+ res.push_back(MathAtom(new InsetMathDelim(from_ascii("("), from_ascii(")"))));
res.back().nucleus()->cell(0).push_back(at);
}
return res;
bool roman, bool translate)
{
string const macro = "\\" + macroName + "{";
- string::size_type const len = macro.length();
- string::size_type i = out.find(macro);
+ size_t const len = macro.length();
+ size_t i = out.find(macro);
- while (i != string::npos) {
- string::size_type const j = get_matching_brace(out, i + len);
+ while (i != npos) {
+ size_t const j = get_matching_brace(out, i + len);
string const name = out.substr(i + len, j - i - len);
out = out.substr(0, i)
+ (roman ? "\\mathrm{" : "")
out = captureOutput("math", full);
lyxerr << "out: '" << out << "'" << endl;
- string::size_type pos1 = out.find("Out[1]//TeXForm= ");
- string::size_type pos2 = out.find("In[2]:=");
+ size_t pos1 = out.find("Out[1]//TeXForm= ");
+ size_t pos2 = out.find("In[2]:=");
if (pos1 == string::npos || pos2 == string::npos)
return MathArray();
prettifyMathematicaOutput(out, "Mvariable", false, false);
MathArray res;
- mathed_parse_cell(res, out);
+ mathed_parse_cell(res, from_utf8(out));
return res;
}
// run external sript
string out = captureOutput(file, data);
MathArray res;
- mathed_parse_cell(res, out);
+ mathed_parse_cell(res, from_utf8(out));
return res;
}