Compare commits

...

6 commits

Author SHA1 Message Date
Artem Pavlenko
591da55914 update version to v4.0.4 [WIP] 2024-11-13 14:04:29 +00:00
Artem Pavlenko
8929dbdeb1 Unit tests - add explicit conversions test cases 2024-11-13 14:03:54 +00:00
Artem Pavlenko
dfdeda17fd expressions - add "bool(expr)" explicit conversion operator 2024-11-13 14:03:01 +00:00
Artem Pavlenko
2e175a16fe value::to_bool() - sync logic with Python bool() operator e.g if 0 -> false : else -> true 2024-11-13 14:00:50 +00:00
Artem Pavlenko
4e59b4c7c3 clang-format 2024-11-08 09:56:45 +00:00
Artem Pavlenko
3422472c7d Add int(),'float() and str()` implicit conversions ref #4477. 2024-11-08 09:53:43 +00:00
5 changed files with 39 additions and 2 deletions

View file

@ -217,6 +217,10 @@ struct unary_function_types_ : x3::symbols<unary_function_impl>
("log", log_impl()) // ("log", log_impl()) //
("abs", abs_impl()) // ("abs", abs_impl()) //
("length", length_impl()) // ("length", length_impl()) //
("bool", bool_impl()) //
("int", int_impl()) //
("float", float_impl()) //
("str", str_impl()) //
; ;
} }
} const unary_func_types; } const unary_func_types;

View file

@ -89,6 +89,30 @@ struct length_impl
value_type operator()(value_type const& val) const { return val.to_unicode().length(); } value_type operator()(value_type const& val) const { return val.to_unicode().length(); }
}; };
// str
struct str_impl
{
value_type operator()(value_type const& val) const { return val.to_unicode(); }
};
// bool
struct bool_impl
{
value_type operator()(value_type const& val) const { return val.to_bool(); }
};
// int
struct int_impl
{
value_type operator()(value_type const& val) const { return val.to_int(); }
};
// float
struct float_impl
{
value_type operator()(value_type const& val) const { return val.to_double(); }
};
// min // min
inline value_type min_impl(value_type const& arg1, value_type const& arg2) inline value_type min_impl(value_type const& arg1, value_type const& arg2)
{ {

View file

@ -27,7 +27,7 @@
#define MAPNIK_MAJOR_VERSION 4 #define MAPNIK_MAJOR_VERSION 4
#define MAPNIK_MINOR_VERSION 0 #define MAPNIK_MINOR_VERSION 0
#define MAPNIK_PATCH_VERSION 3 #define MAPNIK_PATCH_VERSION 4
#define MAPNIK_VERSION MAPNIK_VERSION_ENCODE(MAPNIK_MAJOR_VERSION, MAPNIK_MINOR_VERSION, MAPNIK_PATCH_VERSION) #define MAPNIK_VERSION MAPNIK_VERSION_ENCODE(MAPNIK_MAJOR_VERSION, MAPNIK_MINOR_VERSION, MAPNIK_PATCH_VERSION)

View file

@ -441,7 +441,7 @@ struct convert<value_bool>
template<typename T> template<typename T>
value_bool operator()(T val) const value_bool operator()(T val) const
{ {
return val > 0 ? true : false; return val == 0 ? false : true;
} }
}; };

View file

@ -94,6 +94,15 @@ TEST_CASE("expressions")
TRY_CHECK(parse_and_dump("'escaped \\' apostrophe'") == "'escaped \\' apostrophe'"); TRY_CHECK(parse_and_dump("'escaped \\' apostrophe'") == "'escaped \\' apostrophe'");
TRY_CHECK(parse_and_dump("'escaped \\\\ backslash'") == "'escaped \\\\ backslash'"); TRY_CHECK(parse_and_dump("'escaped \\\\ backslash'") == "'escaped \\\\ backslash'");
// explicit conversions
TRY_CHECK(eval("int('123')") == 123);
TRY_CHECK(eval("float('3.14'+'159')") == 3.14159);
TRY_CHECK(eval("bool(-0.001)") == true);
TRY_CHECK(eval("bool(0.001)") == true);
TRY_CHECK(eval("bool(0.0)") == false);
TRY_CHECK(eval("str(123)") == tr.transcode("123"));
TRY_CHECK(eval("float(str(3.14) + str(159))") == 3.14159);
// floating point constants // floating point constants
TRY_CHECK(parse_and_dump("pi") == "3.14159"); TRY_CHECK(parse_and_dump("pi") == "3.14159");
TRY_CHECK(parse_and_dump("deg_to_rad") == "0.0174533"); TRY_CHECK(parse_and_dump("deg_to_rad") == "0.0174533");