MCPcopy Create free account
hub / github.com/LibrePCB/LibrePCB / parse

Method parse

libs/librepcb/core/utils/mathparser.cpp:50–90  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48}
49
50MathParser::Result MathParser::parse(QString expression) const noexcept {
51 MathParser::Result result;
52
53 try {
54 const QString decimalPoint(mLocale.decimalPoint());
55 const QString groupSeparator(mLocale.groupSeparator());
56
57 // Always support '.' as decimal separator in addition to the
58 // locale-dependent separator, especially for German people
59 // (https://github.com/LibrePCB/LibrePCB/issues/1367).
60 if (decimalPoint != ".") {
61 expression.replace(".", decimalPoint);
62 }
63
64 mu::Parser parser;
65 parser.SetArgSep(';'); // avoid conflict with other separators
66 if (decimalPoint.length() == 1) {
67 parser.SetDecSep(decimalPoint.at(0).toLatin1());
68 }
69 if (groupSeparator.length() == 1) {
70 parser.SetThousandsSep(groupSeparator.at(0).toLatin1());
71 }
72#if defined(_UNICODE)
73 parser.SetExpr(expression.toStdWString());
74#else
75 parser.SetExpr(expression.toStdString());
76#endif
77 result.value = static_cast<qreal>(parser.Eval()); // can throw
78 result.valid = true;
79 } catch (const mu::Parser::exception_type& e) {
80 result.valid = false;
81 result.error = tr("Failed to parse expression:") % "\n\n";
82#if defined(_UNICODE)
83 result.error += QString::fromStdWString(e.GetMsg());
84#else
85 result.error += QString::fromStdString(e.GetMsg());
86#endif
87 }
88
89 return result;
90}
91
92/*******************************************************************************
93 * End of File

Callers 4

executeMethod · 0.45
get_desktop_translationsFunction · 0.45
createNewWindowMethod · 0.45
updateValueFromTextMethod · 0.45

Calls 2

lengthMethod · 0.80
atMethod · 0.80

Tested by

no test coverage detected