Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Commit

Permalink
Fix operator+ for string and float (#579)
Browse files Browse the repository at this point in the history
* Fix operator+ for srting and float

* Remove folly::stringPrintf()
  • Loading branch information
Aiee committed Jul 8, 2021
1 parent 82524be commit fa5a8bd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
8 changes: 2 additions & 6 deletions src/common/datatypes/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1966,9 +1966,7 @@ Value operator+(const Value& lhs, const Value& rhs) {
return lhs.getFloat() + rhs.getFloat();
}
case Value::Type::STRING: {
return folly::stringPrintf("%lf%s",
lhs.getFloat(),
rhs.getStr().c_str());
return folly::to<std::string>(lhs.getFloat()) + rhs.getStr();
}
case Value::Type::LIST: {
auto ret = rhs.getList();
Expand All @@ -1993,9 +1991,7 @@ Value operator+(const Value& lhs, const Value& rhs) {
rhs.getInt());
}
case Value::Type::FLOAT: {
return folly::stringPrintf("%s%lf",
lhs.getStr().c_str(),
rhs.getFloat());
return lhs.getStr() + folly::to<std::string>(rhs.getFloat());
}
case Value::Type::STRING: {
return lhs.getStr() + rhs.getStr();
Expand Down
18 changes: 16 additions & 2 deletions src/common/datatypes/test/ValueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ TEST(Value, Arithmetics) {

v = vFloat1 + vStr2;
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("3.140000World"), v.getStr());
EXPECT_EQ(std::string("3.14World"), v.getStr());

v = vFloat2 + vInt2;
EXPECT_EQ(Value::Type::FLOAT, v.type());
Expand All @@ -92,7 +92,7 @@ TEST(Value, Arithmetics) {

v = vStr1 + vFloat2;
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("Hello 2.670000"), v.getStr());
EXPECT_EQ(std::string("Hello 2.67"), v.getStr());

v = vStr1 + vBool2;
EXPECT_EQ(Value::Type::STRING, v.type());
Expand Down Expand Up @@ -121,6 +121,20 @@ TEST(Value, Arithmetics) {
v = vList2 + vSet;
EXPECT_EQ(Value::Type::LIST, v.type());
EXPECT_EQ(List({6, 4, 5, Set({8, 7})}), v.getList());

// str + float
v = Value("Allen Wilson") + Value(30.142857142857142);
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("Allen Wilson30.142857142857142"), v.getStr());
v = Value("Allen Wilson") + Value(-30.142857142857142);
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("Allen Wilson-30.142857142857142"), v.getStr());
v = Value(30.142857142857142) + Value("Allen Wilson");
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("30.142857142857142Allen Wilson"), v.getStr());
v = Value(-30.142857142857142) + Value("Allen Wilson");
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("-30.142857142857142Allen Wilson"), v.getStr());
}
// -
{
Expand Down

0 comments on commit fa5a8bd

Please sign in to comment.