SVG parse_svg_value: restore result to default value on parsing failure + update is_percent only if parsing is successful

This commit is contained in:
Artem Pavlenko 2021-04-20 12:31:10 +01:00
parent 865b0ec120
commit 3361db7b5d

View file

@ -265,10 +265,10 @@ double parse_svg_value(T & err_handler, const char* str, bool & is_percent)
css_unit_value units; css_unit_value units;
const char* cur = str; // phrase_parse mutates the first iterator const char* cur = str; // phrase_parse mutates the first iterator
const char* end = str + std::strlen(str); const char* end = str + std::strlen(str);
bool is_percent_;
auto apply_value = [&](auto const& ctx) { val = _attr(ctx); is_percent = false; }; auto apply_value = [&](auto const& ctx) { val = _attr(ctx); is_percent_ = false; };
auto apply_units = [&](auto const& ctx) { val *= _attr(ctx); }; auto apply_units = [&](auto const& ctx) { val *= _attr(ctx); };
auto apply_percent = [&](auto const& ctx) { val *= 0.01; is_percent = true; }; auto apply_percent = [&](auto const& ctx) { val *= 0.01; is_percent_ = true; };
if (!x3::phrase_parse(cur, end, if (!x3::phrase_parse(cur, end,
x3::double_[apply_value] x3::double_[apply_value]
@ -277,8 +277,13 @@ double parse_svg_value(T & err_handler, const char* str, bool & is_percent)
x3::lit('%')[apply_percent]), x3::lit('%')[apply_percent]),
x3::space) || (cur != end)) x3::space) || (cur != end))
{ {
val = 0.0; // restore to default on parsing failure
err_handler.on_error("SVG parse error: failed to parse <number> with value \"" + std::string(str) + "\""); err_handler.on_error("SVG parse error: failed to parse <number> with value \"" + std::string(str) + "\"");
} }
else
{
is_percent = is_percent_; // update only on success
}
return val; return val;
} }