Skip to content
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

add processing for addition in unit strings #298

Merged
merged 7 commits into from
May 27, 2023
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
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ codecov:
coverage:
precision: 2
round: down
range: '90...100'
range: '95...100'
status:
project:
default:
Expand Down
34 changes: 34 additions & 0 deletions test/test_unit_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,40 @@ TEST(stringToUnits, rotSequences)
EXPECT_EQ(u1, unit_from_string("BTU-IT"));
}

TEST(stringToUnits, addition)
{
auto u1 = unit_from_string("cm+cm");
EXPECT_EQ(u1, precise_unit(2, precise::cm));

u1 = unit_from_string("km + m+ cm+mm");
EXPECT_EQ(u1, precise_unit(1001.011, precise::m));
u1 = unit_from_string("kilometer+ 3in");
EXPECT_EQ(u1, (1.0 * precise::km + 3.0 * precise::in).as_unit());

u1 = unit_from_string("km+ 7.0 feet");
EXPECT_EQ(u1, (1.0 * precise::km + 7.0 * precise::ft).as_unit());
u1 = unit_from_string("m*km+ ft*in");
EXPECT_EQ(
u1,
(1.0 * precise::km * precise::m + 1.0 * precise::ft * precise::in)
.as_unit());

u1 = unit_from_string("meter + hippyhoppy");
EXPECT_FALSE(is_valid(u1));

u1 = unit_from_string("hippyhoppy + meter");
EXPECT_FALSE(is_valid(u1));

u1 = unit_from_string("arggh + kilometer");
EXPECT_FALSE(is_valid(u1));

u1 = unit_from_string("kilometer + arggh");
EXPECT_FALSE(is_valid(u1));

u1 = unit_from_string("meter + kg");
EXPECT_EQ(u1, precise::m * precise::kg);
Comment on lines +1060 to +1061
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this turning addition into multiplication?

}

TEST(stringToUnits, cleanPhase2)
{
auto u1 = unit_from_string("tech-nical-at-mosphere");
Expand Down
54 changes: 54 additions & 0 deletions units/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4795,6 +4795,53 @@ static precise_unit
return precise::invalid;
}

/** handle addition of similar units as a regular unit*/
static precise_unit
checkUnitAddition(const std::string& unit_string, std::uint64_t match_flags)
{
auto sep = findOperatorSep(unit_string, "+");
if (sep != std::string::npos && sep > 0) {
if ((unit_string[sep - 1] == '+') || sep == unit_string.size() - 1 ||
unit_string[sep + 1] == '+') {
return precise::invalid;
}
precise_unit a_unit;
precise_unit b_unit;
if (sep + 1 > unit_string.size() / 2) {
b_unit = unit_from_string_internal(
unit_string.substr(sep + 1), match_flags);
if (!is_valid(b_unit)) {
return precise::invalid;
}
a_unit = unit_from_string_internal(
unit_string.substr(0, sep), match_flags);

if (!is_valid(a_unit)) {
return precise::invalid;
}
} else {
a_unit = unit_from_string_internal(
unit_string.substr(0, sep), match_flags);

if (!is_valid(a_unit)) {
return precise::invalid;
}
b_unit = unit_from_string_internal(
unit_string.substr(sep + 1), match_flags);
if (!is_valid(b_unit)) {
return precise::invalid;
}
}
auto res = convert(b_unit, a_unit);
if (!std::isnan(res)) {
return {
a_unit.base_units(),
a_unit.multiplier() + a_unit.multiplier() * res};
}
}
return precise::invalid;
}

precise_unit
unit_from_string(std::string unit_string, std::uint64_t match_flags)
{
Expand Down Expand Up @@ -4934,6 +4981,13 @@ static precise_unit unit_from_string_internal(
}
}

if (unit_string.find_first_of('+') != std::string::npos) {
retunit = checkUnitAddition(unit_string, match_flags);
if (is_valid(retunit)) {
return retunit;
}
}

auto sep = findOperatorSep(unit_string, "*/");
if (sep != std::string::npos) {
precise_unit a_unit;
Expand Down