From d12b31815c052731aa40154728eb26daeb761937 Mon Sep 17 00:00:00 2001 From: Shakyan Kushwaha Date: Wed, 11 Dec 2024 15:18:54 +0530 Subject: [PATCH] [native] Refactor Duration::toString and DataSize::toString to use fmt::format --- .../presto_cpp/presto_protocol/core/DataSize.cpp | 13 +++++-------- .../presto_cpp/presto_protocol/core/Duration.cpp | 12 +++++------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/presto-native-execution/presto_cpp/presto_protocol/core/DataSize.cpp b/presto-native-execution/presto_cpp/presto_protocol/core/DataSize.cpp index 02c658abe3a35..afd089595b500 100644 --- a/presto-native-execution/presto_cpp/presto_protocol/core/DataSize.cpp +++ b/presto-native-execution/presto_cpp/presto_protocol/core/DataSize.cpp @@ -12,6 +12,7 @@ * limitations under the License. */ #include "presto_cpp/presto_protocol/core/DataSize.h" +#include #include namespace facebook::presto::protocol { @@ -29,16 +30,12 @@ DataSize::DataSize(const std::string& string) { } std::string DataSize::toString() const { - char buffer[32]; - snprintf( - buffer, - sizeof(buffer), - "%f%s", + return fmt::format( + "{:-f}{}", round(value_ * 100.0) / 100.0, - dataUnitToString(dataUnit_).c_str()); - return std::string(buffer); + dataUnitToString(dataUnit_) + ); } - double DataSize::toBytesPerDataUnit(DataUnit dataUnit) { switch (dataUnit) { case DataUnit::BYTE: diff --git a/presto-native-execution/presto_cpp/presto_protocol/core/Duration.cpp b/presto-native-execution/presto_cpp/presto_protocol/core/Duration.cpp index e6bdce6208677..720f5b64be036 100644 --- a/presto-native-execution/presto_cpp/presto_protocol/core/Duration.cpp +++ b/presto-native-execution/presto_cpp/presto_protocol/core/Duration.cpp @@ -12,6 +12,7 @@ * limitations under the License. */ #include "presto_cpp/presto_protocol/core/Duration.h" +#include namespace facebook::presto::protocol { @@ -27,14 +28,11 @@ Duration::Duration(const std::string& duration) { } std::string Duration::toString() const { - char buffer[32]; - snprintf( - buffer, - sizeof(buffer), - "%.2f%s", + return fmt::format( + "{:.2f}{}", value_, - timeUnitToString(timeUnit_).c_str()); - return std::string(buffer); + timeUnitToString(timeUnit_) + ); } double Duration::toMillisPerTimeUnit(TimeUnit timeUnit) {