Skip to content

Double always with decimal #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions include/yaramod/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cctype>
#include <sstream>
#include <string>
#include <cmath>

namespace yaramod {

Expand Down Expand Up @@ -48,12 +49,44 @@ template <typename T>
std::string numToStr(const T num, std::ios_base &(*format)(std::ios_base&) = std::dec, bool showbase = false, bool toUpper = false)
{
std::ostringstream os;

// Set precision if num is floating point
if (std::is_floating_point<T>::value)
os.precision(std::numeric_limits<T>::digits10 - 1);

os << std::fixed;
if (toUpper)
os << std::uppercase;
if (showbase)
os << format << std::showbase << num;
else
os << format << num;

// Postprocess value if value is floating point
if (std::is_floating_point<T>::value)
{
std::string value = os.str();
auto comma = value.find('.');

// Generated string presentation do not have comma add .0 (xxxx)
if (comma == std::string::npos)
{
return value.append(".0");
}
// Generated string presentation have comma on last place (xxxx.)
if (comma == value.length() - 1)
{
return value.append("0");
}
// Generated string have format xxx.x
if (comma == value.length() - 2)
{
return value;
}
// Trim tailing zeros from generated float presentation (10.030000000 -> 10.03)
auto zero = value.find_last_not_of('0');
return {value.begin(), value.begin() + static_cast<long>(std::max(zero + 1, comma + 2))};
}
return os.str();
}

Expand Down
30 changes: 30 additions & 0 deletions tests/cpp/builder_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1927,5 +1927,35 @@ auto cond = boolVal(false).defined().get();
)", yaraFile->getTextFormatted());
}


TEST_F(BuilderTests,
FloatValueWorks) {
auto cond = doubleVal(0.0).get();

YaraRuleBuilder newRule;
auto rule = newRule
.withName("float_value_works")
.withCondition(cond)
.get();

YaraFileBuilder newFile;
auto yaraFile = newFile
.withRule(std::move(rule))
.get(true);

ASSERT_NE(nullptr, yaraFile);
EXPECT_EQ(R"(rule float_value_works {
condition:
0.0
})", yaraFile->getText());

EXPECT_EQ(R"(rule float_value_works
{
condition:
0.0
}
)", yaraFile->getTextFormatted());
}

}
}
40 changes: 40 additions & 0 deletions tests/cpp/parser_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3264,6 +3264,46 @@ rule same_variable_in_nested_for_loops
}
}

TEST_F(ParserTests,
FloatValueWorks) {
prepareInput(
R"(
rule rule_with_float_value_in_condition
{
condition:
0.8699322552472 == 0.8699322552472
}
)");

EXPECT_TRUE(driver.parse(input));
ASSERT_EQ(1u, driver.getParsedFile().getRules().size());

const auto& rule = driver.getParsedFile().getRules()[0];
EXPECT_EQ(R"(0.8699322552472 == 0.8699322552472)", rule->getCondition()->getText());

EXPECT_EQ(input_text, driver.getParsedFile().getTextFormatted());
}

TEST_F(ParserTests,
FloatValueWorks2) {
prepareInput(
R"(
rule rule_with_float_value_in_condition
{
condition:
0.0000000001 == 0.0000000001
}
)");

EXPECT_TRUE(driver.parse(input));
ASSERT_EQ(1u, driver.getParsedFile().getRules().size());

const auto& rule = driver.getParsedFile().getRules()[0];
EXPECT_EQ(R"(0.0000000001 == 0.0000000001)", rule->getCondition()->getText());

EXPECT_EQ(input_text, driver.getParsedFile().getTextFormatted());
}

TEST_F(ParserTests,
CuckooModuleWorks) {
prepareInput(
Expand Down
11 changes: 11 additions & 0 deletions tests/cpp/utils_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,16 @@ UnescapeStringWorks) {
EXPECT_EQ("\n\t\\\"\x01", unescapeString(R"(\n\t\\\"\x01)"));
}

TEST_F(UtilsTests,
NumToStrWorks) {
EXPECT_EQ("10", numToStr<int>(10));
EXPECT_EQ("a", numToStr<int>(10, std::hex));
EXPECT_EQ("10.0", numToStr<double>(10.0));
EXPECT_EQ("10.05", numToStr<double>(10.05));
EXPECT_EQ("0.0000000001", numToStr<double>(0.0000000001));
EXPECT_EQ("0.0", numToStr<double>(0.0));
EXPECT_EQ("0.869932552472", numToStr<double>(0.869932552472));
}

}
}