Skip to content

Commit

Permalink
Fix sonar findings
Browse files Browse the repository at this point in the history
  • Loading branch information
andjordan committed Nov 22, 2023
1 parent 17c12ec commit 9722608
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
12 changes: 10 additions & 2 deletions infra/syntax/Json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ namespace infra
for (std::size_t count = fractional.size(); count < nanoValueWidth; ++count)
fractionalValue = fractionalValue * 10;

return JsonFloat(integer, fractionalValue, sign);
return JsonFloat(static_cast<uint32_t>(integer), fractionalValue, sign);
}

JsonObject::JsonObject(infra::BoundedConstString objectString)
Expand Down Expand Up @@ -1336,7 +1336,15 @@ namespace infra
if (token.Negative())
stream << '-';

stream << token.IntValue() << "." << infra::Width(9, '0') << token.NanoFractionalValue();
auto fractional = token.NanoFractionalValue();
auto width = nanoValueWidth;
while (width > 1 && (fractional % 10 == 0))
{
fractional = fractional / 10;
--width;
}

stream << token.IntValue() << "." << infra::Width(width, '0') << fractional;
}

void operator()(infra::JsonToken::Boolean token)
Expand Down
4 changes: 2 additions & 2 deletions infra/syntax/JsonFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ namespace infra
if (tag.NanoFractionalValue() % 1000000 == 0)
AddFractionalFloat(tagName.Raw(), tag.IntValue(), tag.NanoFractionalValue() / 1000000, tag.Negative(), milliValueWidth);
else
AddFractionalFloat(tagName.Raw(), tag.IntValue(), tag.NanoFractionalValue(), tag.Negative(), nanoValueWidth);
AddFractionalFloat(tagName.Raw(), tag.IntValue(), tag.NanoFractionalValue(), tag.Negative(), nanoValueWidth);
}

void JsonObjectFormatter::Add(const infra::JsonKeyValue& keyValue)
Expand Down Expand Up @@ -465,7 +465,7 @@ namespace infra

if (negative)
*stream << '-';

*stream << intValue << '.' << infra::Width(fractionalWidth, '0') << fractionalValue;
}

Expand Down
4 changes: 2 additions & 2 deletions infra/syntax/test/TestJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ TEST(JsonTokenizerTest, get_multiple_tokens)

TEST(JsonTokenizerTest, clean_json)
{
infra::BoundedString::WithStorage<512> data(R"({ "key" : "value", "key2" : 1234, "key3" : true })");
infra::BoundedString::WithStorage<512> data(R"({ "key" : "value", "key2" : 1234, "key3" : true, "key4" : -42.1 })");
infra::CleanJsonContents(data);
EXPECT_EQ(R"({"key":"value","key2":1234,"key3":true})", data);
EXPECT_EQ(R"({"key":"value","key2":1234,"key3":true,"key4":-42.1})", data);
}

TEST(JsonTokenizerTest, ValidJsonObject)
Expand Down
23 changes: 23 additions & 0 deletions infra/syntax/test/TestJsonFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ TEST(JsonObjectFormatter, add_milli_float)
{
infra::BoundedString::WithStorage<64> string;

{
infra::JsonObjectFormatter::WithStringStream formatter(infra::inPlace, string);
formatter.AddMilliFloat(infra::JsonString("tag"), 4, 002);
}

EXPECT_EQ(R"({ "tag":4.002 })", string);
}

TEST(JsonObjectFormatter, add_milli_float_jsonstring_key)
{
infra::BoundedString::WithStorage<64> string;

{
infra::JsonObjectFormatter::WithStringStream formatter(infra::inPlace, string);
formatter.AddMilliFloat("tag", 12, 34);
Expand Down Expand Up @@ -298,6 +310,17 @@ TEST(JsonObjectFormatter, add_key_jsonstring_negative_value_JsonFloat)
EXPECT_EQ(R"({ "tag":-55.300 })", string);
}

TEST(JsonObjectFormatter, add_key_jsonstring_nano_value_JsonFloat)
{
infra::BoundedString::WithStorage<64> string;

{
infra::JsonObjectFormatter::WithStringStream formatter(infra::inPlace, string);
formatter.Add(infra::JsonString{ "tag" }, infra::JsonValue(infra::InPlaceType<infra::JsonFloat>(), infra::JsonFloat{ 0, 5, false }));
}
EXPECT_EQ(R"({ "tag":0.000000005 })", string);
}

TEST(JsonObjectFormatter, add_key_jsonstring_value_JsonString)
{
infra::BoundedString::WithStorage<64> string;
Expand Down

0 comments on commit 9722608

Please sign in to comment.