diff options
| author | Vaxry <43317083+vaxerski@users.noreply.github.com> | 2026-04-28 18:10:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-28 18:10:25 +0100 |
| commit | 94b90a2cb0b4a0e13018cd5ebbf09394abf3b96b (patch) | |
| tree | 484d32baaab1acdaab7a817c86aa89f117a21059 /src/helpers/math/Expression.cpp | |
| parent | ab75bbfbecdfb4a0355106f740944c87cb9edd4c (diff) | |
config/lua: add ExpressionVec2, allow using a table for vec2 rules (#14197)
Diffstat (limited to 'src/helpers/math/Expression.cpp')
| -rw-r--r-- | src/helpers/math/Expression.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/helpers/math/Expression.cpp b/src/helpers/math/Expression.cpp index 3c0bee91..2289b85e 100644 --- a/src/helpers/math/Expression.cpp +++ b/src/helpers/math/Expression.cpp @@ -2,8 +2,50 @@ #include "muParser.h" #include "../../debug/log/Logger.hpp" +#include <cctype> +#include <format> + using namespace Math; +static std::string_view trimExprView(std::string_view s) { + while (!s.empty() && std::isspace(static_cast<unsigned char>(s.front()))) + s.remove_prefix(1); + + while (!s.empty() && std::isspace(static_cast<unsigned char>(s.back()))) + s.remove_suffix(1); + + return s; +} + +bool SExpressionVec2::empty() const { + return x.empty() || y.empty(); +} + +std::string SExpressionVec2::toString() const { + if (empty()) + return ""; + + return std::format("{} {}", x, y); +} + +std::expected<SExpressionVec2, std::string> Math::parseExpressionVec2(std::string_view raw) { + raw = trimExprView(raw); + if (raw.empty()) + return SExpressionVec2{}; + + const auto spacePos = raw.find_first_of(" \t\n\r\f\v"); + if (spacePos == std::string_view::npos) + return std::unexpected("expression vec2 requires two expressions separated by whitespace"); + + auto lhs = trimExprView(raw.substr(0, spacePos)); + auto rhs = trimExprView(raw.substr(spacePos + 1)); + + if (lhs.empty() || rhs.empty()) + return std::unexpected("expression vec2 requires two non-empty expressions"); + + return SExpressionVec2{std::string{lhs}, std::string{rhs}}; +} + CExpression::CExpression() : m_parser(makeUnique<mu::Parser>()) { ; } |
