From e3dd5900e25ffaabf9c13ac22d2278eda05d04f3 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 26 Sep 2021 02:23:10 +0200 Subject: [PATCH] patterns: Added parse_int and parse_float functions --- .../source/content/pl_builtin_functions.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/plugins/builtin/source/content/pl_builtin_functions.cpp b/plugins/builtin/source/content/pl_builtin_functions.cpp index e16810c1b..346e72760 100644 --- a/plugins/builtin/source/content/pl_builtin_functions.cpp +++ b/plugins/builtin/source/content/pl_builtin_functions.cpp @@ -213,6 +213,21 @@ namespace hex::plugin::builtin { return string.substr(pos, size); }); + /* parse_int(string, base) */ + ContentRegistry::PatternLanguageFunctions::add(nsStdString, "parse_int", 2, [](Evaluator *ctx, auto params) -> std::optional { + auto string = Token::literalToString(params[0], false); + auto base = Token::literalToUnsigned(params[1]); + + return s128(std::strtoll(string.c_str(), nullptr, base)); + }); + + /* parse_float(string) */ + ContentRegistry::PatternLanguageFunctions::add(nsStdString, "parse_float", 1, [](Evaluator *ctx, auto params) -> std::optional { + auto string = Token::literalToString(params[0], false); + + return double(std::strtod(string.c_str(), nullptr)); + }); + } ContentRegistry::PatternLanguageFunctions::Namespace nsStdHttp = { "std", "http" };