| 48 | } |
| 49 | |
| 50 | MathParser::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 |
no test coverage detected